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

L=[]
l=["*" for i in range(W+2)]
L.append(l)
for i in range(H):
    s=input()
    s="*"+s+"*"
    s=list(s)
    L.append(s)
L.append(l)

#print(L)
Li=[]
for i in range(1,H+1):
    for j in range(1,W+1):
        if L[i][j]=="#":
            Li.append([i,j])
#print(Li)
x=[1,-1,0,0]
y=[0,0,1,-1]
ans=0
for i in range(len(Li)):
    A=[[0 for i in range(W+2)]for j in range(H+2)]
    Arr=deque()
    Arr.append([Li[i][0],Li[i][1],0])
    #print(Arr)
    A[Arr[0][0]][Arr[0][1]]=1
    for j in range(W*H*2):
        if len(Arr)==0:
            break
        else:
            #print(Arr,ans)
            
            if (Arr[0][2])%2==0:
                for k in range(4):
                    if L[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=="." and A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]==0:
                        Arr.append([Arr[0][0]+x[k],Arr[0][1]+y[k],Arr[0][2]+1])
                        A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=1
                        ans+=1
                del Arr[0]
            else:
                for k in range(4):
                    if L[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=="#" and A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]==0:
                        Arr.append([Arr[0][0]+x[k],Arr[0][1]+y[k],Arr[0][2]+1])
                        A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=1
                del Arr[0]
print(ans)

Fixed submission:
H,W=map(int,input().split())
L=[list(input())for i in range(H)]
#print(L)

P=[i for i in range(H*W)]
size=[[0,0]for i in range(H*W)]

for i in range(H):
    for j in range(W):
        if L[i][j]==".":
            size[i*W+j][0]+=1
        else:
            size[i*W+j][1]+=1
#print(size)

def find(z):
    if  z==P[z]:
        return z
    else:
        return find(P[z])
def union(x,y):
    X=find(x)
    Y=find(y)
    if sum(size[X])>=sum(size[Y]):
        P[Y]=P[X]
        size[X][0]+=size[Y][0]
        size[X][0]+=size[Y][1]
    else:
        P[X]=P[Y]
        size[Y][0]+=size[X][0]
        size[Y][0]+=size[X][1]
        
for i in range(H):
    for j in range(W):
        if j+1<=W-1:
            
            if L[i][j]!=L[i][j+1]:
                
                if find(i*W+j)!=find(i*W+j+1):
                    #print(i,j)
                    union(i*W+j,i*W+j+1)
        if i+1<=H-1:
            if L[i][j]!=L[i+1][j]:
                if find((i)*W+j)!=find((i+1)*W+j):
                    union(i*W+j,(i+1)*W+j)
BW=[[0,0]for i in range(H*W)]
for i in range(H):
    for j in range(W):
        if L[i][j]==".":
            BW[find(i*W+j)][0]+=1
        else:
            BW[find(i*W+j)][1]+=1

ans=0
for i in range(H):
    for j in range(W):
        if P[i*W+j]==i*W+j:
            ans+=BW[i*W+j][0]*BW[i*W+j][1]
#print(P)
#print(BW)
print(ans)

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

L=[]
l=["*" for i in range(W+2)]
L.append(l)
for i in range(H):
    s=input()
    s="*"+s+"*"
    s=list(s)
    L.append(s)
L.append(l)

#print(L)
Li=[]
for i in range(1,H+1):
    for j in range(1,W+1):
        if L[i][j]=="#":
            Li.append([i,j])
#print(Li)
x=[1,-1,0,0]
y=[0,0,1,-1]
ans=0
for i in range(len(Li)):
    A=[[0 for i in range(W+2)]for j in range(H+2)]
    Arr=[[Li[i][0],Li[i][1],0]]
    #print(Arr)
    A[Arr[0][0]][Arr[0][1]]=1
    for j in range(W*H*2):
        if Arr==[]:
            break
        else:
            #print(Arr,ans)
            
            if (Arr[0][2])%2==0:
                for k in range(4):
                    if L[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=="." and A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]==0:
                        Arr.append([Arr[0][0]+x[k],Arr[0][1]+y[k],Arr[0][2]+1])
                        A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=1
                        ans+=1
                del Arr[0]
            else:
                for k in range(4):
                    if L[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=="#" and A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]==0:
                        Arr.append([Arr[0][0]+x[k],Arr[0][1]+y[k],Arr[0][2]+1])
                        A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=1
                del Arr[0]
print(ans)

Fixed submission:
H,W=map(int,input().split())
L=[list(input())for i in range(H)]
#print(L)

P=[i for i in range(H*W)]
size=[[0,0]for i in range(H*W)]

