Buggy python submission (verdict: Time Limit Exceeded):
def c_alternating_path(H, W, Grid):
    from collections import deque
    ans = 0
    already_checked = [[False] * W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if already_checked[h][w]:
                continue
            if Grid[h][w] == '#':
                reached_black = 0
                tmp = 0
                is_visit = [[False] * W for _ in range(H)]
                q = deque([(h, w)])
                while q:
                    row, col = q.pop()
                    if is_visit[row][col]:
                        continue
                    if (not already_checked[row][col]) and Grid[row][col] == '#':
                        already_checked[row][col] = True
                        reached_black += 1
                    is_visit[row][col] = True
                    if Grid[row][col] == '.':
                        tmp += 1
                    for dr, dc in zip((1, 0, -1, 0), (0, 1, 0, -1)):
                        nr, nc = row + dr, col + dc
                        if (0 <= nr < H and 0 <= nc < W
                                and Grid[row][col] != Grid[nr][nc]
                                and not is_visit[nr][nc]):
                            q.appendleft((nr, nc))
                ans += tmp * reached_black
    return ans

H, W = [int(i) for i in input().split()]
Grid = [input() for _ in range(H)]
print(c_alternating_path(H, W, Grid))

Fixed submission:
def c_alternating_path(H, W, Grid):
    from itertools import product
    from collections import deque

    ans = 0
    is_visited = [[False] * W for _ in range(H)]
    for h, w in product(range(H), range(W)):
        if Grid[h][w] == '#' and not is_visited[h][w]:
            count_white, count_black = 0, 0
            q = deque([(h, w)])
            while q:
                row, col = q.popleft()
                if is_visited[row][col]:
                    continue

                is_visited[row][col] = True
                if Grid[row][col] == '#':
                    count_black += 1
                else:
                    count_white += 1

                for dr, dc in [(0, -1), (1, 0), (0, 1), (-1, 0)]:
                    nr, nc = row + dr, col + dc
                    if (0 <= nr < H and 0 <= nc < W
                        and Grid[row][col] != Grid[nr][nc]
                            and not is_visited[nr][nc]):
                        q.append((nr, nc))
            ans += count_black * count_white
    return ans

H, W = [int(i) for i in input().split()]
Grid = [input() for _ in range(H)]
print(c_alternating_path(H, W, Grid))

Buggy python submission (verdict: Time Limit Exceeded):
def c_alternating_path(H, W, Grid):
    from collections import deque
    ans = 0
    for h in range(H):
        for w in range(W):
            if Grid[h][w] == '#':
                is_visit = [[False] * W for _ in range(H)]
                q = deque([(h, w)])
                while q:
                    row, col = q.pop()
                    if is_visit[row][col]:
                        continue
                    is_visit[row][col] = True
                    if Grid[row][col] == '.':
                        ans += 1
                    for dr, dc in zip((1, 0, -1, 0), (0, 1, 0, -1)):
                        nr, nc = row + dr, col + dc
                        if (0 <= nr < H and 0 <= nc < W
                                and Grid[row][col] != Grid[nr][nc]
                                and not is_visit[nr][nc]):
                            q.appendleft((nr, nc))
    return ans

H, W = [int(i) for i in input().split()]
Grid = [input() for _ in range(H)]
print(c_alternating_path(H, W, Grid))

Fixed submission:
def c_alternating_path(H, W, Grid):
    from itertools import product
    from collections import deque

    ans = 0
    is_visited = [[False] * W for _ in range(H)]
    for h, w in product(range(H), range(W)):
        if Grid[h][w] == '#' and not is_visited[h][w]:
            count_white, count_black = 0, 0
            q = deque([(h, w)])
            while q:
                row, col = q.popleft()
                if is_visited[row][col]:
                    continue

                is_visited[row][col] = True
                if Grid[row][col] == '#':
                    count_black += 1
                else:
                    count_white += 1

                for dr, dc in [(0, -1), (1, 0), (0, 1), (-1, 0)]:
                    nr, nc = row + dr, col + dc
                    if (0 <= nr < H and 0 <= nc < W
                        and Grid[row][col] != Grid[nr][nc]
                            and not is_visited[nr][nc]):
                        q.append((nr, nc))
            ans += count_black * count_white
    return ans

H, W = [int(i) for i in input().split()]
Grid = [input() for _ in range(H)]
print(c_alternating_path(H, W, Grid))

Buggy python submission (verdict: Time Limit Exceeded):
def c_alternating_path(H, W, Grid):
    from collections import deque
    ans = 0
    for h in range(H):
        for w in range(W):
            if Grid[h][w] == '#':
                is_visit = [[False] * W for _ in range(H)]
                q = deque([(h, w)])
                while q:
                    row, col = q.pop()
                    if is_visit[row][col]:
                        continue
                    is_visit[row][col] = True
                    if Grid[row][col] == '.':
                        ans += 1
                    for dr, dc in zip((1, 0, -1, 0), (0, 1, 0, -1)):
                        nr, nc = row + dr, col + dc
                        if (0 <= nr < H and 0 <= nc < W
                                and Grid[row][col] != Grid[nr][nc]
                                and not is_visit[nr][nc]):
                            q.appendleft((nr, nc))
    return ans

H, W = [int(i) for i in input().split()]
Grid = [input() for _ in range(H)]
print(c_alternating_path(H, W, Grid))

Fixed submission:
def c_alternating_path(H, W, Grid):
    from itertools import product
    from collections import deque

    ans = 0
    is_visited = [[False] * W for _ in range(H)]
    for h, w in product(range(H), range(W)):
        if Grid[h][w] == '#' and not is_visited[h][w]:
            count_white, count_black = 0, 0
            q = deque([(h, w)])
            while q:
                row, col = q.popleft()
                if is_visited[row][col]:
                    continue

                is_visited[row][col] = True
                if Grid[row][col] == '#':
                    count_black += 1
                else:
                    count_white += 1

                for dr, dc in [(0, -1), (1, 0), (0, 1), (-1, 0)]:
                    nr, nc = row + dr, col + dc
                    if (0 <= nr < H and 0 <= nc < W
                        and Grid[row][col] != Grid[nr][nc]
                            and not is_visited[nr][nc]):
                        q.append((nr, nc))
            ans += count_black * count_white
    return ans

H, W = [int(i) for i in input().split()]
Grid = [input() for _ in range(H)]
print(c_alternating_path(H, W, Grid))