Problem p03157 (python)

Fixed submission:
import sys
sys.setrecursionlimit(400*400*1000)
from collections import Counter
def rec(union_id, i, j):
    symbol = S[i][j]
    if union_white[i][j] != 0:
        return
    if symbol == '.':
        union_white[i][j] = union_id
        next_symbol = '#'
    else:
        union_white[i][j] = -1
        next_symbol = '.'
        union_black[union_id] += 1

    if i >= 1 and S[i-1][j] == next_symbol:
        rec(union_id, i-1, j)
    if i < H-1 and S[i+1][j] == next_symbol:
        rec(union_id, i+1, j)
    if j >= 1 and S[i][j-1] == next_symbol:
        rec(union_id, i, j-1)
    if j < W-1 and S[i][j+1] == next_symbol:
        rec(union_id, i, j+1)

    return
H, W = map(int, input().split())
S = [input() for h in range(H)]
ans = 0
union_white = [[0 for w in range(W)]for h in range(H)]
union_black = Counter()
count_black = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.' or union_white[i][j] != 0:
            continue
        count_black += 1
        rec(count_black, i, j)

counter = Counter([x for Si in union_white for x in Si])
ans = 0
# print(counter)
# print(union_black)
# print(union_white)
for i in union_black.keys():
    ans += counter[i] * union_black[i]

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(400*400)
from collections import Counter
def rec(union_id, i, j):
    symbol = S[i][j]
    if union_white[i][j] != 0:
        return
    if symbol == '.':
        union_white[i][j] = union_id
        next_symbol = '#'
    else:
        union_white[i][j] = -1
        next_symbol = '.'
        union_black[union_id] += 1

    if i >= 1 and S[i-1][j] == next_symbol:
        rec(union_id, i-1, j)
    if i < H-1 and S[i+1][j] == next_symbol:
        rec(union_id, i+1, j)
    if j >= 1 and S[i][j-1] == next_symbol:
        rec(union_id, i, j-1)
    if j < W-1 and S[i][j+1] == next_symbol:
        rec(union_id, i, j+1)

    return
H, W = map(int, input().split())
S = [input() for h in range(H)]
ans = 0
union_white = [[0 for w in range(W)]for h in range(H)]
union_black = Counter()
count_black = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.' or union_white[i][j] != 0:
            continue
        count_black += 1
        rec(count_black, i, j)

counter = Counter([x for Si in union_white for x in Si])
ans = 0
# print(counter)
# print(union_black)
# print(union_white)
for i in union_black.keys():
    ans += counter[i] * union_black[i]

print(ans)

Buggy submission (verdict: Runtime Error):
from collections import Counter
def rec(union_id, i, j):
    symbol = S[i][j]
    if union_white[i][j] != 0:
        return
    if symbol == '.':
        union_white[i][j] = union_id
        next_symbol = '#'
    else:
        union_white[i][j] = -1
        next_symbol = '.'
        union_black[union_id] += 1

    if i >= 1 and S[i-1][j] == next_symbol:
        rec(union_id, i-1, j)
    if i < H-1 and S[i+1][j] == next_symbol:
        rec(union_id, i+1, j)
    if j >= 1 and S[i][j-1] == next_symbol:
        rec(union_id, i, j-1)
    if j < W-1 and S[i][j+1] == next_symbol:
        rec(union_id, i, j+1)

    return
H, W = map(int, input().split())
S = [input() for h in range(H)]
ans = 0
union_white = [[0 for w in range(W)]for h in range(H)]
union_black = Counter()
count_black = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.' or union_white[i][j] != 0:
            continue
        count_black += 1
        rec(count_black, i, j)

counter = Counter([x for Si in union_white for x in Si])
ans = 0
# print(counter)
# print(union_black)
# print(union_white)
for i in union_black.keys():
    ans += counter[i] * union_black[i]

print(ans)