Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
blacklist = []
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            blacklist.append((h,w))
count = 0
alr = [[0 for i in range(W)] for j in range(H)]
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h,w in blacklist:
    que = deque()
    que.append((h,w,True))
    alr[h][w] = 1
    alr2 = [(h,w)]
    while que:
        hn,wn,black = que.popleft()
        black = not black
        for i in range(4):
            if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and (hn+h1[i],wn+w1[i]) not in alr2:
                if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                    if not alr[hn+h1[i]][wn+w1[i]]:
                        count += 1
                    que.append((hn+h1[i],wn+w1[i],black))
                    alr2.append((hn+h1[i],wn+w1[i]))
                elif black and S[hn+h1[i]][wn+w1[i]] == '#':
                    que.append((hn+h1[i],wn+w1[i],black))
                    alr2.append((hn+h1[i],wn+w1[i]))
print(count)
                        

Fixed submission:
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
blacklist = []
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            blacklist.append((h,w))
ans = 0
alr = [[0 for i in range(W)] for j in range(H)]
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h,w in blacklist:
    if alr[h][w] == 0:
        bcount = 1
        wcount = 0
        que = deque()
        que.append((h,w,True))
        alr[h][w] = 1
        while que:
            hn,wn,black = que.popleft()
            black = not black
            for i in range(4):
                if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and not alr[hn+h1[i]][wn+w1[i]]:
                    if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                        wcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
                    elif black and S[hn+h1[i]][wn+w1[i]] == '#':
                        bcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
        ans += wcount*bcount
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
blacklist = []
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            blacklist.append((h,w))
count = 0
alr = [[0 for i in range(W)] for j in range(H)]
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h,w in blacklist:
    que = deque()
    que.append((h,w,True))
    alr[h][w] = 1
    alr2 = [(h,w)]
    while que:
        hn,wn,black = que.popleft()
        black = not black
        for i in range(4):
            if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and (hn+h1[i],wn+w1[i]) not in alr2:
                if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                    if not alr[hn+h1[i]][wn+w1[i]]:
                        count += 1
                    que.append((hn+h1[i],wn+w1[i],black))
                    alr2.append((hn+h1[i],wn+w1[i]))
                elif black and S[hn+h1[i]][wn+w1[i]] == '#':
                    que.append((hn+h1[i],wn+w1[i],black))
                    alr2.append((hn+h1[i],wn+w1[i]))
print(count)
                        

Fixed submission:
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
blacklist = []
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            blacklist.append((h,w))
ans = 0
alr = [[0 for i in range(W)] for j in range(H)]
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h,w in blacklist:
    if alr[h][w] == 0:
        bcount = 1
        wcount = 0
        que = deque()
        que.append((h,w,True))
        alr[h][w] = 1
        while que:
            hn,wn,black = que.popleft()
            black = not black
            for i in range(4):
                if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and not alr[hn+h1[i]][wn+w1[i]]:
                    if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                        wcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
                    elif black and S[hn+h1[i]][wn+w1[i]] == '#':
                        bcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
        ans += wcount*bcount
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
blacklist = []
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            blacklist.append((h,w))
count = 0
alr = []
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h,w in blacklist:
    que = deque()
    que.append((h,w,True))
    alr.append((h,w))
    alr2 = [(h,w)]
    while que:
        hn,wn,black = que.popleft()
        black = not black
        for i in range(4):
            if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and (hn+h1[i],wn+w1[i]) not in alr2:
                if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                    if (hn+h1[i],wn+w1[i]) not in alr:
                        count += 1
                    que.append((hn+h1[i],wn+w1[i],black))
                    alr2.append((hn+h1[i],wn+w1[i]))
                elif black and S[hn+h1[i]][wn+w1[i]] == '#':
                    que.append((hn+h1[i],wn+w1[i],black))
                    alr2.append((hn+h1[i],wn+w1[i]))
print(count)
                        

