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

H,W=map(int,input().split())
grid=[input() for i in range(H)]
dist=[[-1]*W for _ in range(H)]

M=deque()
for i in range(W):
    for j in range(H):
        if grid[i][j]=="#":
            M.append((i,j))
            dist[i][j]=0

def bfs(M,dist):
    d=0
    while M:
        x,y=M.popleft()
        d=dist[x][y]
        for dx,dy in ((1,0),(0,1),(-1,0),(0,-1)):
            new_h=x+dy
            new_w=y+dx
            if new_h < 0 or H <= new_h or new_w <0 or W <= new_w:
                continue
            if dist[new_h][new_w] == -1:
                dist[new_h][new_w]=d+1
                M.append((new_h,new_w))
    return d

d=bfs(M,dist)
print(d)



Fixed submission:
from collections import deque

H,W=map(int,input().split())
grid=[input() for i in range(H)]
dist=[[-1]*W for _ in range(H)]

black_cells=deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == "#":
            black_cells.append((h,w))
            dist[h][w]=0

def bfs(black_cells,dist):
        d=0
        while black_cells:
            h,w=black_cells.popleft()
            d=dist[h][w]
            for dx, dy in ((1,0),(0,1),(-1,0),(0,-1)):
                new_h=h+dy
                new_w=w+dx
                if new_h <0 or H <= new_h or new_w < 0 or W<=new_w:
                    continue
                if dist[new_h][new_w]==-1:
                    dist[new_h][new_w]=d+1
                    black_cells.append((new_h,new_w))
        return d

d=bfs(black_cells,dist)
print(d)

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

H,W=map(int,input().split())
grid=[input() for i in range(H)]
dist=[[-1]*W for _ in range(H)]

black_cells=deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == "#":
            black_cells.append((h,w))
            dist[h][w]=0

def bfs(black_cells,dist,grid):
        d=0
        while black_cells:
            h,w=black_cells.popleft()
            d=dist[h][w]
            for dx, dy in ((1,0),(0,1),(-1,0),(0,-1)):
                new_h=h+dy
                new_w=w+dx
                if new_h <0 or H <= new_h or new_w < 0 or W<=new_w:
                    continue
                if dist[new_h][new_w]==-1:
                    dist[new_h][new_w]=d+1
                    black_cells.append((new_h,new_w))
        return d

d=bfs(black_cells,dist)
print(d)


Fixed submission:
from collections import deque

H,W=map(int,input().split())
grid=[input() for i in range(H)]
dist=[[-1]*W for _ in range(H)]

black_cells=deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == "#":
            black_cells.append((h,w))
            dist[h][w]=0

def bfs(black_cells,dist):
        d=0
        while black_cells:
            h,w=black_cells.popleft()
            d=dist[h][w]
            for dx, dy in ((1,0),(0,1),(-1,0),(0,-1)):
                new_h=h+dy
                new_w=w+dx
                if new_h <0 or H <= new_h or new_w < 0 or W<=new_w:
                    continue
                if dist[new_h][new_w]==-1:
                    dist[new_h][new_w]=d+1
                    black_cells.append((new_h,new_w))
        return d

d=bfs(black_cells,dist)
print(d)

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

H,W=map(int,input().split())
grid=[input() for _ in range(H)]
dist=[[-1]*W for _ in range(H)]

M=deque()
for i in range(W):
    for j in range(H):
        if grid[i][j]=="#":
            M.append((i,j))
            dist[i][j]=0

def bfs(M,dist):
    d=0
    while M:
        x,y=M.popleft()
        d=dist[x][y]
        for dx,dy in ((1,0),(0,1),(-1,0),(0,-1)):
            new_h=x+dx
            new_w=y+dy
            if new_h < 0 or H <= new_h or new_w <0 or W <= new_w:
                continue
            if dist[new_h][new_w] == -1:
                dist[new_h][new_w]=d+1
                M.append((new_h,new_w))
    return d

d=bfs(M,dist)
print(d)

Fixed submission:
from collections import deque

H,W=map(int,input().split())
grid=[input() for i in range(H)]
dist=[[-1]*W for _ in range(H)]

black_cells=deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == "#":
            black_cells.append((h,w))
            dist[h][w]=0

def bfs(black_cells,dist):
        d=0
        while black_cells:
            h,w=black_cells.popleft()
            d=dist[h][w]
            for dx, dy in ((1,0),(0,1),(-1,0),(0,-1)):
                new_h=h+dy
                new_w=w+dx
                if new_h <0 or H <= new_h or new_w < 0 or W<=new_w:
                    continue
                if dist[new_h][new_w]==-1:
                    dist[new_h][new_w]=d+1
                    black_cells.append((new_h,new_w))
        return d

d=bfs(black_cells,dist)
print(d)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H,W=map(int,input().split())
grid=[input() for i in range(H)]
dist=[[-1]*W for _ in range(H)]

black_cells=deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == "#":
            black_cells.append((h,w))
            dist[h][w]=0

def bfs(black_cells,dist):
        d=0
        while black_cells:
            h,w=black_cells.popleft()
            d=dist[h][w]
            for dx, dy in ((1,0),(0,1),(-1,0),(0,-1)):
                new_h=h+dy
                new_w=w+dx
                if new_h <0 or H <= new_h or new_w < 0 or W<=new_w:
                    continue
                if dist[new_h][new_w]==-1:
                    dist[new_h][new_w]=d+1
                    black_cells.append((new_h,new_w))
        return d

d=bfs(black_cells,dist)
print(d)


Fixed submission:
from collections import deque

H,W=map(int,input().split())
grid=[input() for i in range(H)]
dist=[[-1]*W for _ in range(H)]

black_cells=deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == "#":
            black_cells.append((h,w))
            dist[h][w]=0

def bfs(black_cells,dist):
        d=0
        while black_cells:
            h,w=black_cells.popleft()
            d=dist[h][w]
            for dx, dy in ((1,0),(0,1),(-1,0),(0,-1)):
                new_h=h+dy
                new_w=w+dx
                if new_h <0 or H <= new_h or new_w < 0 or W<=new_w:
                    continue
                if dist[new_h][new_w]==-1:
                    dist[new_h][new_w]=d+1
                    black_cells.append((new_h,new_w))
        return d

d=bfs(black_cells,dist)
print(d)