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

h, w = map(int, input().split())
Black = deque([])
White = deque([])
Black_grid = [[-1 for _ in range(w)] for _ in range(h)]

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([i,j])
            Black_grid[j][i] = 0
        else:
            White.append([i,j])

Move = [[-1,0],[1,0],[0,-1],[0,1]]


while Black:
    now = Black.popleft()
    x = now[0]
    y = now[1]
    for move in Move:
        x_next = x + move[0]
        y_next = y + move[1]
        if x_next < 0 or x_next >= w or y_next < 0 or y_next >= h:
            continue
        elif Black_grid[y_next][x_next] >= 0:
            continue
        else:
            Black.append([x_next, y_next])
            Black_grid[y_next][x_next] = Black_grid[y][x] + 1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, Black_grid[i][j])
    
print(ans)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
Black = deque([])
Black_grid = [[-1 for _ in range(w)] for _ in range(h)]

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([j,i])
            Black_grid[i][j] = 0

Move = [[-1,0],[1,0],[0,-1],[0,1]]

while Black:
    now = Black.popleft()
    x = now[0]
    y = now[1]
    for move in Move:
        x_next = x + move[0]
        y_next = y + move[1]
        if x_next < 0 or x_next >= w or y_next < 0 or y_next >= h:
            continue
        elif Black_grid[y_next][x_next] >= 0:
            continue
        else:
            Black.append([x_next, y_next])
            Black_grid[y_next][x_next] = Black_grid[y][x] + 1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, Black_grid[i][j])
    
print(ans)

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

h, w = map(int, input().split())
Black = []
White = deque()

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([i,j])
        else:
            White.append([i,j])

longest = 0
while White:
    white = White.popleft()
    white_x = white[0]
    white_y = white[1]
    shortest_per_white = 10**4
    for black in Black:
        length = abs(black[0]-white_x) + abs(black[1]-white_y)
        shortest_per_white = min(shortest_per_white, length)
    longest = max(shortest_per_white, longest)

print(longest)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
Black = deque([])
Black_grid = [[-1 for _ in range(w)] for _ in range(h)]

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([j,i])
            Black_grid[i][j] = 0

Move = [[-1,0],[1,0],[0,-1],[0,1]]

while Black:
    now = Black.popleft()
    x = now[0]
    y = now[1]
    for move in Move:
        x_next = x + move[0]
        y_next = y + move[1]
        if x_next < 0 or x_next >= w or y_next < 0 or y_next >= h:
            continue
        elif Black_grid[y_next][x_next] >= 0:
            continue
        else:
            Black.append([x_next, y_next])
            Black_grid[y_next][x_next] = Black_grid[y][x] + 1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, Black_grid[i][j])
    
print(ans)

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

h, w = map(int, input().split())
Black = []
White = deque()

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([i,j])
        else:
            White.append([i,j])

longest = 0
while White:
    white = White.popleft()
    white_x = white[0]
    white_y = white[1]
    shortest_per_white = 10**4
    for black in Black:
        length = abs(black[0]-white_x) + abs(black[1]-white_y)
        shortest_per_white = min(shortest_per_white, length)
    longest = max(shortest_per_white, longest)

print(longest)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
Black = deque([])
Black_grid = [[-1 for _ in range(w)] for _ in range(h)]

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([j,i])
            Black_grid[i][j] = 0

Move = [[-1,0],[1,0],[0,-1],[0,1]]

while Black:
    now = Black.popleft()
    x = now[0]
    y = now[1]
    for move in Move:
        x_next = x + move[0]
        y_next = y + move[1]
        if x_next < 0 or x_next >= w or y_next < 0 or y_next >= h:
            continue
        elif Black_grid[y_next][x_next] >= 0:
            continue
        else:
            Black.append([x_next, y_next])
            Black_grid[y_next][x_next] = Black_grid[y][x] + 1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, Black_grid[i][j])
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = map(int, input().split())
Black = []
White = []

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([i,j])
        else:
            White.append([i,j])

longest = 0
for white in White:
    white_x = white[0]
    white_y = white[1]
    shortest_per_white = 10**4
    for black in Black:
        length = abs(black[0]-white_x) + abs(black[1]-white_y)
        shortest_per_white = min(shortest_per_white, length)
    longest = max(shortest_per_white, longest)

print(longest)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
Black = deque([])
Black_grid = [[-1 for _ in range(w)] for _ in range(h)]

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([j,i])
            Black_grid[i][j] = 0

Move = [[-1,0],[1,0],[0,-1],[0,1]]

while Black:
    now = Black.popleft()
    x = now[0]
    y = now[1]
    for move in Move:
        x_next = x + move[0]
        y_next = y + move[1]
        if x_next < 0 or x_next >= w or y_next < 0 or y_next >= h:
            continue
        elif Black_grid[y_next][x_next] >= 0:
            continue
        else:
            Black.append([x_next, y_next])
            Black_grid[y_next][x_next] = Black_grid[y][x] + 1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, Black_grid[i][j])
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = map(int, input().split())
Black = []
White = []

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([i,j])
        else:
            White.append([i,j])

longest = 0
for white in White:
    white_x = white[0]
    white_y = white[1]
    shortest_per_white = 10**4
    for black in Black:
        length = abs(black[0]-white_x) + abs(black[1]-white_y)
        shortest_per_white = min(shortest_per_white, length)
    longest = max(shortest_per_white, longest)

print(longest)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
Black = deque([])
Black_grid = [[-1 for _ in range(w)] for _ in range(h)]

for i in range(h):
    a = str(input())
    for j in range(w):
        if a[j] == '#':
            Black.append([j,i])
            Black_grid[i][j] = 0

Move = [[-1,0],[1,0],[0,-1],[0,1]]

while Black:
    now = Black.popleft()
    x = now[0]
    y = now[1]
    for move in Move:
        x_next = x + move[0]
        y_next = y + move[1]
        if x_next < 0 or x_next >= w or y_next < 0 or y_next >= h:
            continue
        elif Black_grid[y_next][x_next] >= 0:
            continue
        else:
            Black.append([x_next, y_next])
            Black_grid[y_next][x_next] = Black_grid[y][x] + 1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, Black_grid[i][j])
    
print(ans)