from collections import deque

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

S = [input().strip() for _ in range(H)]

D = {0:".",1:"#"}

G = {}

C = {}

hist = [[-1 for _ in range(W)] for _ in range(H)]

col = -1

for i in range(H):

    for j in range(W):

        if S[i][j]=="#" and hist[i][j]==-1:

            col += 1

            G[col] = [(i,j)]

            que = deque([(i,j,1)])

            hist[i][j] = col

            cnt = 0

            while que:

                y,x,a = que.popleft()

                if x+1<W and S[y][x+1]==D[1-a]:

                    if a==1 and hist[y][x+1]!=col:

                        cnt += 1

                        que.append((y,x+1,1-a))

                        hist[y][x+1]=col

                    elif a==0 and hist[y][x+1]==-1:

                        G[col].append((y,x+1))

                        que.append((y,x+1,1-a))

                        hist[y][x+1]=col

                if y-1>-1 and S[y-1][x]==D[1-a]:

                    if a==1 and hist[y-1][x]!=col:

                        cnt += 1

                        que.append((y-1,x,1-a))

                        hist[y-1][x]=col

                    elif a==0 and hist[y-1][x]==-1:

                        G[col].append((y-1,x))

                        que.append((y-1,x,1-a))

                        hist[y-1][x]=col

                if x-1>-1 and S[y][x-1]==D[1-a]:

                    if a==1 and hist[y][x-1]!=col:

                        cnt += 1

                        que.append((y,x-1,1-a))

                        hist[y][x-1]=col

                    elif a==0 and hist[y][x-1]==-1:

                        G[col].append((y,x-1))

                        hist[y][x-1]=col

                        que.append((y,x-1,1-a))

                if y+1<H and S[y+1][x]==D[1-a]:

                    if a==1 and hist[y+1][x]!=col:

                        cnt += 1

                        que.append((y+1,x,1-a))

                        hist[y+1][x]=col

                    elif a==0 and hist[y+1][x]==-1:

                        G[col].append((y+1,x))

                        que.append((y+1,x,1-a))

                        hist[y+1][x]=col

            C[col] = cnt

tot = 0

for col in G:

    tot += len(G[col])*C[col]

print(tot)
from collections import deque

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

S = [input().strip() for _ in range(H)]

hist = [[0 for _ in range(W)] for _ in range(H)]

col = 1

for i in range(H):

    for j in range(W):

        if hist[i][j]==0:

            hist[i][j]=col

            que = deque([(i,j,S[i][j])])

            while que:

                y,x,c = que.popleft()

                if x+1<W and S[y][x+1]!=c and hist[y][x+1]==0:

                    hist[y][x+1]=col

                    que.append((y,x+1,S[y][x+1]))

                if y-1>=0 and S[y-1][x]!=c and hist[y-1][x]==0:

                    hist[y-1][x]=col

                    que.append((y-1,x,S[y-1][x]))

                if x-1>=0 and S[y][x-1]!=c and hist[y][x-1]==0:

                    hist[y][x-1]=col

                    que.append((y,x-1,S[y][x-1]))

                if y+1<H and S[y+1][x]!=c and hist[y+1][x]==0:

                    hist[y+1][x]=col

                    que.append((y+1,x,S[y+1][x]))

            col += 1

C = {}

for i in range(H):

    for j in range(W):

        col = hist[i][j]

        if col not in C:

            C[col] = [0,0]

        C[col][0] += 1

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

            C[col][1] += 1

cnt = 0

for col in C:

    if col!=0:

        tot = C[col][0]

        x = C[col][1]

        cnt += x*(tot-x)

print(cnt)
from collections import deque

H, W = list(map(int, input().split()))
S = [input().strip() for _ in range(H)]
D = {0: ".", 1: "#"}
G = {}
C = {}
hist = [[-1 for _ in range(W)] for _ in range(H)]
col = -1
for i in range(H):
    for j in range(W):
        if S[i][j] == "#" and hist[i][j] == -1:
            col += 1
            G[col] = [(i, j)]
            que = deque([(i, j, 1)])
            hist[i][j] = col
            cnt = 0
            while que:
                y, x, a = que.popleft()
                if x + 1 < W and S[y][x + 1] == D[1 - a]:
                    if a == 1 and hist[y][x + 1] != col:
                        cnt += 1
                        que.append((y, x + 1, 1 - a))
                        hist[y][x + 1] = col
                    elif a == 0 and hist[y][x + 1] == -1:
                        G[col].append((y, x + 1))
                        que.append((y, x + 1, 1 - a))
                        hist[y][x + 1] = col
                if y - 1 > -1 and S[y - 1][x] == D[1 - a]:
                    if a == 1 and hist[y - 1][x] != col:
                        cnt += 1
                        que.append((y - 1, x, 1 - a))
                        hist[y - 1][x] = col
                    elif a == 0 and hist[y - 1][x] == -1:
                        G[col].append((y - 1, x))
                        que.append((y - 1, x, 1 - a))
                        hist[y - 1][x] = col
                if x - 1 > -1 and S[y][x - 1] == D[1 - a]:
                    if a == 1 and hist[y][x - 1] != col:
                        cnt += 1
                        que.append((y, x - 1, 1 - a))
                        hist[y][x - 1] = col
                    elif a == 0 and hist[y][x - 1] == -1:
                        G[col].append((y, x - 1))
                        hist[y][x - 1] = col
                        que.append((y, x - 1, 1 - a))
                if y + 1 < H and S[y + 1][x] == D[1 - a]:
                    if a == 1 and hist[y + 1][x] != col:
                        cnt += 1
                        que.append((y + 1, x, 1 - a))
                        hist[y + 1][x] = col
                    elif a == 0 and hist[y + 1][x] == -1:
                        G[col].append((y + 1, x))
                        que.append((y + 1, x, 1 - a))
                        hist[y + 1][x] = col
            C[col] = cnt
