Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(2000)

def dfs(i,j):
    for m in move:
        ni = i + m[0]
        nj = j + m[1]
        if ni < 0 or ni >= H or nj < 0 or nj >= W:
            continue
        if ch[ni][nj]:
            continue
        if X[i][j] == X[ni][nj]:
            continue
            
        ch[ni][nj] = 1
        K[(ni+nj)%2] += 1
        dfs(ni, nj)
        
H, W = map(int, input().split())
X = [[[0, 1][a=="#"] for a in input()] for i in range(H)]
ch = [[0] * W for _ in range(H)]
    
move = [[0,1], [0,-1], [1,0], [-1,0]]

K = [0, 0]
ans = 0
for i in range(H):
    for j in range(W):
        if ch[i][j] > 0:
            continue
        ch[i][j] = 1
        K = [0, 0]
        K[(i+j)%2] = 1
        dfs(i, j)
        ans += K[0] * K[1]
        
print(ans)



Fixed submission:
H, W = map(int, input().split())

X = [[0] * W for i in range(H)]
G = [[0,0] for i in range(W*H)] 
L = [[-1] * W for i in range(H)] # Group ID

for i in range(H):
    s = input()
    for j in range(W):
        if (i+j) % 2 == 0:
            if s[j] == "#":
                X[i][j] = 1
        else:
            if s[j] != "#":
                X[i][j] = 1



