import collections

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

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

M=[[-1]*W for _ in [0]*H]

k=0

for y in range(H):

    for x in range(W):

        if M[y][x]!=-1:continue #visited

        M[y][x]=k

        Q=collections.deque()

        Q.append((y,x))

        while len(Q)>0:

            i,j=Q.popleft()

            s=S[i][j]

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

                M[i+1][j]=k

                Q.append((i+1,j))

            if i-1>=0 and M[i-1][j]==-1 and s!=S[i-1][j]:

                M[i-1][j]=k

                Q.append((i-1,j))

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

                M[i][j+1]=k

                Q.append((i,j+1))

            if j-1>=0 and M[i][j-1]==-1 and s!=S[i][j-1]:

                M[i][j-1]=k

                Q.append((i,j-1))

            #print(k,M,Q)

        k+=1

ds={}

dd={}

for i in range(H):

    for j in range(W):

        m=M[i][j]

        if S[i][j]=='#':ds[m]=ds.get(m,0)+1

        else:dd[m]=dd.get(m,0)+1

r=0

for i in range(k):

    r += ds.get(i,0)*dd.get(i,0)

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

h=list(range(H))

S=[eval(input()) for _ in h]

M=[[-1]*W for _ in h]

k=0

for y in h:

    for x in range(W):

        if M[y][x]!=-1:continue

        M[y][x]=k;Q=[(y,x)]

        while len(Q)>0:

            i,j=Q.pop();s=S[i][j];p=i+1;q=i-1;r=j+1;t=j-1

            if p<H and M[p][j]==-1 and s!=S[p][j]:M[p][j]=k;Q+=[(p,j)]

            if i>0 and M[q][j]==-1 and s!=S[q][j]:M[q][j]=k;Q+=[(q,j)]

            if r<W and M[i][r]==-1 and s!=S[i][r]:M[i][r]=k;Q+=[(i,r)]

            if j>0 and M[i][t]==-1 and s!=S[i][t]:M[i][t]=k;Q+=[(i,t)]

        k+=1

s={}

d={}

for i in range(H):

    for j in range(W):

        m=M[i][j]

        if S[i][j]=='#':s[m]=s.get(m,0)+1

        else:d[m]=d.get(m,0)+1

print((sum(s.get(i,0)*d.get(i,0)for i in range(k))))
import collections

H, W = list(map(int, input().split()))
S = [eval(input()) for i in [0] * H]
M = [[-1] * W for _ in [0] * H]
k = 0
for y in range(H):
    for x in range(W):
        if M[y][x] != -1:
            continue  # visited
        M[y][x] = k
        Q = collections.deque()
        Q.append((y, x))
        while len(Q) > 0:
            i, j = Q.popleft()
            s = S[i][j]
            if i + 1 < H and M[i + 1][j] == -1 and s != S[i + 1][j]:
                M[i + 1][j] = k
                Q.append((i + 1, j))
            if i - 1 >= 0 and M[i - 1][j] == -1 and s != S[i - 1][j]:
                M[i - 1][j] = k
                Q.append((i - 1, j))
            if j + 1 < W and M[i][j + 1] == -1 and s != S[i][j + 1]:
                M[i][j + 1] = k
                Q.append((i, j + 1))
            if j - 1 >= 0 and M[i][j - 1] == -1 and s != S[i][j - 1]:
                M[i][j - 1] = k
                Q.append((i, j - 1))
            # print(k,M,Q)
        k += 1
ds = {}
dd = {}
for i in range(H):
    for j in range(W):
        m = M[i][j]
        if S[i][j] == "#":
            ds[m] = ds.get(m, 0) + 1
        else:
            dd[m] = dd.get(m, 0) + 1
r = 0
for i in range(k):
    r += ds.get(i, 0) * dd.get(i, 0)
print(r)

H, W = list(map(int, input().split()))
h = list(range(H))
S = [eval(input()) for _ in h]
M = [[-1] * W for _ in h]
k = 0
for y in h:
    for x in range(W):
        if M[y][x] != -1:
            continue
        M[y][x] = k
        Q = [(y, x)]
        while len(Q) > 0:
            i, j = Q.pop()
            s = S[i][j]
            p = i + 1
            q = i - 1
            r = j + 1
            t = j - 1
            if p < H and M[p][j] == -1 and s != S[p][j]:
                M[p][j] = k
                Q += [(p, j)]
            if i > 0 and M[q][j] == -1 and s != S[q][j]:
                M[q][j] = k
                Q += [(q, j)]
            if r < W and M[i][r] == -1 and s != S[i][r]:
                M[i][r] = k
                Q += [(i, r)]
            if j > 0 and M[i][t] == -1 and s != S[i][t]:
                M[i][t] = k
                Q += [(i, t)]
        k += 1
s = {}
d = {}
for i in range(H):
    for j in range(W):
        m = M[i][j]
        if S[i][j] == "#":
            s[m] = s.get(m, 0) + 1
        else:
            d[m] = d.get(m, 0) + 1
print((sum(s.get(i, 0) * d.get(i, 0) for i in range(k))))

-import collections - -S = [eval(input()) for i in [0] * H] -M = [[-1] * W for _ in [0] * H] +h = list(range(H)) +S = [eval(input()) for _ in h] +M = [[-1] * W for _ in h] -for y in range(H): +for y in h: -            continue  # visited +            continue -        Q = collections.deque() -        Q.append((y, x)) +        Q = [(y, x)] -            i, j = Q.popleft() +            i, j = Q.pop() -            if i + 1 < H and M[i + 1][j] == -1 and s != S[i + 1][j]: -                M[i + 1][j] = k -                Q.append((i + 1, j)) -            if i - 1 >= 0 and M[i - 1][j] == -1 and s != S[i - 1][j]: -                M[i - 1][j] = k -                Q.append((i - 1, j)) -            if j + 1 < W and M[i][j + 1] == -1 and s != S[i][j + 1]: -                M[i][j + 1] = k -                Q.append((i, j + 1)) -            if j - 1 >= 0 and M[i][j - 1] == -1 and s != S[i][j - 1]: -                M[i][j - 1] = k -                Q.append((i, j - 1)) -            # print(k,M,Q) +            p = i + 1 +            q = i - 1 +            r = j + 1 +            t = j - 1 +            if p < H and M[p][j] == -1 and s != S[p][j]: +                M[p][j] = k +                Q += [(p, j)] +            if i > 0 and M[q][j] == -1 and s != S[q][j]: +                M[q][j] = k +                Q += [(q, j)] +            if r < W and M[i][r] == -1 and s != S[i][r]: +                M[i][r] = k +                Q += [(i, r)] +            if j > 0 and M[i][t] == -1 and s != S[i][t]: +                M[i][t] = k +                Q += [(i, t)] -ds = {} -dd = {} +s = {} +d = {} -            ds[m] = ds.get(m, 0) + 1 +            s[m] = s.get(m, 0) + 1 -            dd[m] = dd.get(m, 0) + 1 -r = 0 -for i in range(k): -    r += ds.get(i, 0) * dd.get(i, 0) -print(r) +            d[m] = d.get(m, 0) + 1 +print((sum(s.get(i, 0) * d.get(i, 0) for i in range(k))))