Problem p03157 (python)

Fixed submission:
import sys
c=0
b=0
wi=0
x=[-1,0,1,0]
y=[0,-1,0,1]
buf=[]
def one():
    return -1
def count(h,w,S,table,H,W,index):
    global buf,x,y,b,wi
    if index==1:
        b+=1
    else:
        wi+=1
    if index==1:
        S[h][w]='@'
    for i in range(4):
        if h+y[i]>=0 and h+y[i]<H and w+x[i]>=0 and w+x[i]<W:
            if table[h+y[i]][w+x[i]]==-1:
                if index==0 and S[h+y[i]][w+x[i]]=='#':
                    table[h+y[i]][w+x[i]]=1
                    buf.append([h+y[i],w+x[i],1])
                elif index==1 and S[h+y[i]][w+x[i]]=='.':
                    table[h+y[i]][w+x[i]]=1
                    buf.append([h+y[i],w+x[i],0])
    if buf:
        a=buf.pop(0)
        count(a[0],a[1],S,table,H,W,a[2])
sys.setrecursionlimit(10000000)
H,W=map(int,input().split())
T=[]
table=[[-1 for j in range(W)] for i in range(H)]
for i in range(H):
    T.append(input())
S=[['' for j in range(W)]for i in range(H)]
for h in range(H):
    for w in range(W):
        S[h][w]=T[h][w]
for h in range(H):
    for w in range(W):
        if S[h][w]=='#':
            table[h][w]=0
            count(h,w,S,table,H,W,1)
            c+=b*wi
            b=0
            wi=0
print(c)

Buggy submission (verdict: Runtime Error):
c=0
x=[-1,0,1,0]
y=[0,-1,0,1]
buf=[]
p=0
def count(h,w,S,table,H,W):
    global buf,x,y,p
    for i in range(4):
        if h+y[i]>=0 and h+y[i]<H and w+x[i]>=0 and w+x[i]<W:
            if table[h+y[i]][w+x[i]]==-1:
                if S[h][w]=='.' and S[h+y[i]][w+x[i]]=='#':
                    table[h+y[i]][w+x[i]]=table[h][w]+1
                    buf.append([h+y[i],w+x[i]])
                elif S[h][w]=='#' and S[h+y[i]][w+x[i]]=='.':
                    table[h+y[i]][w+x[i]]=table[h][w]+1
                    buf.append([h+y[i],w+x[i]])
    if buf:
        a=buf.pop(0)
        count(a[0],a[1],S,table,H,W)

H,W=map(int,input().split())
S=[]
for i in range(H):
    S.append(input())
for h in range(H):
    for w in range(W):
        if S[h][w]=='#':
            table=[[-1 for j in range(W)] for i in range(H)]
            table[h][w]=0
            p=0
            count(h,w,S,table,H,W)
            for k in range(H):
                for l in range(W):
                    if table[k][l]%2==1 and table[k][l]!=-1:
                        c+=1
print(c)

Buggy submission (verdict: Runtime Error):
c=0
x=[-1,0,1,0]
y=[0,-1,0,1]
buf=[]
p=0
def count(h,w,S,table,H,W):
    global buf,x,y,p
    if buf or p==0:
        for i in range(4):
            if h+y[i]>=0 and h+y[i]<H and w+x[i]>=0 and w+x[i]<W:
                if table[h+y[i]][w+x[i]]==-1:
                    if S[h][w]=='.' and S[h+y[i]][w+x[i]]=='#':
                        table[h+y[i]][w+x[i]]=table[h][w]+1
                        buf.append([h+y[i],w+x[i]])
                    elif S[h][w]=='#' and S[h+y[i]][w+x[i]]=='.':
                        table[h+y[i]][w+x[i]]=table[h][w]+1
                        buf.append([h+y[i],w+x[i]])
        if buf:
            a=buf.pop(0)
            count(a[0],a[1],S,table,H,W)
    elif not(buf) and p==0:
        p+=1
        count(h,w,S,table,H,W)

H,W=map(int,input().split())
S=[]
for i in range(H):
    S.append(input())
for h in range(H):
    for w in range(W):
        if S[h][w]=='#':
            table=[[-1 for j in range(W)] for i in range(H)]
            table[h][w]=0
            p=0
            count(h,w,S,table,H,W)
            for k in range(H):
                for l in range(W):
                    if table[k][l]%2==1 and table[k][l]!=-1:
                        c+=1
print(c)

Buggy submission (verdict: Runtime Error):
c=0
x=[-1,0,1,0]
y=[0,-1,0,1]
buf=[]
p=0
def count(h,w,S,table,H,W):
    global buf,x,y,p
    if buf or p==0:
        for i in range(4):
            if h+y[i]>=0 and h+y[i]<H and w+x[i]>=0 and w+x[i]<W:
                if table[h+y[i]][w+x[i]]==-1:
                    if S[h][w]=='.' and S[h+y[i]][w+x[i]]=='#':
                        table[h+y[i]][w+x[i]]=table[h][w]+1
                        buf.append([h+y[i],w+x[i]])
                    elif S[h][w]=='#' and S[h+y[i]][w+x[i]]=='.':
                        table[h+y[i]][w+x[i]]=table[h][w]+1
                        buf.append([h+y[i],w+x[i]])
        if buf:
            a=buf.pop(0)
            count(a[0],a[1],S,table,H,W)
    elif not(buf) and p==0:
        p+=1
        count(h,w,S,table,H,W)

H,W=map(int,input().split())
S=[]
for i in range(H):
    S.append(input())
for h in range(H):
    for w in range(W):
        if S[h][w]=='#':
            table=[[-1 for j in range(W)]for i in range(H)]
            table[h][w]=0
            count(h,w,S,table,H,W)
            for k in range(H):
                for l in range(W):
                    if table[k][l]%2==1 and table[k][l]!=-1:
                        c+=1
print(c)