g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        post.append([i0-1,j0])
                        L[i0-1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        post.append([i0+1,j0])
                        L[i0+1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        post.append([i0,j0-1])
                        L[i0][j0-1] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        post.append([i0,j0+1])
                        L[i0][j0+1] = g
                        G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)

Buggy python submission (verdict: Runtime Error):
def dfs(i,j):
    # print("dfs", i, j)
    for m in move:
        ni = i + m[0]
        nj = j + m[1]
        if ni < 0 or ni >= H or nj < 0 or nj >= W:
            continue
        if ch[ni][nj]:
            continue
        if X[i][j] == X[ni][nj]:
            continue
            
        ch[ni][nj] = 1
        K[(ni+nj)%2] += 1
        dfs(ni, nj)
        
H, W = map(int, input().split())
X = [[[0, 1][a=="#"] for a in input()] for i in range(H)]
ch = [[0] * W for _ in range(H)]
    
move = [[0,1], [0,-1], [1,0], [-1,0]]

K = [0, 0]
ans = 0
for i in range(H):
    for j in range(W):
        if ch[i][j] > 0:
            continue
        ch[i][j] = 1
        K = [0, 0]
        K[(i+j)%2] = 1
        dfs(i, j)
        ans += K[0] * K[1]
        # print(K)
print(ans)

Fixed submission:
H, W = map(int, input().split())

X = [[0] * W for i in range(H)]
G = [[0,0] for i in range(W*H)] 
L = [[-1] * W for i in range(H)] # Group ID

for i in range(H):
    s = input()
    for j in range(W):
        if (i+j) % 2 == 0:
            if s[j] == "#":
                X[i][j] = 1
        else:
            if s[j] != "#":
                X[i][j] = 1



g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        post.append([i0-1,j0])
                        L[i0-1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        post.append([i0+1,j0])
                        L[i0+1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        post.append([i0,j0-1])
                        L[i0][j0-1] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        post.append([i0,j0+1])
                        L[i0][j0+1] = g
                        G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)

Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
X = [[0] * W for _ in range(H)]
ch = [[0] * W for _ in range(H)]
for i in range(H):
    s = input()
    for j in range(W):
        if ((i + j)%2 == 0) ^ (s[j] == "#"):
            X[i][j] = 1
    
move = [[0,1], [0,-1], [1,0], [-1,0]]

def dfs(i,j):
    for m in move:
        ni = i+ m[0]
        nj = j + m[1]
        if ni < 0 or ni >= H or nj < 0 or nj >= W:
            continue
        if ch[ni][nj]:
            continue
        if X[i][j] != X[ni][nj]:
            continue
            
        ch[ni][nj] = 1
        K[(ni+nj)%2] += 1
        dfs(ni, nj)

K = [0, 0]
k = -1
ans = 0
for i in range(H):
    for j in range(W):
        if ch[i][j] > 0:
            continue
        ch[i][j] = 1
        K = [0, 0]
        K[(i+j)%2] = 1
        dfs(i, j)
        ans += K[0] * K[1]
print(ans)

Fixed submission:
H, W = map(int, input().split())

X = [[0] * W for i in range(H)]
G = [[0,0] for i in range(W*H)] 
L = [[-1] * W for i in range(H)] # Group ID

for i in range(H):
    s = input()
    for j in range(W):
        if (i+j) % 2 == 0:
            if s[j] == "#":
                X[i][j] = 1
        else:
            if s[j] != "#":
                X[i][j] = 1



g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        post.append([i0-1,j0])
                        L[i0-1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        post.append([i0+1,j0])
                        L[i0+1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        post.append([i0,j0-1])
                        L[i0][j0-1] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        post.append([i0,j0+1])
                        L[i0][j0+1] = g
                        G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)

Buggy python submission (verdict: Runtime Error):
if False:
    H, W = map(int, input().split())

    X = [[0] * W for i in range(H)]
    G = [[0,0] for i in range(W*H)] 
    L = [[-1] * W for i in range(H)] # Group ID

    for i in range(H):
        s = input()
        for j in range(W):
            if (i+j) % 2 == 0:
                if s[j] == "#":
                    X[i][j] = 1
            else:
                if s[j] != "#":
                    X[i][j] = 1
else:
    H, W = 400, 400

    X = [[0] * W for i in range(H)]
    G = [[0,0] for i in range(W*H)] 
    L = [[-1] * W for i in range(H)] # Group ID

    for i in range(H):
        if i%2 == 0:
            s = ("#.#." * W)[:W]
        else:
            s = (".#.#" * W)[:W]
        for j in range(W):
            if (i+j) % 2 == 0:
                if s[j] == "#":
                    X[i][j] = 1
            else:
                if s[j] != "#":
                    X[i][j] = 1


g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            # GGG = [[i,j]]
            nd = Node(i*400+j,1)
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        post.append([i0-1,j0])
                        L[i0-1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        post.append([i0+1,j0])
                        L[i0+1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        post.append([i0,j0-1])
                        L[i0][j0-1] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        post.append([i0,j0+1])
                        L[i0][j0+1] = g
                        G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)


Fixed submission:
H, W = map(int, input().split())

X = [[0] * W for i in range(H)]
G = [[0,0] for i in range(W*H)] 
L = [[-1] * W for i in range(H)] # Group ID

for i in range(H):
    s = input()
    for j in range(W):
        if (i+j) % 2 == 0:
            if s[j] == "#":
                X[i][j] = 1
        else:
            if s[j] != "#":
                X[i][j] = 1



g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        post.append([i0-1,j0])
                        L[i0-1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        post.append([i0+1,j0])
                        L[i0+1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        post.append([i0,j0-1])
                        L[i0][j0-1] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        post.append([i0,j0+1])
                        L[i0][j0+1] = g
                        G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

class Node:
    def __init__(self, key, value, parent=None, left=None, right=None):
        self.key = key
        self.value = value
        self.parent = parent
        self.left = left
        self.right = right
        self.balance = 0
        
    def disp_key(self):
        s = str(self.key)
        if self.left is None:
            pass
        else:
            s = str(self.left.key) + " " + s
            
        if self.right is None:
            pass
        else:
            s = s + " " + str(self.right.key)
        print(s)
        
    def disp(self):
        if self.left:
            self.left.disp()
        print(self.key, self.value)
        if self.right:
            self.right.disp()
        
    def disp_preorder(self):
        # print(self.key, self.value, self.left.key, self.left.value, self.right.key, self.right.value)
        lr = ""
        if self.left:
            lr += str(self.left.key)
        else:
            lr += "."
        lr += "-"
        if self.right:
            lr += str(self.right.key)
        else:
            lr += "."
            
        if self.parent:
            print(self.key, self.value, lr, self.parent.key)
        else:
            print(self.key, self.value, lr)
        if self.left:
            self.left.disp_preorder()
        if self.right:
            self.right.disp_preorder()
        
    def insert(self, key, value):
        # print(self.key, "<-", key)
        if self is None:
            self = Node(key, value)
        elif key == self.key:
            # return self
            self.value = value
            pass
        elif key < self.key:
            if self.left is None:
                self.left = Node(key, value, self)
            else:
                # self.left = self.left.insert(key, value)
                self.left.insert(key, value)
        else:
            if self.right is None:
                self.right = Node(key, value, self)
            else:
                # self.right = self.right.insert(key, value)
                self.right.insert(key, value)
        
        # self.disp()
        
    def set_value(self, key, value):
        tmp = self.search_node(key)
        if tmp:
            tmp.value = value
        else:
            self.insert(key, value)
        
    def modify_value(self, key, operator):
        tmp = self.search_node(key)
        if tmp:
            tmp.value = operator(tmp.value)
            return tmp.value
        else:
            return None
        
        
    def search(self, key):
        tmp = self.search_node(key)
        if tmp:
            return tmp.value
        else:
            return None
    
    def search_node(self, key):
        if key == self.key:
            return self
        elif key < self.key:
            if self.left:
                return self.left.search_node(key)
            else:
                return None
        else:
            if self.right:
                return self.right.search_node(key)
            else:
                return None
            
    def minimum(self):
        if self.left:
            return self.left.minimum()
        else:
            return (self.key, self.value)
    
    def maximum(self):
        if self.right:
            return self.right.maximum()
        else:
            return (self.key, self.value)
        
    def delete(self, key):
        if self.key == key:
            if (self.left is None) and (self.right is None):
                print("DELETE:", self.key, self.value)
                if self.parent:
                    if self.parent.left.key == self.key:
                        self.parent.left = None
                    elif self.parent.right.key == self.key:
                        self.parent.right = None
                # self.value = None
                # self = None
            elif self.left is None:
                if self.parent.left.key == self.key:
                    self.parent.left = self.right
                elif self.parent.right.key == self.key:
                    self.parent.right = self.right
            elif self.right is None:
                if self.parent.left.key == self.key:
                    self.parent.left = self.left
                elif self.parent.right.key == self.key:
                    self.parent.right = self.left
            else:
                tmp = self.right.minimum()
                self.key, self.value = tmp
                self.right.delete(tmp[0])
        elif key < self.key:
            if self.left:
                self.left.delete(key)
        else:
            if self.right:
                self.right.delete(key)
                
    def delete_more_than(self, key):
        if self.key == key:
            self.right = None
        elif key > self.key:
            self.right.delete_more_than(key)
        else:
            self.right = None
            self.delete(self.key)
            self.left.delete_more_than(key)
    
if False:
    H, W = map(int, input().split())

    X = [[0] * W for i in range(H)]
    G = [[0,0] for i in range(W*H)] 
    L = [[-1] * W for i in range(H)] # Group ID

    for i in range(H):
        s = input()
        for j in range(W):
            if (i+j) % 2 == 0:
                if s[j] == "#":
                    X[i][j] = 1
            else:
                if s[j] != "#":
                    X[i][j] = 1
else:
    H, W = 400, 400

    X = [[0] * W for i in range(H)]
    G = [[0,0] for i in range(W*H)] 
    L = [[-1] * W for i in range(H)] # Group ID

    for i in range(H):
        if i%2 == 0:
            s = ("#.#." * W)[:W]
        else:
            s = (".#.#" * W)[:W]
        for j in range(W):
            if (i+j) % 2 == 0:
                if s[j] == "#":
                    X[i][j] = 1
            else:
                if s[j] != "#":
                    X[i][j] = 1


g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            # GGG = [[i,j]]
            nd = Node(i*400+j,1)
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        # if [i0-1,j0] not in GGG:
                        if not nd.search((i0-1)*400+j0):
                            post.append([i0-1,j0])
                            # GGG.append([i0-1,j0])
                            nd.insert((i0-1)*400+j0, 1)
                            L[i0-1][j0] = g
                            G[g][(i0+j0+1)%2] += 1
                        else:
                            print("AAA")
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        # print([i0+1,j0], GGG)
                        # if [i0+1,j0] not in GGG:
                        if not nd.search((i0+1)*400+j0):
                            post.append([i0+1,j0])
                            # GGG.append([i0+1,j0])
                            nd.insert((i0+1)*400+j0, 1)
                            L[i0+1][j0] = g
                            G[g][(i0+j0+1)%2] += 1
                        else:
                            print("BBB")
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        # print([i0,j0-1], GGG)
                        # if [i0,j0-1] not in GGG:
                        if not nd.search((i0)*400+j0-1):
                            post.append([i0,j0-1])
                            # GGG.append([i0,j0-1])
                            nd.insert((i0)*400+j0-1, 1)
                            L[i0][j0-1] = g
                            G[g][(i0+j0+1)%2] += 1
                        else:
                            print("AAA")
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        # print([i0,j0+1], GGG)
                        # if [i0,j0+1] not in GGG:
                        if not nd.search((i0)*400+j0+1):
                            post.append([i0,j0+1])
                            # GGG.append([i0,j0+1])
                            nd.insert((i0)*400+j0+1, 1)
                            L[i0][j0+1] = g
                            G[g][(i0+j0+1)%2] += 1
                        else:
                            print("AAA")
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)


Fixed submission:
H, W = map(int, input().split())

X = [[0] * W for i in range(H)]
G = [[0,0] for i in range(W*H)] 
L = [[-1] * W for i in range(H)] # Group ID

for i in range(H):
    s = input()
    for j in range(W):
        if (i+j) % 2 == 0:
            if s[j] == "#":
                X[i][j] = 1
        else:
            if s[j] != "#":
                X[i][j] = 1



g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        post.append([i0-1,j0])
                        L[i0-1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        post.append([i0+1,j0])
                        L[i0+1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        post.append([i0,j0-1])
                        L[i0][j0-1] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        post.append([i0,j0+1])
                        L[i0][j0+1] = g
                        G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())

X = [[0] * W for i in range(H)]
G = [[0,0] for i in range(W*H)] 
L = [[-1] * W for i in range(H)] # Group ID

for i in range(H):
    s = input()
    for j in range(W):
        if (i+j) % 2 == 0:
            if s[j] == "#":
                X[i][j] = 1
        else:
            if s[j] != "#":
                X[i][j] = 1
            
g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            GGG = [[i,j]]
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        if [i0-1,j0] not in GGG:
                            post.append([i0-1,j0])
                            GGG.append([i0-1,j0])
                            L[i0-1][j0] = g
                            G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        if [i0+1,j0] not in GGG:
                            post.append([i0+1,j0])
                            GGG.append([i0+1,j0])
                            L[i0+1][j0] = g
                            G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        if [i0,j0-1] not in GGG:
                            post.append([i0,j0-1])
                            GGG.append([i0,j0-1])
                            L[i0][j0-1] = g
                            G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        if [i0,j0+1] not in GGG:
                            post.append([i0,j0+1])
                            GGG.append([i0,j0+1])
                            L[i0][j0+1] = g
                            G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)


Fixed submission:
H, W = map(int, input().split())

X = [[0] * W for i in range(H)]
G = [[0,0] for i in range(W*H)] 
L = [[-1] * W for i in range(H)] # Group ID

for i in range(H):
    s = input()
    for j in range(W):
        if (i+j) % 2 == 0:
            if s[j] == "#":
                X[i][j] = 1
        else:
            if s[j] != "#":
                X[i][j] = 1



g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        post.append([i0-1,j0])
                        L[i0-1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        post.append([i0+1,j0])
                        L[i0+1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        post.append([i0,j0-1])
                        L[i0][j0-1] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        post.append([i0,j0+1])
                        L[i0][j0+1] = g
                        G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())

X = [[0] * W for i in range(H)]
G = [[0,0] for i in range(W*H)] 
L = [[-1] * W for i in range(H)] # Group ID

for i in range(H):
    s = input()
    for j in range(W):
        if (i+j) % 2 == 0:
            if s[j] == "#":
                X[i][j] = 1
        else:
            if s[j] != "#":
                X[i][j] = 1
            
g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            GGG = [[i,j]]
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        if [i0-1,j0] not in GGG:
                            post.append([i0-1,j0])
                            GGG.append([i0-1,j0])
                            L[i0-1][j0] = g
                            G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        if [i0+1,j0] not in GGG:
                            post.append([i0+1,j0])
                            GGG.append([i0+1,j0])
                            L[i0+1][j0] = g
                            G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        if [i0,j0-1] not in GGG:
                            post.append([i0,j0-1])
                            GGG.append([i0,j0-1])
                            L[i0][j0-1] = g
                            G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        if [i0,j0+1] not in GGG:
                            post.append([i0,j0+1])
                            GGG.append([i0,j0+1])
                            L[i0][j0+1] = g
                            G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)


Fixed submission:
H, W = map(int, input().split())

X = [[0] * W for i in range(H)]
G = [[0,0] for i in range(W*H)] 
L = [[-1] * W for i in range(H)] # Group ID

for i in range(H):
    s = input()
    for j in range(W):
        if (i+j) % 2 == 0:
            if s[j] == "#":
                X[i][j] = 1
        else:
            if s[j] != "#":
                X[i][j] = 1



g = -1
for i in range(H):
    for j in range(W):
        if L[i][j] < 0:
            g += 1
            L[i][j] = g
            post = [[i,j]]
            G[g][(i+j)%2] += 1
            while post:
                pre = post
                post = []
                for ggg in pre:
                    i0, j0 = ggg
                    if i0>0 and L[i0-1][j0] < 0 and X[i0][j0] == X[i0-1][j0]:
                        post.append([i0-1,j0])
                        L[i0-1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if i0<H-1 and L[i0+1][j0] < 0 and X[i0][j0] == X[i0+1][j0]:
                        post.append([i0+1,j0])
                        L[i0+1][j0] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0>0 and L[i0][j0-1] < 0 and X[i0][j0] == X[i0][j0-1]:
                        post.append([i0,j0-1])
                        L[i0][j0-1] = g
                        G[g][(i0+j0+1)%2] += 1
                    if j0<W-1 and L[i0][j0+1] < 0 and X[i0][j0] == X[i0][j0+1]:
                        post.append([i0,j0+1])
                        L[i0][j0+1] = g
                        G[g][(i0+j0+1)%2] += 1
                        
            # print(GGG)
# print(G)
ans = 0
for t in G:
    aa, bb = t
    ans += aa * bb
    
print(ans)