import queue



h,w = list(map(int,input().split()))

si = [eval(input()) for i in range(h)]



q = queue.Queue()



check = [-1]*(h*w)



vx = [0,1,0,-1]

vy = [1,0,-1,0]



for i in range(h*w):

    y = i//w

    x = i%w

    if si[y][x] == '#':

        if check[i] != -1:

            continue

        q.put([i,'#'])

        check[i] = i

        

        while not q.empty():

            num, clr = q.get()

            y = num//w

            x = num%w

            

            for j in range(4):

                ny = y + vy[j]

                nx = x + vx[j]

                if ny >= 0 and ny < h and nx >= 0 and nx < w:

                    new_num = ny*w+nx

                    if check[new_num] != -1:

                        continue

                    #print(new_num)

                    if clr == '#':

                        if si[ny][nx] == '.':

                            q.put([new_num,'.'])

                            check[new_num] = check[num]

                    else:

                        if si[ny][nx] == '#':

                            q.put([new_num,'#'])

                            check[new_num] = check[num]

    

#print(check)



ans = 0



li = [0]*(h*w+1)



for i in range(h):

    for j in range(w):

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

            li[check[i*w+j]] += 1



#print(li)



for i in range(h):

    for j in range(w):

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

            ans += li[check[i*w+j]]

            

print(ans)
import sys



sys.setrecursionlimit(10**7)



h,w = list(map(int,input().split()))

si = [eval(input()) for i in range(h)]



check = [-1]*(h*w)



#print(check,check==-1)

#exit()



vx = [0,1,0,-1]

vy = [1,0,-1,0]



def dfs(num,clr):

    #print(min(check))

    #if min(check) > -1:

    #    return

    

    y = num//w

    x = num%w

    for j in range(4):

        ny = y + vy[j]

        nx = x + vx[j]

        if ny >= 0 and ny < h and nx >= 0 and nx < w:

            new_num = ny*w+nx

            if check[new_num] != -1:

                continue

            #print(new_num,num,clr,si[ny][nx])

            if clr == '#':

                #print(ny,nx)

                if si[ny][nx] == '.':

                    check[new_num] = check[num]

                    dfs(new_num,'.')

            else:

                    

                if si[ny][nx] == '#':

                    check[new_num] = check[num]

                    dfs(new_num,'#')

            #print(check)



for i in range(h*w):

    #print(i)

    y = i//w

    x = i%w

    if si[y][x] == '#':

        if check[i] != -1:

            continue

        check[i] = i

        dfs(i,'#')

        #print(check)

        #exit()



ans = 0



li = [0]*(h*w+1)



for i in range(h):

    for j in range(w):

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

            li[check[i*w+j]] += 1



#print(li)



for i in range(h):

    for j in range(w):

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

            ans += li[check[i*w+j]]

            

print(ans)
import queue

h, w = list(map(int, input().split()))
si = [eval(input()) for i in range(h)]
q = queue.Queue()
check = [-1] * (h * w)
vx = [0, 1, 0, -1]
vy = [1, 0, -1, 0]
for i in range(h * w):
    y = i // w
    x = i % w
    if si[y][x] == "#":
        if check[i] != -1:
            continue
        q.put([i, "#"])
        check[i] = i
        while not q.empty():
            num, clr = q.get()
            y = num // w
            x = num % w
            for j in range(4):
                ny = y + vy[j]
                nx = x + vx[j]
                if ny >= 0 and ny < h and nx >= 0 and nx < w:
                    new_num = ny * w + nx
                    if check[new_num] != -1:
                        continue
                    # print(new_num)
                    if clr == "#":
                        if si[ny][nx] == ".":
                            q.put([new_num, "."])
                            check[new_num] = check[num]
                    else:
                        if si[ny][nx] == "#":
                            q.put([new_num, "#"])
                            check[new_num] = check[num]
# print(check)
ans = 0
li = [0] * (h * w + 1)
for i in range(h):
    for j in range(w):
        if si[i][j] == ".":
            li[check[i * w + j]] += 1
# print(li)
for i in range(h):
    for j in range(w):
        if si[i][j] == "#":
            ans += li[check[i * w + j]]
print(ans)

import sys

sys.setrecursionlimit(10**7)
h, w = list(map(int, input().split()))
si = [eval(input()) for i in range(h)]
check = [-1] * (h * w)
# print(check,check==-1)
# exit()
vx = [0, 1, 0, -1]
vy = [1, 0, -1, 0]


def dfs(num, clr):
    # print(min(check))
    # if min(check) > -1:
    #    return
    y = num // w
    x = num % w
    for j in range(4):
        ny = y + vy[j]
        nx = x + vx[j]
        if ny >= 0 and ny < h and nx >= 0 and nx < w:
            new_num = ny * w + nx
            if check[new_num] != -1:
                continue
            # print(new_num,num,clr,si[ny][nx])
            if clr == "#":
                # print(ny,nx)
                if si[ny][nx] == ".":
                    check[new_num] = check[num]
                    dfs(new_num, ".")
            else:
                if si[ny][nx] == "#":
                    check[new_num] = check[num]
                    dfs(new_num, "#")
            # print(check)


for i in range(h * w):
    # print(i)
    y = i // w
    x = i % w
    if si[y][x] == "#":
        if check[i] != -1:
            continue
        check[i] = i
        dfs(i, "#")
        # print(check)
        # exit()
ans = 0
li = [0] * (h * w + 1)
for i in range(h):
    for j in range(w):
        if si[i][j] == ".":
            li[check[i * w + j]] += 1
# print(li)
for i in range(h):
    for j in range(w):
        if si[i][j] == "#":
            ans += li[check[i * w + j]]
print(ans)

-import queue +import sys +sys.setrecursionlimit(10**7) -q = queue.Queue() +# print(check,check==-1) +# exit() + + +def dfs(num, clr): +    # print(min(check)) +    # if min(check) > -1: +    #    return +    y = num // w +    x = num % w +    for j in range(4): +        ny = y + vy[j] +        nx = x + vx[j] +        if ny >= 0 and ny < h and nx >= 0 and nx < w: +            new_num = ny * w + nx +            if check[new_num] != -1: +                continue +            # print(new_num,num,clr,si[ny][nx]) +            if clr == "#": +                # print(ny,nx) +                if si[ny][nx] == ".": +                    check[new_num] = check[num] +                    dfs(new_num, ".") +            else: +                if si[ny][nx] == "#": +                    check[new_num] = check[num] +                    dfs(new_num, "#") +            # print(check) + + +    # print(i) -        q.put([i, "#"]) -        while not q.empty(): -            num, clr = q.get() -            y = num // w -            x = num % w -            for j in range(4): -                ny = y + vy[j] -                nx = x + vx[j] -                if ny >= 0 and ny < h and nx >= 0 and nx < w: -                    new_num = ny * w + nx -                    if check[new_num] != -1: -                        continue -                    # print(new_num) -                    if clr == "#": -                        if si[ny][nx] == ".": -                            q.put([new_num, "."]) -                            check[new_num] = check[num] -                    else: -                        if si[ny][nx] == "#": -                            q.put([new_num, "#"]) -                            check[new_num] = check[num] -# print(check) +        dfs(i, "#") +        # print(check) +        # exit()