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

s = [eval(input()) for _ in range(H)]





def dfs(startx: int, starty: int) -> set:

    dxdy = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    stack = [(startx, starty, s[starty][startx] == '#')]

    reached = set()

    reached.add((startx, starty))

    b, w = 0, 0

    while stack:

        d = stack.pop()

        if d[2]:

            b += 1

        else:

            w += 1

        for a in dxdy:

            x = d[0] + a[0]

            y = d[1] + a[1]

            if (x, y) not in reached and 0 <= x < W and 0 <= y < H and d[2] ^ (s[y][x] == '#'):

                stack.append((x, y, s[y][x] == '#'))

                reached.add((x, y))

    return (reached, b, w)





reach = set()

ans = 0

for i in range(H):

    for j in range(W):

        if (j, i) not in reach:

            res = dfs(j, i)

            reach |= res[0]

            ans += res[1] * res[2]

print(ans)

dxdy = [(0, 1), (0, -1), (1, 0), (-1, 0)]

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

s = [list(eval(input())) for _ in range(h)]

for i in range(h):

    for j in range(w):

        if (i + j) % 2:

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

                s[i][j] = '#'

            else:

                s[i][j] = '.'

d = [[True] * w for _ in range(h)]

ans = 0

for i in range(h):

    for j in range(w):

        if not d[i][j]:

            continue

        d[i][j] = False

        st = [(i, j)]

        b, wh = 0, 0

        while st:

            y, x = st.pop()

            if (x + y) % 2:

                b += 1

            else:

                wh += 1

            for dx, dy in dxdy:

                if 0 <= x + dx < w and 0 <= y + dy < h and d[y + dy][x + dx] and s[y][x] == s[y + dy][x + dx]:

                    st.append((y + dy, x + dx))

                    d[y + dy][x + dx] = False

        ans += b * wh

print(ans)

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


def dfs(startx: int, starty: int) -> set:
    dxdy = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    stack = [(startx, starty, s[starty][startx] == "#")]
    reached = set()
    reached.add((startx, starty))
    b, w = 0, 0
    while stack:
        d = stack.pop()
        if d[2]:
            b += 1
        else:
            w += 1
        for a in dxdy:
            x = d[0] + a[0]
            y = d[1] + a[1]
            if (
                (x, y) not in reached
                and 0 <= x < W
                and 0 <= y < H
                and d[2] ^ (s[y][x] == "#")
            ):
                stack.append((x, y, s[y][x] == "#"))
                reached.add((x, y))
    return (reached, b, w)


reach = set()
ans = 0
for i in range(H):
    for j in range(W):
        if (j, i) not in reach:
            res = dfs(j, i)
            reach |= res[0]
            ans += res[1] * res[2]
print(ans)

dxdy = [(0, 1), (0, -1), (1, 0), (-1, 0)]
h, w = list(map(int, input().split()))
s = [list(eval(input())) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if (i + j) % 2:
            if s[i][j] == ".":
                s[i][j] = "#"
            else:
                s[i][j] = "."
d = [[True] * w for _ in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if not d[i][j]:
            continue
        d[i][j] = False
        st = [(i, j)]
        b, wh = 0, 0
        while st:
            y, x = st.pop()
            if (x + y) % 2:
                b += 1
            else:
                wh += 1
            for dx, dy in dxdy:
                if (
                    0 <= x + dx < w
                    and 0 <= y + dy < h
                    and d[y + dy][x + dx]
                    and s[y][x] == s[y + dy][x + dx]
                ):
                    st.append((y + dy, x + dx))
                    d[y + dy][x + dx] = False
        ans += b * wh
print(ans)

-H, W = list(map(int, input().split())) -s = [eval(input()) for _ in range(H)] - - -def dfs(startx: int, starty: int) -> set: -    dxdy = [[1, 0], [-1, 0], [0, 1], [0, -1]] -    stack = [(startx, starty, s[starty][startx] == "#")] -    reached = set() -    reached.add((startx, starty)) -    b, w = 0, 0 -    while stack: -        d = stack.pop() -        if d[2]: -            b += 1 -        else: -            w += 1 -        for a in dxdy: -            x = d[0] + a[0] -            y = d[1] + a[1] -            if ( -                (x, y) not in reached -                and 0 <= x < W -                and 0 <= y < H -                and d[2] ^ (s[y][x] == "#") -            ): -                stack.append((x, y, s[y][x] == "#")) -                reached.add((x, y)) -    return (reached, b, w) - - -reach = set() +dxdy = [(0, 1), (0, -1), (1, 0), (-1, 0)] +h, w = list(map(int, input().split())) +s = [list(eval(input())) for _ in range(h)] +for i in range(h): +    for j in range(w): +        if (i + j) % 2: +            if s[i][j] == ".": +                s[i][j] = "#" +            else: +                s[i][j] = "." +d = [[True] * w for _ in range(h)] -for i in range(H): -    for j in range(W): -        if (j, i) not in reach: -            res = dfs(j, i) -            reach |= res[0] -            ans += res[1] * res[2] +for i in range(h): +    for j in range(w): +        if not d[i][j]: +            continue +        d[i][j] = False +        st = [(i, j)] +        b, wh = 0, 0 +        while st: +            y, x = st.pop() +            if (x + y) % 2: +                b += 1 +            else: +                wh += 1 +            for dx, dy in dxdy: +                if ( +                    0 <= x + dx < w +                    and 0 <= y + dy < h +                    and d[y + dy][x + dx] +                    and s[y][x] == s[y + dy][x + dx] +                ): +                    st.append((y + dy, x + dx)) +                    d[y + dy][x + dx] = False +        ans += b * wh