import sys



sys.setrecursionlimit(1000000)





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

S=[]

for i in range(H):

    S.append(eval(input()))



def DFS(h,w,black,white,m):

    black,white=BorW(h,w,black,white)

    step=[[0,1],[0,-1],[1,0],[-1,0]]

    seen[h][w]=1

    for dh,dw in step:

        nh=h+dh

        nw=w+dw

        if 0<=nh<H and 0<=nw<W:

            if seen[nh][nw]==0 and m!=S[nh][nw]:

                seen[nh][nw]=1

                black,white=DFS(nh,nw,black,white,S[nh][nw])

    return black,white





def BorW(h,w,black,white):

    if S[h][w]=="#":

        black+=1

    else:

        white+=1

    return black,white





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



L=[]

for h in range(H):

    for w in range(W):

        black=0

        white=0

        if seen[h][w]==0:

            black,white=DFS(h,w,black,white,S[h][w])

            L.append([black,white])

ans=0

for b,w in L:

     ans+=b*w



print(ans)
from collections import deque



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

S=[]

for i in range(H):

    S.append(eval(input()))



q=deque([])

T=[]

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

step=[[0,1],[0,-1],[1,0],[-1,0]]

for h in range(H):

    for w in range(W):

        temp=[]

        if seen[h][w]==0:

            temp.append(S[h][w])

            seen[h][w]=1

            q.append([h,w,S[h][w]])

            while q:

                oh,ow,m=q.popleft()

                for dh,dw in step:

                    if 0<=oh+dh<H and 0<=ow+dw<W:

                        if seen[oh+dh][ow+dw]==0 and m!=S[oh+dh][ow+dw]:

                            temp.append(S[oh+dh][ow+dw])

                            seen[oh+dh][ow+dw]=1

                            q.append([oh+dh,ow+dw,S[oh+dh][ow+dw]])

            T.append(temp)



ans=0

for i in range(len(T)):

    black=0

    white=0

    for s in T[i]:

        if s=="#":

            black+=1

        else:

            white+=1

    ans+=black*white



print(ans)





import sys

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


def DFS(h, w, black, white, m):
    black, white = BorW(h, w, black, white)
    step = [[0, 1], [0, -1], [1, 0], [-1, 0]]
    seen[h][w] = 1
    for dh, dw in step:
        nh = h + dh
        nw = w + dw
        if 0 <= nh < H and 0 <= nw < W:
            if seen[nh][nw] == 0 and m != S[nh][nw]:
                seen[nh][nw] = 1
                black, white = DFS(nh, nw, black, white, S[nh][nw])
    return black, white


def BorW(h, w, black, white):
    if S[h][w] == "#":
        black += 1
    else:
        white += 1
    return black, white


seen = [[0] * W for _ in range(H)]
L = []
for h in range(H):
    for w in range(W):
        black = 0
        white = 0
        if seen[h][w] == 0:
            black, white = DFS(h, w, black, white, S[h][w])
            L.append([black, white])
ans = 0
for b, w in L:
    ans += b * w
print(ans)

from collections import deque

H, W = list(map(int, input().split()))
S = []
for i in range(H):
    S.append(eval(input()))
q = deque([])
T = []
seen = [[0] * W for _ in range(H)]
step = [[0, 1], [0, -1], [1, 0], [-1, 0]]
for h in range(H):
    for w in range(W):
        temp = []
        if seen[h][w] == 0:
            temp.append(S[h][w])
            seen[h][w] = 1
            q.append([h, w, S[h][w]])
            while q:
                oh, ow, m = q.popleft()
                for dh, dw in step:
                    if 0 <= oh + dh < H and 0 <= ow + dw < W:
                        if seen[oh + dh][ow + dw] == 0 and m != S[oh + dh][ow + dw]:
                            temp.append(S[oh + dh][ow + dw])
                            seen[oh + dh][ow + dw] = 1
                            q.append([oh + dh, ow + dw, S[oh + dh][ow + dw]])
            T.append(temp)
ans = 0
for i in range(len(T)):
    black = 0
    white = 0
    for s in T[i]:
        if s == "#":
            black += 1
        else:
            white += 1
    ans += black * white
print(ans)

-import sys +from collections import deque -sys.setrecursionlimit(1000000) - - -def DFS(h, w, black, white, m): -    black, white = BorW(h, w, black, white) -    step = [[0, 1], [0, -1], [1, 0], [-1, 0]] -    seen[h][w] = 1 -    for dh, dw in step: -        nh = h + dh -        nw = w + dw -        if 0 <= nh < H and 0 <= nw < W: -            if seen[nh][nw] == 0 and m != S[nh][nw]: -                seen[nh][nw] = 1 -                black, white = DFS(nh, nw, black, white, S[nh][nw]) -    return black, white - - -def BorW(h, w, black, white): -    if S[h][w] == "#": -        black += 1 -    else: -        white += 1 -    return black, white - - +q = deque([]) +T = [] -L = [] +step = [[0, 1], [0, -1], [1, 0], [-1, 0]] -        black = 0 -        white = 0 +        temp = [] -            black, white = DFS(h, w, black, white, S[h][w]) -            L.append([black, white]) +            temp.append(S[h][w]) +            seen[h][w] = 1 +            q.append([h, w, S[h][w]]) +            while q: +                oh, ow, m = q.popleft() +                for dh, dw in step: +                    if 0 <= oh + dh < H and 0 <= ow + dw < W: +                        if seen[oh + dh][ow + dw] == 0 and m != S[oh + dh][ow + dw]: +                            temp.append(S[oh + dh][ow + dw]) +                            seen[oh + dh][ow + dw] = 1 +                            q.append([oh + dh, ow + dw, S[oh + dh][ow + dw]]) +            T.append(temp) -for b, w in L: -    ans += b * w +for i in range(len(T)): +    black = 0 +    white = 0 +    for s in T[i]: +        if s == "#": +            black += 1 +        else: +            white += 1 +    ans += black * white