Fixed submission:
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
blacklist = []
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            blacklist.append((h,w))
ans = 0
alr = [[0 for i in range(W)] for j in range(H)]
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h,w in blacklist:
    if alr[h][w] == 0:
        bcount = 1
        wcount = 0
        que = deque()
        que.append((h,w,True))
        alr[h][w] = 1
        while que:
            hn,wn,black = que.popleft()
            black = not black
            for i in range(4):
                if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and not alr[hn+h1[i]][wn+w1[i]]:
                    if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                        wcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
                    elif black and S[hn+h1[i]][wn+w1[i]] == '#':
                        bcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
        ans += wcount*bcount
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
count = 0
alr = []
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#' and (h,w) not in alr:
            que = deque()
            que.append((h,w,True))
            alr.append((h,w))
            alr2 = [(h,w)]
            while que:
                hn,wn,black = que.popleft()
                black = not black
                for i in range(4):
                    if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and (hn+h1[i],wn+w1[i]) not in alr2:
                        if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                            if (hn+h1[i],wn+w1[i]) not in alr:
                                count += 1
                            que.append((hn+h1[i],wn+w1[i],black))
                            alr2.append((hn+h1[i],wn+w1[i]))
                        elif black and S[hn+h1[i]][wn+w1[i]] == '#':

                            que.append((hn+h1[i],wn+w1[i],black))
                            alr2.append((hn+h1[i],wn+w1[i]))
print(count)
                        

Fixed submission:
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
blacklist = []
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            blacklist.append((h,w))
ans = 0
alr = [[0 for i in range(W)] for j in range(H)]
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h,w in blacklist:
    if alr[h][w] == 0:
        bcount = 1
        wcount = 0
        que = deque()
        que.append((h,w,True))
        alr[h][w] = 1
        while que:
            hn,wn,black = que.popleft()
            black = not black
            for i in range(4):
                if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and not alr[hn+h1[i]][wn+w1[i]]:
                    if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                        wcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
                    elif black and S[hn+h1[i]][wn+w1[i]] == '#':
                        bcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
        ans += wcount*bcount
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
count = 0
alr = []
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#' and (h,w) not in alr:
            que = deque()
            que.append((h,w,True))
            alr.append((h,w))
            alr2 = [(h,w)]
            while que:
                hn,wn,black = que.popleft()
                black = not black
                for i in range(4):
                    if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and (hn+h1[i],wn+w1[i]) not in alr2:
                        if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                            if (hn+h1[i],wn+w1[i]) not in alr:
                                count += 1
                            que.append((hn+h1[i],wn+w1[i],black))
                            alr2.append((hn+h1[i],wn+w1[i]))
                        elif black and S[hn+h1[i]][wn+w1[i]] == '#':

                            que.append((hn+h1[i],wn+w1[i],black))
                            alr2.append((hn+h1[i],wn+w1[i]))
print(count)
                        

Fixed submission:
from collections import deque
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
blacklist = []
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            blacklist.append((h,w))
ans = 0
alr = [[0 for i in range(W)] for j in range(H)]
h1 = [0,0,1,-1]
w1 = [1,-1,0,0]
for h,w in blacklist:
    if alr[h][w] == 0:
        bcount = 1
        wcount = 0
        que = deque()
        que.append((h,w,True))
        alr[h][w] = 1
        while que:
            hn,wn,black = que.popleft()
            black = not black
            for i in range(4):
                if 0 <= hn+h1[i] <= H-1 and 0 <= wn+w1[i] <= W-1 and not alr[hn+h1[i]][wn+w1[i]]:
                    if (not black) and S[hn+h1[i]][wn+w1[i]] == '.':
                        wcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
                    elif black and S[hn+h1[i]][wn+w1[i]] == '#':
                        bcount += 1
                        que.append((hn+h1[i],wn+w1[i],black))
                        alr[hn+h1[i]][wn+w1[i]] = 1
        ans += wcount*bcount
print(ans)