Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
S=[input() for i in [0]*H]
M=[[-1]*W for _ in [0]*H]
def dfs(i,j,k):
    M[i][j]=k
    s=S[i][j]
    if i+1<H and M[i+1][j]==-1 and s!=S[i+1][j]:
        dfs(i+1,j,k)
    if i-1>=0 and M[i-1][j]==-1 and s!=S[i-1][j]:
        dfs(i-1,j,k)
    if j+1<W and M[i][j+1]==-1 and s!=S[i][j+1]:
        dfs(i,j+1,k)
    if j-1>=0 and M[i][j-1]==-1 and s!=S[i][j-1]:
        dfs(i,j-1,k)
k=0
for i in range(H):
    for j in range(W):
        if M[i][j]==-1:dfs(i,j,k);k+=1
ds={}# #
dd={}# .
#di=set()
for i in range(H):
    for j in range(W):
        m=M[i][j]
        if S[i][j]=='#':ds[m]=ds.get(m,0)+1
        else:dd[m]=dd.get(m,0)+1
        #di|={m}
r=0
for i in range(k):
    r += ds.get(i,0)*dd.get(i,0)
print(r)

Fixed submission:
import collections
H,W=map(int,input().split())
S=[input() for i in [0]*H]
M=[[-1]*W for _ in [0]*H]
k=0
for y in range(H):
    for x in range(W):
        if M[y][x]!=-1:continue #visited
        M[y][x]=k
        Q=collections.deque()
        Q.append((y,x))
        while len(Q)>0:
            i,j=Q.popleft()
            s=S[i][j]
            if i+1<H and M[i+1][j]==-1 and s!=S[i+1][j]:
                M[i+1][j]=k
                Q.append((i+1,j))
            if i-1>=0 and M[i-1][j]==-1 and s!=S[i-1][j]:
                M[i-1][j]=k
                Q.append((i-1,j))
            if j+1<W and M[i][j+1]==-1 and s!=S[i][j+1]:
                M[i][j+1]=k
                Q.append((i,j+1))
            if j-1>=0 and M[i][j-1]==-1 and s!=S[i][j-1]:
                M[i][j-1]=k
                Q.append((i,j-1))
            #print(k,M,Q)
        k+=1
ds={}
dd={}
for i in range(H):
    for j in range(W):
        m=M[i][j]
        if S[i][j]=='#':ds[m]=ds.get(m,0)+1
        else:dd[m]=dd.get(m,0)+1
r=0
for i in range(k):
    r += ds.get(i,0)*dd.get(i,0)
print(r)

Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
S=[input() for i in [0]*H]
M=[[-1]*W for _ in [0]*H]
def dfs(i,j,k):
    M[i][j]=k
    s=S[i][j]
    if i+1<H and M[i+1][j]==-1 and s!=S[i+1][j]:
        dfs(i+1,j,k)
    if i-1>0 and M[i-1][j]==-1 and s!=S[i-1][j]:
        dfs(i-1,j,k)
    if j+1<W and M[i][j+1]==-1 and s!=S[i][j+1]:
        dfs(i,j+1,k)
    if j-1>0 and M[i][j-1]==-1 and s!=S[i][j-1]:
        dfs(i,j-1,k)
k=0
for i in range(H):
    for j in range(W):
        if M[i][j]==-1:dfs(i,j,k);k+=1
ds={}# #
dd={}# .
for i in range(H):
    for j in range(W):
        m=M[i][j]
        if S[i][j]=='#':ds[m]=ds.get(m,0)+1
        else:dd[m]=dd.get(m,0)+1
r=0
for i in range(k):
    r += ds.get(i,0)*dd.get(i,0)
print(r)

