Problem p03157 (python)

Fixed submission:
# coding: utf-8
# Your code here!

#n = int(input())
import sys

sys.setrecursionlimit(1000000)

h,W = [int(i) for i in input().split()]
#b = [i for i in input().split()]
board = []
for _ in range(h):
    board.append(input())

checked = [[0]*W for i in range(h)]

dx=[ 0, 1, 0,-1]
dy=[ 1, 0,-1, 0]

ans = 0

def dfs(x,y,b,w):
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
#        print(nx,ny)
#        print(checked)
        if nx < 0 or nx >= h or ny < 0 or ny >= W or checked[nx][ny]==1:
            continue
        if board[x][y] == board[nx][ny]:
            continue
#        print("ok")
        checked[nx][ny]=1
        if board[nx][ny] == '#':
            b+=1
        else:
            w+=1
        b1,w1 = dfs(nx, ny,0,0)
        b+=b1
        w+=w1
#    print(x,y,b,w)
    return b,w

#print(dfs(0,0,0,0))

for i in range(h):
    for j in range(W):
        if checked[i][j] == 1:
            continue
        checked[i][j] = 1
        if board[i][j] == '#':
            b2 = 1; w2=0
        else:
            b2 = 0; w2=1
        b1,w1 = dfs(i,j,b2,w2)
#        print(b1,w1)
        ans += b1*w1

print(ans)

Buggy submission (verdict: Runtime Error):
h,w = [int(i) for i in input().split()]
board = [input() for _ in range(h)]

used = [[0]*w for _ in range(h)]
dx=[ 0, 1, 0,-1]
dy=[ 1, 0,-1, 0]

def dfs(x, y):
    global numb, numw #numbたちがグローバル変数だと教えてあげる
    #(x,y)地点を訪問済みにする
    used[x][y] = 1
    #(x,y)地点での処理を行う（マスの色をカウントするなど）
    if board[x][y] == '#':
        numb += 1
    else:
        numw += 1
    #foreach((nx,ny) \in (x,y)のお隣さん){(nx,ny)に訪問していないとき、dfs(nx,ny)} 
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if nx < 0 or nx >= h or ny < 0 or ny >= w or used[nx][ny] == 1: continue #範囲外参照 or (nx,ny)は確認済み スキップ
        if board[x][y]!=board[nx][ny]: #さらに(nx,ny)と(x,y)がつながっている場合(nx,ny)を訪問
            dfs(nx,ny)
    return #情報をもらうとき：返り値、各地点の情報のみで更新：グローバル変数、情報を送るとき：引数

ans=0
for i in range(h):
    for j in range(w):
        numw = 0
        numb = 0
        if used[i][j] == 1: continue #すでに確認した地点は見ない
        dfs(i,j)
        ans += numw*numb

print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

#n = int(input())
import sys

sys.setrecursionlimit(100000)

h,W = [int(i) for i in input().split()]
#b = [i for i in input().split()]
board = []
for _ in range(h):
    board.append(input())

checked = [[0]*W for i in range(h)]

dx=[ 0, 1, 0,-1]
dy=[ 1, 0,-1, 0]

ans = 0

def dfs(x,y,b,w):
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
#        print(nx,ny)
#        print(checked)
        if nx < 0 or nx >= h or ny < 0 or ny >= W or checked[nx][ny]==1:
            continue
        if board[x][y] == board[nx][ny]:
            continue
#        print("ok")
        checked[nx][ny]=1
        if board[nx][ny] == '#':
            b+=1
        else:
            w+=1
        b1,w1 = dfs(nx, ny,0,0)
        b+=b1
        w+=w1
#    print(x,y,b,w)
    return b,w

#print(dfs(0,0,0,0))

for i in range(h):
    for j in range(W):
        if checked[i][j] == 1:
            continue
        checked[i][j] = 1
        if board[i][j] == '#':
            b2 = 1; w2=0
        else:
            b2 = 0; w2=1
        b1,w1 = dfs(i,j,b2,w2)
#        print(b1,w1)
        ans += b1*w1

print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

#n = int(input())
h,W = [int(i) for i in input().split()]
#b = [i for i in input().split()]
board = []
for _ in range(h):
    board.append(input())

checked = [[0]*W for i in range(h)]

dx=[ 0, 1, 0,-1]
dy=[ 1, 0,-1, 0]

ans = 0

def dfs(x,y,b,w):
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
#        print(nx,ny)
#        print(checked)
        if nx < 0 or nx >= h or ny < 0 or ny >= W or checked[nx][ny]==1:
            continue
        if board[x][y] == board[nx][ny]:
            continue
#        print("ok")
        checked[nx][ny]=1
        if board[nx][ny] == '#':
            b+=1
        else:
            w+=1
        b1,w1 = dfs(nx, ny,0,0)
        b+=b1
        w+=w1
#    print(x,y,b,w)
    return b,w

#print(dfs(0,0,0,0))

for i in range(h):
    for j in range(W):
        if checked[i][j] == 1:
            continue
        checked[i][j] = 1
        if board[i][j] == '#':
            b2 = 1; w2=0
        else:
            b2 = 0; w2=1
        b1,w1 = dfs(i,j,b2,w2)
#        print(b1,w1)
        ans += b1*w1

print(ans)