Problem p03053 (python)

Fixed submission:
from subprocess import call
call (('pypy3', '-c', """
from collections import deque

def main():
    h, w = map(int, input().split())
    A = [list(input()) for _ in range(h)]
    d = [[10**9] * w for _ in range(h)]
    que = deque([])
    
    for i in range(h):
        for j in range(w):
           if A[i][j] == "#":
               que.append([i, j])
               d[i][j] = 0
           else:
               d[i][j] = 10 ** 9
            
    while que:
        x, y = que.popleft()
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for i in range(4): 
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < h and 0 <= ny < w and d[nx][ny] == 10**9:
                d[nx][ny] = d[x][y] + 1
                que.append([nx, ny])
    ans = 0
    for i in range(h):
        for j in range(w):
            ans = max(ans, d[i][j])
    print(ans)
    
    return
    
main()

"""))

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

def bfs():
    global ans
    d = [[float("inf")] * w for _ in range(h)]
    que = deque([])
    for i in range(h):
        for j in range(w):
           if A[i][j] == "#":
               que.append((i, j))
               d[i][j] = 0
            
    while que:
        p = que.popleft()
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        i = 0
        while i < 4: 
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < w and 0 <= ny < h and A[nx][ny] != "#" and d[nx][ny] == float("inf"):
                d[nx][ny] = d[p[0]][p[1]] + 1
                A[nx][ny] = "#"
                que.append((nx, ny))
            i += 1
    ans = 0
    for i in range(h):
        for j in range(w):
            ans = max(ans, d[i][j])


h, w = map(int, input().split())
A = [list(input()) for _ in range(h)]
ans = 0
bfs()

print(ans)

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

def bfs():
    global ans
    que = deque([])
    for i in range(h):
        for j in range(w):
           if A[i][j] == "#":
               que.append((i, j))
               d[i][j] = 0
            
    while que:
        p = que.popleft()
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for k in range(len(que)):
            i = 0
            while i < 4: 
                nx = p[0] + dx[i]
                ny = p[1] + dy[i]
                if 0 <= nx < w and 0 <= ny < h and A[nx][ny] != "#" and d[nx][ny] == float("inf"):
                    d[nx][ny] = d[p[0]][p[1]] + 1
                    A[nx][ny] = "#"
                    que.append((nx, ny))
                i += 1
    ans = 0
    for i in range(h):
        for j in range(w):
            ans = max(ans, d[i][j])


h, w = map(int, input().split())
A = [list(input()) for _ in range(h)]
d = [[float("inf")] * w for _ in range(h)]
ans = 0
bfs()

print(ans)

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

def bfs():
    global ans
    que = deque([])
    black = 0
    for i in range(h):
        for j in range(w):
           if A[i][j] == "#":
               que.append((i, j))
               d[i][j] = 0
               black += 1
    if black == h*w:
        print(0)
        exit()
            
    white = True
    while white == True:
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for k in range(len(que)):
            i = 0
            while i < 4: 
                nx = que[k][0] + dx[i]
                ny = que[k][1] + dy[i]
                if 0 <= nx < w and 0 <= ny < h and A[nx][ny] != "#" and d[nx][ny] == float("inf"):
                    d[nx][ny] = d[que[k][0]][que[k][1]] + 1
                    A[nx][ny] = "#"
                i += 1
        que = deque([])
        black = 0
        for i in range(h):
            for j in range(w):
              if A[i][j] == "#":
                  que.append((i, j))
                  black += 1
                  ans = max(ans, d[i][j])
        if black == h * w:
            white = False
      
        


h, w = map(int, input().split())
A = [list(input()) for _ in range(h)]
d = [[float("inf")] * w for _ in range(h)]
ans = 0
bfs()

print(ans)

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

def bfs():
    global ans
    que = deque([])
    for i in range(h):
        for j in range(w):
           if A[i][j] == "#":
               que.append((i, j))
               d[i][j] = 0
               
    white = True
    while white == True:
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for k in range(len(que)):
            i = 0
            while i < 4: 
                nx = que[k][0] + dx[i]
                ny = que[k][1] + dy[i]
                if 0 <= nx < w and 0 <= ny < h and A[nx][ny] != "#" and d[nx][ny] == float("inf"):
                    d[nx][ny] = d[que[k][0]][que[k][1]] + 1
                    A[nx][ny] = "#"
                i += 1
        que = deque([])
        black = 0
        for i in range(h):
            for j in range(w):
              if A[i][j] == "#":
                  que.append((i, j))
                  black += 1
                  ans = max(ans, d[i][j])
        if black == h * w:
            white = False
      
        


h, w = map(int, input().split())
A = [list(input()) for _ in range(h)]
d = [[float("inf")] * w for _ in range(h)]
ans = 0
bfs()

print(ans)