Fixed submission:
import collections
H,W=map(int,input().split())
S=[input() for i in [0]*H]
M=[[-1]*W for _ in [0]*H]
k=0
for y in range(H):
    for x in range(W):
        if M[y][x]!=-1:continue #visited
        M[y][x]=k
        Q=collections.deque()
        Q.append((y,x))
        while len(Q)>0:
            i,j=Q.popleft()
            s=S[i][j]
            if i+1<H and M[i+1][j]==-1 and s!=S[i+1][j]:
                M[i+1][j]=k
                Q.append((i+1,j))
            if i-1>=0 and M[i-1][j]==-1 and s!=S[i-1][j]:
                M[i-1][j]=k
                Q.append((i-1,j))
            if j+1<W and M[i][j+1]==-1 and s!=S[i][j+1]:
                M[i][j+1]=k
                Q.append((i,j+1))
            if j-1>=0 and M[i][j-1]==-1 and s!=S[i][j-1]:
                M[i][j-1]=k
                Q.append((i,j-1))
            #print(k,M,Q)
        k+=1
ds={}
dd={}
for i in range(H):
    for j in range(W):
        m=M[i][j]
        if S[i][j]=='#':ds[m]=ds.get(m,0)+1
        else:dd[m]=dd.get(m,0)+1
r=0
for i in range(k):
    r += ds.get(i,0)*dd.get(i,0)
print(r)

Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
S=[input() for i in [0]*H]
m=[[-1]*W for _ in [0]*H]
def dfs(i,j,k):
    m[i][j]=k
    if i+1<H and m[i+1][j]==-1 and S[i][j]!=S[i+1][j]:
        dfs(i+1,j,k)
    if i-1>0 and m[i-1][j]==-1 and S[i][j]!=S[i-1][j]:
        dfs(i-1,j,k)
    if j+1<W and m[i][j+1]==-1 and S[i][j]!=S[i][j+1]:
        dfs(i,j+1,k)
    if j-1>0 and m[i][j-1]==-1 and S[i][j]!=S[i][j-1]:
        dfs(i,j-1,k)
k=0
for i in range(H):
    for j in range(W):
        if m[i][j]==-1:
            dfs(i,j,k)
            k += 1
d={}
di=set()
for i in range(H):
    for j in range(W):
        mij=m[i][j]
        sij=0 if S[i][j]=='.' else 1
        if (mij,sij) in d.keys():d[(mij,sij)]+=1
        else:d[(mij,sij)]=1
        di|={mij}
r=0
for i in di:
    if (i,0) in d.keys() and (i,1) in d.keys():
        r += d[(i,0)]*d[(i,1)]
#print(m)
#print(d,di)
print(r)

Fixed submission:
import collections
H,W=map(int,input().split())
S=[input() for i in [0]*H]
M=[[-1]*W for _ in [0]*H]
k=0
for y in range(H):
    for x in range(W):
        if M[y][x]!=-1:continue #visited
        M[y][x]=k
        Q=collections.deque()
        Q.append((y,x))
        while len(Q)>0:
            i,j=Q.popleft()
            s=S[i][j]
            if i+1<H and M[i+1][j]==-1 and s!=S[i+1][j]:
                M[i+1][j]=k
                Q.append((i+1,j))
            if i-1>=0 and M[i-1][j]==-1 and s!=S[i-1][j]:
                M[i-1][j]=k
                Q.append((i-1,j))
            if j+1<W and M[i][j+1]==-1 and s!=S[i][j+1]:
                M[i][j+1]=k
                Q.append((i,j+1))
            if j-1>=0 and M[i][j-1]==-1 and s!=S[i][j-1]:
                M[i][j-1]=k
                Q.append((i,j-1))
            #print(k,M,Q)
        k+=1
ds={}
dd={}
for i in range(H):
    for j in range(W):
        m=M[i][j]
        if S[i][j]=='#':ds[m]=ds.get(m,0)+1
        else:dd[m]=dd.get(m,0)+1
r=0
for i in range(k):
    r += ds.get(i,0)*dd.get(i,0)
print(r)