tot = 0
for col in G:
    tot += len(G[col]) * C[col]
print(tot)

from collections import deque

H, W = list(map(int, input().split()))
S = [input().strip() for _ in range(H)]
hist = [[0 for _ in range(W)] for _ in range(H)]
col = 1
for i in range(H):
    for j in range(W):
        if hist[i][j] == 0:
            hist[i][j] = col
            que = deque([(i, j, S[i][j])])
            while que:
                y, x, c = que.popleft()
                if x + 1 < W and S[y][x + 1] != c and hist[y][x + 1] == 0:
                    hist[y][x + 1] = col
                    que.append((y, x + 1, S[y][x + 1]))
                if y - 1 >= 0 and S[y - 1][x] != c and hist[y - 1][x] == 0:
                    hist[y - 1][x] = col
                    que.append((y - 1, x, S[y - 1][x]))
                if x - 1 >= 0 and S[y][x - 1] != c and hist[y][x - 1] == 0:
                    hist[y][x - 1] = col
                    que.append((y, x - 1, S[y][x - 1]))
                if y + 1 < H and S[y + 1][x] != c and hist[y + 1][x] == 0:
                    hist[y + 1][x] = col
                    que.append((y + 1, x, S[y + 1][x]))
            col += 1
C = {}
for i in range(H):
    for j in range(W):
        col = hist[i][j]
        if col not in C:
            C[col] = [0, 0]
        C[col][0] += 1
        if S[i][j] == "#":
            C[col][1] += 1
cnt = 0
for col in C:
    if col != 0:
        tot = C[col][0]
        x = C[col][1]
        cnt += x * (tot - x)
print(cnt)

-D = {0: ".", 1: "#"} -G = {} -C = {} -hist = [[-1 for _ in range(W)] for _ in range(H)] -col = -1 +hist = [[0 for _ in range(W)] for _ in range(H)] +col = 1 -        if S[i][j] == "#" and hist[i][j] == -1: +        if hist[i][j] == 0: +            hist[i][j] = col +            que = deque([(i, j, S[i][j])]) +            while que: +                y, x, c = que.popleft() +                if x + 1 < W and S[y][x + 1] != c and hist[y][x + 1] == 0: +                    hist[y][x + 1] = col +                    que.append((y, x + 1, S[y][x + 1])) +                if y - 1 >= 0 and S[y - 1][x] != c and hist[y - 1][x] == 0: +                    hist[y - 1][x] = col +                    que.append((y - 1, x, S[y - 1][x])) +                if x - 1 >= 0 and S[y][x - 1] != c and hist[y][x - 1] == 0: +                    hist[y][x - 1] = col +                    que.append((y, x - 1, S[y][x - 1])) +                if y + 1 < H and S[y + 1][x] != c and hist[y + 1][x] == 0: +                    hist[y + 1][x] = col +                    que.append((y + 1, x, S[y + 1][x])) -            G[col] = [(i, j)] -            que = deque([(i, j, 1)]) -            hist[i][j] = col -            cnt = 0 -            while que: -                y, x, a = que.popleft() -                if x + 1 < W and S[y][x + 1] == D[1 - a]: -                    if a == 1 and hist[y][x + 1] != col: -                        cnt += 1 -                        que.append((y, x + 1, 1 - a)) -                        hist[y][x + 1] = col -                    elif a == 0 and hist[y][x + 1] == -1: -                        G[col].append((y, x + 1)) -                        que.append((y, x + 1, 1 - a)) -                        hist[y][x + 1] = col -                if y - 1 > -1 and S[y - 1][x] == D[1 - a]: -                    if a == 1 and hist[y - 1][x] != col: -                        cnt += 1 -                        que.append((y - 1, x, 1 - a)) -                        hist[y - 1][x] = col -                    elif a == 0 and hist[y - 1][x] == -1: -                        G[col].append((y - 1, x)) -                        que.append((y - 1, x, 1 - a)) -                        hist[y - 1][x] = col -                if x - 1 > -1 and S[y][x - 1] == D[1 - a]: -                    if a == 1 and hist[y][x - 1] != col: -                        cnt += 1 -                        que.append((y, x - 1, 1 - a)) -                        hist[y][x - 1] = col -                    elif a == 0 and hist[y][x - 1] == -1: -                        G[col].append((y, x - 1)) -                        hist[y][x - 1] = col -                        que.append((y, x - 1, 1 - a)) -                if y + 1 < H and S[y + 1][x] == D[1 - a]: -                    if a == 1 and hist[y + 1][x] != col: -                        cnt += 1 -                        que.append((y + 1, x, 1 - a)) -                        hist[y + 1][x] = col -                    elif a == 0 and hist[y + 1][x] == -1: -                        G[col].append((y + 1, x)) -                        que.append((y + 1, x, 1 - a)) -                        hist[y + 1][x] = col -            C[col] = cnt -tot = 0 -for col in G: -    tot += len(G[col]) * C[col] -print(tot) +C = {} +for i in range(H): +    for j in range(W): +        col = hist[i][j] +        if col not in C: +            C[col] = [0, 0] +        C[col][0] += 1 +        if S[i][j] == "#": +            C[col][1] += 1 +cnt = 0 +for col in C: +    if col != 0: +        tot = C[col][0] +        x = C[col][1] +        cnt += x * (tot - x) +print(cnt)