Problem p03157 (python)

Fixed submission:
H,W=map(int,input().split())
S=[input() for i in range(H)]
visit=[[False]*W for i in range(H)]
par=[[None]*W for i in range(H)]
dd={}
X=[-1,0,1,0]
Y=[0,-1,0,1]
for j in range(H):
    for i in range(W):
        if visit[j][i]:
            continue
        if S[j][i]=='.':
            continue
        lH=[]
        lH.append((j,i,0))#0が黒
        par[j][i]=(i,j)
        num=0
        while lH:
            y,x,t=lH.pop()
            visit[y][x]=True
            for k in range(4):
                nx=x+X[k]
                ny=y+Y[k]
                if 0<=nx<W and 0<=ny<H:
                    if visit[ny][nx]:
                        continue
                    if t==0 and S[ny][nx]=='.':
                        visit[ny][nx]=True
                        num+=1
                        lH.append((ny,nx,1))
                    elif t==1 and S[ny][nx]=='#':
                        visit[ny][nx] = True
                        par[ny][nx]=(i,j)
                        lH.append((ny,nx,0))
        dd[(i,j)]=num
ans=0
for j in range(H):
    for i in range(W):
        if S[j][i]=='#':
            x,y=par[j][i]
            ans+=dd[(x,y)]
print(ans)

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[input() for _ in range(h)]
dy=[0,0,1,-1]
dx=[1,-1,0,0]
import sys
sys.setrecursionlimit(10000)
def dfs(y,x):
    global check
    global was
    was[y][x]=1
    if s[y][x]=="#":
        check[0]+=1
    else:
        check[1]+=1
    for k in range(4):
        nx=x+dx[k]
        ny=y+dy[k]
        if nx >= 0 and ny >= 0 and nx < w and ny < h and s[ny][nx] != s[y][x] and not was[ny][nx]:
            dfs(ny,nx) 
ans=0
was=[[0 for _ in range(w)] for _ in range(h)]
for i in range(w):
    for j in range(h):
        if not was[j][i]:
            check=[0,0]
            dfs(j,i)
            ans+=check[0]*check[1]
print(ans)

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[input() for _ in range(h)]
dy=[0,0,1,-1]
dx=[1,-1,0,0]
def dfs(y,x):
    global check
    global was
    was[y][x]=1
    if s[y][x]=="#":
        check[0]+=1
    else:
        check[1]+=1
    for k in range(4):
        nx=x+dx[k]
        ny=y+dy[k]
        if nx >= 0 and ny >= 0 and nx < w and ny < h and s[ny][nx] != s[y][x] and not was[ny][nx]:
            dfs(ny,nx) 
ans=0
was=[[0 for _ in range(w)] for _ in range(h)]
for i in range(w):
    for j in range(h):
        if not was[j][i]:
            check=[0,0]
            dfs(j,i)
            ans+=check[0]*check[1]
print(ans)

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[input() for _ in range(n)]
dy=[0,0,1,-1]
dx=[1,-1,0,0]
def dfs(y,x):
    global check
    global was
    was[y][x]=1
    if s[y][x]=="#":
        check[0]+=1
    else:
        check[1]+=1
    for k in range(4):
        nx=x+dx[k]
        ny=y+dy[k]
        if nx >= 0 and ny >= 0 and nx < w and ny < h and s[ny][nx] != s[y][x] and not was[ny][nx]:
            dfs(ny,nx) 
ans=0
was=[[0 for _ in range(w)] for _ in range(h)]
for i in range(w):
    for j in range(h):
        if not was[j][i]:
            check=[0,0]
            dfs(j,i)
            ans+=check[0]*check[1]
print(ans)