from collections import Counter



H,W = list(map(int,input().split()))

S = [eval(input()) for i in range(H)]



class UnionFind:

    def __init__(self,N):

        self.parent = [i for i in range(N)]

        self.rank = [0] * N

        self.count = 0

    def root(self,a):

        if self.parent[a] == a:

            return a

        else:

            self.parent[a] = self.root(self.parent[a])

            return self.parent[a]

    def is_same(self,a,b):

        return self.root(a) == self.root(b)

    def unite(self,a,b):

        ra = self.root(a)

        rb = self.root(b)

        if ra == rb: return

        if self.rank[ra] < self.rank[rb]:

            self.parent[ra] = rb

        else:

            self.parent[rb] = ra

            if self.rank[ra] == self.rank[rb]: self.rank[ra] += 1

        self.count += 1



uf = UnionFind(H*W)



for i in range(H):

    for j in range(W):

        if j < W-1 and S[i][j] != S[i][j+1]:

            uf.unite(i*W+j, i*W+j+1)

        if i < H-1 and S[i][j] != S[i+1][j]:

            uf.unite(i*W+j, (i+1)*W+j)

for i in range(H*W):

    uf.root(i)



c1 = Counter()

c2 = Counter()



for i in range(H):

    for j in range(W):

        k = i*W+j

        if S[i][j] == '.':

            c1[uf.root(k)] += 1

        else:

            c2[uf.root(k)] += 1



ans = 0

for k in list(c1.keys()):

    ans += c1[k] * c2[k]

print(ans)
H,W = list(map(int,input().split()))

S = [eval(input()) for i in range(H)]



class UnionFind:

    def __init__(self,N):

        self.parent = [i for i in range(N)]

        self._size = [1] * N

        self.count = 0

    def root(self,a):

        if self.parent[a] == a:

            return a

        else:

            self.parent[a] = self.root(self.parent[a])

            return self.parent[a]

    def is_same(self,a,b):

        return self.root(a) == self.root(b)

    def unite(self,a,b):

        ra = self.root(a)

        rb = self.root(b)

        if ra == rb: return

        if self._size[ra] < self._size[rb]: ra,rb = rb,ra

        self._size[ra] += self._size[rb]

        self.parent[rb] = ra

        self.count += 1

    def size(self,a):

        return self._size[self.root(a)]



uf = UnionFind(H*W)

for i in range(H):

    for j in range(W):

        if i < H-1 and S[i][j] != S[i+1][j]:

            uf.unite(i*W+j, (i+1)*W+j)

        if j < W-1 and S[i][j] != S[i][j+1]:

            uf.unite(i*W+j, i*W+j+1)

for i in range(H*W):

    uf.root(i)



from collections import defaultdict

bl = defaultdict(lambda: 0)

wh = defaultdict(lambda: 0)

for i in range(H):

    for j in range(W):

        v = i*W+j

        r = uf.root(v)

        if S[i][j]=='#':

            bl[r] += 1

        else:

            wh[r] += 1



ans = 0

for k in list(bl.keys()):

    ans += bl[k] * wh[k]

print(ans)
from collections import Counter

H, W = list(map(int, input().split()))
S = [eval(input()) for i in range(H)]


class UnionFind:
    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.rank = [0] * N
        self.count = 0

    def root(self, a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]

    def is_same(self, a, b):
        return self.root(a) == self.root(b)

    def unite(self, a, b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb:
            return
        if self.rank[ra] < self.rank[rb]:
            self.parent[ra] = rb
        else:
            self.parent[rb] = ra
            if self.rank[ra] == self.rank[rb]:
                self.rank[ra] += 1
        self.count += 1


uf = UnionFind(H * W)
for i in range(H):
    for j in range(W):
        if j < W - 1 and S[i][j] != S[i][j + 1]:
            uf.unite(i * W + j, i * W + j + 1)
        if i < H - 1 and S[i][j] != S[i + 1][j]:
            uf.unite(i * W + j, (i + 1) * W + j)
for i in range(H * W):
    uf.root(i)
c1 = Counter()
c2 = Counter()
for i in range(H):
    for j in range(W):
        k = i * W + j
        if S[i][j] == ".":
            c1[uf.root(k)] += 1
        else:
            c2[uf.root(k)] += 1
ans = 0
for k in list(c1.keys()):
    ans += c1[k] * c2[k]
print(ans)

H, W = list(map(int, input().split()))
S = [eval(input()) for i in range(H)]


class UnionFind:
    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self._size = [1] * N
        self.count = 0

    def root(self, a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]

    def is_same(self, a, b):
        return self.root(a) == self.root(b)

    def unite(self, a, b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb:
            return
        if self._size[ra] < self._size[rb]:
            ra, rb = rb, ra
        self._size[ra] += self._size[rb]
        self.parent[rb] = ra
        self.count += 1

    def size(self, a):
        return self._size[self.root(a)]


uf = UnionFind(H * W)
for i in range(H):
    for j in range(W):
        if i < H - 1 and S[i][j] != S[i + 1][j]:
            uf.unite(i * W + j, (i + 1) * W + j)
        if j < W - 1 and S[i][j] != S[i][j + 1]:
            uf.unite(i * W + j, i * W + j + 1)
for i in range(H * W):
    uf.root(i)
from collections import defaultdict

bl = defaultdict(lambda: 0)
wh = defaultdict(lambda: 0)
for i in range(H):
    for j in range(W):
        v = i * W + j
        r = uf.root(v)
        if S[i][j] == "#":
            bl[r] += 1
        else:
            wh[r] += 1
ans = 0
for k in list(bl.keys()):
    ans += bl[k] * wh[k]
print(ans)

-from collections import Counter - -        self.rank = [0] * N +        self._size = [1] * N -        if self.rank[ra] < self.rank[rb]: -            self.parent[ra] = rb -        else: -            self.parent[rb] = ra -            if self.rank[ra] == self.rank[rb]: -                self.rank[ra] += 1 +        if self._size[ra] < self._size[rb]: +            ra, rb = rb, ra +        self._size[ra] += self._size[rb] +        self.parent[rb] = ra + +    def size(self, a): +        return self._size[self.root(a)] +        if i < H - 1 and S[i][j] != S[i + 1][j]: +            uf.unite(i * W + j, (i + 1) * W + j) -        if i < H - 1 and S[i][j] != S[i + 1][j]: -            uf.unite(i * W + j, (i + 1) * W + j) -c1 = Counter() -c2 = Counter() +from collections import defaultdict + +bl = defaultdict(lambda: 0) +wh = defaultdict(lambda: 0) -        k = i * W + j -        if S[i][j] == ".": -            c1[uf.root(k)] += 1 +        v = i * W + j +        r = uf.root(v) +        if S[i][j] == "#": +            bl[r] += 1 -            c2[uf.root(k)] += 1 +            wh[r] += 1 -for k in list(c1.keys()): -    ans += c1[k] * c2[k] +for k in list(bl.keys()): +    ans += bl[k] * wh[k]