Problem p03361 (python)

Fixed submission:
import sys

h,w = map(int,input().split())
c = [None]*h
dx = [1,0,-1,0]
dy = [0,1,0,-1]

for i in range(h):
    c[i] = input()
    
for i in range(h):
    for j in range(w):
        a = 0
        b = 0
        for k in range(4):
            nx = j + dx[k]
            ny = i + dy[k]
            if c[i][j] == '#' and 0<=nx and nx<w and 0<=ny and ny<h:
                b += 1
                if c[ny][nx] == '.':
                    a += 1
                    
        if a == b and b != 0:
            print("No")
            sys.exit()
            
print("Yes")

Buggy submission (verdict: Runtime Error):
from collections import deque
from collections import Counter

h,w = map(int,input().split())

c = [None]*h
d = [[-1 for i in range(w)] for j in range(h)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
kk = 0

for i in range(h):
    c[i] = input()
    
for i in range(h):
    for j in range(w):
        if c[i][j] == "#":
            d[i][j] = 0
    
def bfs(y,x):
    global kk
    d[y][x] = -1
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
            
        if 0<=nx and nx<w and 0<=ny and ny<h and d[ny][nx] != -1:
            kk += 1
            bfs(ny,nx)
            

resmin  = 100000
an = []
res = 0
for i in range(h):
    for j in range(w):   
        if d[i][j] == 0:
            bfs(i,j)
            res += 1
            an.append(kk+1)
            
b = Counter(an)
if h == 1 and w == 1:
    print("Yes")
else:
    if b[1] != 0:
        print("No")
    else:
        print("Yes")

Buggy submission (verdict: Runtime Error):
from collections import deque
from collections import Counter

h,w = map(int,input().split())

c = [None]*h
d = [[-1 for i in range(w)] for j in range(h)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
kk = 0

for i in range(h):
    c[i] = input()
    
for i in range(h):
    for j in range(w):
        if c[i][j] == "#":
            d[i][j] = 0
    
def bfs(y,x):
    global kk
    d[y][x] = -1
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
            
        if 0<=nx and nx<w and 0<=ny and ny<h and d[ny][nx] != -1:
            kk += 1
            bfs(ny,nx)
            

resmin  = 100000
an = []
res = 0
for i in range(h):
    for j in range(w):   
        if d[i][j] == 0:
            bfs(i,j)
            res += 1
            an.append(kk+1)
            
b = Counter(an)
if a == 1 and b == 1:
    print("Yes")
else:
    if b[1] != 0:
        print("No")
    else:
        print("Yes")

Buggy submission (verdict: Runtime Error):
from collections import deque
from collections import Counter

h,w = map(int,input().split())

c = [None]*h
d = [[-1 for i in range(w)] for j in range(h)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
kk = 0

for i in range(h):
    c[i] = input()
    
for i in range(h):
    for j in range(w):
        if c[i][j] == "#":
            d[i][j] = 0
    
def bfs(y,x):
    global kk
    d[y][x] = -1
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
            
        if 0<=nx and nx<w and 0<=ny and ny<h and d[ny][nx] != -1:
            kk += 1
            bfs(ny,nx)
            

resmin  = 100000
an = []
res = 0
for i in range(h):
    for j in range(w):   
        if d[i][j] == 0:
            bfs(i,j)
            res += 1
            an.append(kk+1)
            
b = Counter(an)
if b[1] != 0:
    print("No")
else:
    print("Yes")