for i in range(H):
    for j in range(W):
        if L[i][j]==".":
            size[i*W+j][0]+=1
        else:
            size[i*W+j][1]+=1
#print(size)

def find(z):
    if  z==P[z]:
        return z
    else:
        return find(P[z])
def union(x,y):
    X=find(x)
    Y=find(y)
    if sum(size[X])>=sum(size[Y]):
        P[Y]=P[X]
        size[X][0]+=size[Y][0]
        size[X][0]+=size[Y][1]
    else:
        P[X]=P[Y]
        size[Y][0]+=size[X][0]
        size[Y][0]+=size[X][1]
        
for i in range(H):
    for j in range(W):
        if j+1<=W-1:
            
            if L[i][j]!=L[i][j+1]:
                
                if find(i*W+j)!=find(i*W+j+1):
                    #print(i,j)
                    union(i*W+j,i*W+j+1)
        if i+1<=H-1:
            if L[i][j]!=L[i+1][j]:
                if find((i)*W+j)!=find((i+1)*W+j):
                    union(i*W+j,(i+1)*W+j)
BW=[[0,0]for i in range(H*W)]
for i in range(H):
    for j in range(W):
        if L[i][j]==".":
            BW[find(i*W+j)][0]+=1
        else:
            BW[find(i*W+j)][1]+=1

ans=0
for i in range(H):
    for j in range(W):
        if P[i*W+j]==i*W+j:
            ans+=BW[i*W+j][0]*BW[i*W+j][1]
#print(P)
#print(BW)
print(ans)

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

L=[]
l=["*" for i in range(W+2)]
L.append(l)
for i in range(H):
    s=input()
    s="*"+s+"*"
    s=list(s)
    L.append(s)
L.append(l)

#print(L)
Li=[]
for i in range(1,H+1):
    for j in range(1,W+1):
        if L[i][j]=="#":
            Li.append([i,j])
#print(Li)
x=[1,-1,0,0]
y=[0,0,1,-1]
ans=0
for i in range(len(Li)):
    A=[[0 for i in range(W+2)]for j in range(H+2)]
    Arr=[[Li[i][0],Li[i][1],0]]
    #print(Arr)
    A[Arr[0][0]][Arr[0][1]]=1
    for j in range(W*H*2):
        if Arr==[]:
            break
        else:
            #print(Arr,ans)
            
            if (Arr[0][2])%2==0:
                for k in range(4):
                    if L[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=="." and A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]==0:
                        Arr.append([Arr[0][0]+x[k],Arr[0][1]+y[k],Arr[0][2]+1])
                        A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=1
                        ans+=1
                del Arr[0]
            else:
                for k in range(4):
                    if L[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=="#" and A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]==0:
                        Arr.append([Arr[0][0]+x[k],Arr[0][1]+y[k],Arr[0][2]+1])
                        A[Arr[0][0]+x[k]][Arr[0][1]+y[k]]=1
                del Arr[0]
print(ans)

Fixed submission:
H,W=map(int,input().split())
L=[list(input())for i in range(H)]
#print(L)

P=[i for i in range(H*W)]
size=[[0,0]for i in range(H*W)]

for i in range(H):
    for j in range(W):
        if L[i][j]==".":
            size[i*W+j][0]+=1
        else:
            size[i*W+j][1]+=1
#print(size)

def find(z):
    if  z==P[z]:
        return z
    else:
        return find(P[z])
def union(x,y):
    X=find(x)
    Y=find(y)
    if sum(size[X])>=sum(size[Y]):
        P[Y]=P[X]
        size[X][0]+=size[Y][0]
        size[X][0]+=size[Y][1]
    else:
        P[X]=P[Y]
        size[Y][0]+=size[X][0]
        size[Y][0]+=size[X][1]
        
for i in range(H):
    for j in range(W):
        if j+1<=W-1:
            
            if L[i][j]!=L[i][j+1]:
                
                if find(i*W+j)!=find(i*W+j+1):
                    #print(i,j)
                    union(i*W+j,i*W+j+1)
        if i+1<=H-1:
            if L[i][j]!=L[i+1][j]:
                if find((i)*W+j)!=find((i+1)*W+j):
                    union(i*W+j,(i+1)*W+j)
BW=[[0,0]for i in range(H*W)]
for i in range(H):
    for j in range(W):
        if L[i][j]==".":
            BW[find(i*W+j)][0]+=1
        else:
            BW[find(i*W+j)][1]+=1

ans=0
for i in range(H):
    for j in range(W):
        if P[i*W+j]==i*W+j:
            ans+=BW[i*W+j][0]*BW[i*W+j][1]
#print(P)
#print(BW)
print(ans)