Problem p03157 (python)

Fixed submission:
H, W = [int(x) for x in input().strip().split(" ")]

import sys
sys.setrecursionlimit(1000000)
b = []
bset = set()
wset = set()
i = 0
j = 0
for line in sys.stdin:
    b.append(line.strip())
    for s in line.strip():
        if s == "#":
            bset.add((i, j))
        else:
            wset.add((i, j))
        j += 1
    j = 0
    i += 1

fbset = set()
fwset = set()

def it_b(i, j):
    fbset.add((i, j))
    totalb = 1
    totalw = 0
    bset.remove((i, j))
    #print("b", i,j)
    if i - 1 >= 0:
        if b[i-1][j] == "." and (i-1, j) not in fwset:
            rb, rw = it_w(i-1, j)
            totalb += rb
            totalw += rw
    if j - 1 >= 0:
        if b[i][j-1] == "." and (i, j-1) not in fwset:
            rb, rw = it_w(i, j-1)
            totalb += rb
            totalw += rw
    if i + 1 <= H-1:
        if b[i+1][j] == "." and (i+1, j) not in fwset:
            rb, rw = it_w(i+1, j)
            totalb += rb
            totalw += rw
    if j + 1 <= W-1:
        if b[i][j+1] == "." and (i, j+1) not in fwset:
            rb, rw = it_w(i, j+1)
            totalb += rb
            totalw += rw
    return totalb, totalw

def it_w(i, j):
    fwset.add((i, j))
    totalb = 0
    totalw = 1
    #print("w", i,j)
    if i - 1 >= 0:
        if b[i-1][j] == "#" and (i-1, j) not in fbset:
            rb, rw = it_b(i-1, j)
            totalb += rb
            totalw += rw
    if j - 1 >= 0:
        if b[i][j-1] == "#" and (i, j-1) not in fbset:
            rb, rw = it_b(i, j-1)
            totalb += rb
            totalw += rw
    if i + 1 <= H-1:
        if b[i+1][j] == "#" and (i+1, j) not in fbset:
            rb, rw = it_b(i+1, j)
            totalb += rb
            totalw += rw
    if j + 1 <= W-1:
        if b[i][j+1] == "#" and (i, j+1) not in fbset:
            rb, rw = it_b(i, j+1)
            totalb += rb
            totalw += rw
    return totalb, totalw

t = 0
while True:
    if len(bset) == 0:
        break
    x = bset.pop()
    bset.add(x)
    rb, rw = it_b(x[0], x[1])
    t += rb * rw
print(t)

Buggy submission (verdict: Runtime Error):
H, W = [int(x) for x in input().strip().split(" ")]

import sys
sys.setrecursionlimit(100000)
b = []
bset = set()
wset = set()
i = 0
j = 0
for line in sys.stdin:
    b.append(line.strip())
    for s in line.strip():
        if s == "#":
            bset.add((i, j))
        else:
            wset.add((i, j))
        j += 1
    j = 0
    i += 1

fbset = set()
fwset = set()

def it_b(i, j):
    fbset.add((i, j))
    totalb = 1
    totalw = 0
    bset.remove((i, j))
    #print("b", i,j)
    if i - 1 >= 0:
        if b[i-1][j] == "." and (i-1, j) not in fwset:
            rb, rw = it_w(i-1, j)
            totalb += rb
            totalw += rw
    if j - 1 >= 0:
        if b[i][j-1] == "." and (i, j-1) not in fwset:
            rb, rw = it_w(i, j-1)
            totalb += rb
            totalw += rw
    if i + 1 <= H-1:
        if b[i+1][j] == "." and (i+1, j) not in fwset:
            rb, rw = it_w(i+1, j)
            totalb += rb
            totalw += rw
    if j + 1 <= W-1:
        if b[i][j+1] == "." and (i, j+1) not in fwset:
            rb, rw = it_w(i, j+1)
            totalb += rb
            totalw += rw
    return totalb, totalw

def it_w(i, j):
    fwset.add((i, j))
    totalb = 0
    totalw = 1
    #print("w", i,j)
    if i - 1 >= 0:
        if b[i-1][j] == "#" and (i-1, j) not in fbset:
            rb, rw = it_b(i-1, j)
            totalb += rb
            totalw += rw
    if j - 1 >= 0:
        if b[i][j-1] == "#" and (i, j-1) not in fbset:
            rb, rw = it_b(i, j-1)
            totalb += rb
            totalw += rw
    if i + 1 <= H-1:
        if b[i+1][j] == "#" and (i+1, j) not in fbset:
            rb, rw = it_b(i+1, j)
            totalb += rb
            totalw += rw
    if j + 1 <= W-1:
        if b[i][j+1] == "#" and (i, j+1) not in fbset:
            rb, rw = it_b(i, j+1)
            totalb += rb
            totalw += rw
    return totalb, totalw

t = 0
while True:
    if len(bset) == 0:
        break
    x = bset.pop()
    bset.add(x)
    rb, rw = it_b(x[0], x[1])
    t += rb * rw
print(t)

Buggy submission (verdict: Runtime Error):
H, W = [int(x) for x in input().strip().split(" ")]

import sys
b = []
bset = set()
wset = set()
i = 0
j = 0
for line in sys.stdin:
    b.append(line.strip())
    for s in line.strip():
        if s == "#":
            bset.add((i, j))
        else:
            wset.add((i, j))
        j += 1
    j = 0
    i += 1

fbset = set()
fwset = set()

def it_b(i, j):
    fbset.add((i, j))
    totalb = 1
    totalw = 0
    bset.remove((i, j))
    #print("b", i,j)
    if i - 1 >= 0:
        if b[i-1][j] == "." and (i-1, j) not in fwset:
            rb, rw = it_w(i-1, j)
            totalb += rb
            totalw += rw
    if j - 1 >= 0:
        if b[i][j-1] == "." and (i, j-1) not in fwset:
            rb, rw = it_w(i, j-1)
            totalb += rb
            totalw += rw
    if i + 1 <= H-1:
        if b[i+1][j] == "." and (i+1, j) not in fwset:
            rb, rw = it_w(i+1, j)
            totalb += rb
            totalw += rw
    if j + 1 <= W-1:
        if b[i][j+1] == "." and (i, j+1) not in fwset:
            rb, rw = it_w(i, j+1)
            totalb += rb
            totalw += rw
    return totalb, totalw

def it_w(i, j):
    fwset.add((i, j))
    totalb = 0
    totalw = 1
    #print("w", i,j)
    if i - 1 >= 0:
        if b[i-1][j] == "#" and (i-1, j) not in fbset:
            rb, rw = it_b(i-1, j)
            totalb += rb
            totalw += rw
    if j - 1 >= 0:
        if b[i][j-1] == "#" and (i, j-1) not in fbset:
            rb, rw = it_b(i, j-1)
            totalb += rb
            totalw += rw
    if i + 1 <= H-1:
        if b[i+1][j] == "#" and (i+1, j) not in fbset:
            rb, rw = it_b(i+1, j)
            totalb += rb
            totalw += rw
    if j + 1 <= W-1:
        if b[i][j+1] == "#" and (i, j+1) not in fbset:
            rb, rw = it_b(i, j+1)
            totalb += rb
            totalw += rw
    return totalb, totalw

t = 0
while True:
    if len(bset) == 0:
        break
    x = bset.pop()
    bset.add(x)
    rb, rw = it_b(x[0], x[1])
    t += rb * rw
print(t)