Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    S.append(s)

def DFS(y,x,S):
    m=[[0]*W for _ in range(H)]
    d=deque([])
    d.append((y,x,0))
    ans=0
    while d:
        p,q,n=d.popleft()
        if m[p][q]==1:
            continue
        else:
            if n%2==0 and S[p][q]=='#':
    
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='.':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='.':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='.':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='.':
                    d.append((p,q-1,n+1))
            
            elif n%2==1 and S[p][q]=='.':
                ans+=1
                
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='#':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='#':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='#':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='#':
                    d.append((p,q-1,n+1))
    return ans

total=0
for i in range(H):
    for j in range(W):
        total+=DFS(i,j,S)

print(total)
                

Fixed submission:
from collections import deque
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    S.append(s)
m=[[0]*W for _ in range(H)]
def DFS(y,x,S):

    d=deque([])
    d.append((y,x,0))
    white=0
    black=0
    while d:
        p,q,n=d.popleft()
        if m[p][q]==1:
            continue
        else:
            if n%2==0 and S[p][q]=='#':
                black+=1
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='.':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='.':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='.':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='.':
                    d.append((p,q-1,n+1))
            
            elif n%2==1 and S[p][q]=='.':
                white+=1
                
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='#':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='#':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='#':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='#':
                    d.append((p,q-1,n+1))
    return white,black

total=0
for i in range(H):
    for j in range(W):
        if S[i][j]=='.':
            continue
        elif m[i][j]==1:
            continue
        else:
            w,b=DFS(i,j,S)
            total+=w*b

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    S.append(s)

def DFS(y,x,S):
    m=[[0]*W for _ in range(H)]
    d=deque([])
    d.append((y,x,0))
    ans=0
    while d:
        p,q,n=d.pop()
        if m[p][q]==1:
            continue
        else:
            if n%2==0 and S[p][q]=='#':
    
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='.':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='.':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='.':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='.':
                    d.append((p,q-1,n+1))
            
            elif n%2==1 and S[p][q]=='.':
                ans+=1
                
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='#':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='#':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='#':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='#':
                    d.append((p,q-1,n+1))
    return ans

total=0
for i in range(H):
    for j in range(W):
        total+=DFS(i,j,S)

print(total)

Fixed submission:
from collections import deque
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    S.append(s)
m=[[0]*W for _ in range(H)]
def DFS(y,x,S):

    d=deque([])
    d.append((y,x,0))
    white=0
    black=0
    while d:
        p,q,n=d.popleft()
        if m[p][q]==1:
            continue
        else:
            if n%2==0 and S[p][q]=='#':
                black+=1
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='.':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='.':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='.':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='.':
                    d.append((p,q-1,n+1))
            
            elif n%2==1 and S[p][q]=='.':
                white+=1
                
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='#':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='#':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='#':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='#':
                    d.append((p,q-1,n+1))
    return white,black

total=0
for i in range(H):
    for j in range(W):
        if S[i][j]=='.':
            continue
        elif m[i][j]==1:
            continue
        else:
            w,b=DFS(i,j,S)
            total+=w*b

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    S.append(s)

def DFS(y,x,S):
    m=[[0]*W for _ in range(H)]
    d=deque([])
    d.append((y,x,0))
    ans=0
    while d:
        p,q,n=d.pop()
        if m[p][q]==1:
            continue
        else:
            if n%2==0 and S[p][q]=='#':
    
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='.':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='.':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='.':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='.':
                    d.append((p,q-1,n+1))
            
            elif n%2==1 and S[p][q]=='.':
                ans+=1
                
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='#':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='#':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='#':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='#':
                    d.append((p,q-1,n+1))
    return ans

total=0
for i in range(H):
    for j in range(W):
        total+=DFS(i,j,S)

print(total)

Fixed submission:
from collections import deque
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    S.append(s)
m=[[0]*W for _ in range(H)]
def DFS(y,x,S):

    d=deque([])
    d.append((y,x,0))
    white=0
    black=0
    while d:
        p,q,n=d.popleft()
        if m[p][q]==1:
            continue
        else:
            if n%2==0 and S[p][q]=='#':
                black+=1
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='.':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='.':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='.':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='.':
                    d.append((p,q-1,n+1))
            
            elif n%2==1 and S[p][q]=='.':
                white+=1
                
                m[p][q]=1
                if 0<=p+1<=H-1 and S[p+1][q]=='#':
                    d.append((p+1,q,n+1))
                if 0<=p-1<=H-1 and S[p-1][q]=='#':
                    d.append((p-1,q,n+1))
                if 0<=q+1<=W-1 and S[p][q+1]=='#':
                    d.append((p,q+1,n+1))
                if 0<=q-1<=W-1 and S[p][q-1]=='#':
                    d.append((p,q-1,n+1))
    return white,black

total=0
for i in range(H):
    for j in range(W):
        if S[i][j]=='.':
            continue
        elif m[i][j]==1:
            continue
        else:
            w,b=DFS(i,j,S)
            total+=w*b

print(total)