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


h, w = map(int, input().split())
grid = [input() for _ in range(h)]
cost = [[-1] * w for _ in range(h)]
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]

q = deque()
for i in range(h):
    for j in range(w):
        if grid[i][j] == "#":
            q.append((i, j))
            cost[i][j] = 0

while q:
    x, y = q.popleft()
    for dx, dy in nb:
        nx = x + dx
        ny = y + dy
        if 0 <= nx and nx < h and 0 <= ny and ny < w and cost[nx][ny] == -1:
            cost[nx][ny] = cost[x][y] + 1
            q.append((nx, ny))


print(max([max(c) for c in cost]))

Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
A = [input() for i in range(H)]
q = deque()
c = [[-1] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append((i, j))
            c[i][j] = 0
D = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while q:
    y, x = q.popleft()
    for dy, dx in D:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and c[ny][nx] == -1:
            q.append((ny, nx))
            c[ny][nx] = c[y][x]+1
print(max([max(l) for l in c]))

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


def input():
    return sys.stdin.readline()[:-1]


h, w = map(int, input().split())
grid = [input() for _ in range(h)]
cost = [[float("inf")] * w for _ in range(h)]
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]

q = deque()
for i in range(h):
    for j in range(w):
        if grid[i][j] == "#":
            q.append((i, j))
            cost[i][j] = 0

while q:
    x, y = q.popleft()
    for dx, dy in nb:
        nx = x + dx
        ny = y + dy
        if 0 <= nx and nx < h and 0 <= ny and ny < w and cost[nx][ny] == float("inf"):
            cost[nx][ny] = cost[x][y] + 1
            q.append((nx, ny))


print(max([max(c) for c in cost]))


Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
A = [input() for i in range(H)]
q = deque()
c = [[-1] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append((i, j))
            c[i][j] = 0
D = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while q:
    y, x = q.popleft()
    for dy, dx in D:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and c[ny][nx] == -1:
            q.append((ny, nx))
            c[ny][nx] = c[y][x]+1
print(max([max(l) for l in c]))

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


h, w = map(int, input().split())
grid = [input() for _ in range(h)]
cost = [[float("inf")] * w for _ in range(h)]
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]

q = deque()
for i in range(h):
    for j in range(w):
        if grid[i][j] == "#":
            q.append((i, j))
            cost[i][j] = 0

while q:
    x, y = q.popleft()
    for dx, dy in nb:
        nx = x + dx
        ny = y + dy
        if 0 <= nx and nx < h and 0 <= ny and ny < w and cost[nx][ny] == float("inf"):
            cost[nx][ny] = cost[x][y] + 1
            q.append((nx, ny))


print(max([max(c) for c in cost]))

Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
A = [input() for i in range(H)]
q = deque()
c = [[-1] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append((i, j))
            c[i][j] = 0
D = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while q:
    y, x = q.popleft()
    for dy, dx in D:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and c[ny][nx] == -1:
            q.append((ny, nx))
            c[ny][nx] = c[y][x]+1
print(max([max(l) for l in c]))

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


def bfs():
    q = deque()
    for i in range(h):
        for j in range(w):
            if grid[i][j] == "#":
                q.append((i, j))
                cost[i][j] = 0

    while q:
        x, y = q.popleft()
        for dx, dy in nb:
            nx = x + dx
            ny = y + dy
            if 0 <= nx and nx < h and 0 <= ny and ny < w and cost[nx][ny] == float("inf"):
                cost[nx][ny] = cost[x][y] + 1
                q.append((nx, ny))


h, w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
cost = [[float("inf")] * w for _ in range(h)]
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]

bfs()

print(max([max(c) for c in cost]))

Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
A = [input() for i in range(H)]
q = deque()
c = [[-1] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append((i, j))
            c[i][j] = 0
D = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while q:
    y, x = q.popleft()
    for dy, dx in D:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and c[ny][nx] == -1:
            q.append((ny, nx))
            c[ny][nx] = c[y][x]+1
print(max([max(l) for l in c]))

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


def bfs():
    q = deque()
    for i in range(h):
        for j in range(w):
            if grid[i][j] == "#":
                q.append((i, j))
                cost[i][j] = 0

    while q:
        x, y = q.popleft()
        for dx, dy in nb:
            nx = x + dx
            ny = y + dy
            if 0 <= nx and nx < h and 0 <= ny and ny < w and cost[nx][ny] == float("inf"):
                cost[nx][ny] = cost[x][y] + 1
                q.append((nx, ny))


h, w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
cost = [[float("inf")] * w for _ in range(h)]
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]

bfs()

print(max([max(c) for c in cost]))

Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
A = [input() for i in range(H)]
q = deque()
c = [[-1] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append((i, j))
            c[i][j] = 0
D = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while q:
    y, x = q.popleft()
    for dy, dx in D:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and c[ny][nx] == -1:
            q.append((ny, nx))
            c[ny][nx] = c[y][x]+1
print(max([max(l) for l in c]))

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


def bfs(sx, sy):
    q = deque([(sx, sy)])
    visited = [[False] * w for _ in range(h)]
    cost[sx][sy] = 0
    visited[sx][sy] = True
    while q:
        x, y = q.popleft()
        for dx, dy in nb:
            nx = x + dx
            ny = y + dy
            if 0 <= nx and nx < h and 0 <= ny and ny < w and not (visited[nx][ny]):
                cost[nx][ny] = min(cost[x][y] + 1, cost[nx][ny])
                q.append((nx, ny))
                visited[nx][ny] = True


h, w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
cost = [[float("inf")] * w for _ in range(h)]
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]
ans = -1

for i in range(h):
    for j in range(w):
        if grid[i][j] == "#":
            bfs(i, j)

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


Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
A = [input() for i in range(H)]
q = deque()
c = [[-1] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append((i, j))
            c[i][j] = 0
D = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while q:
    y, x = q.popleft()
    for dy, dx in D:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and c[ny][nx] == -1:
            q.append((ny, nx))
            c[ny][nx] = c[y][x]+1
print(max([max(l) for l in c]))

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


def bfs(sx, sy):
    q = deque([(sx, sy)])
    visited = [[False] * w for _ in range(h)]
    cost[sx][sy] = 0
    visited[sx][sy] = True
    while q:
        x, y = q.popleft()
        for dx, dy in nb:
            nx = x + dx
            ny = y + dy
            if 0 <= nx and nx < h and 0 <= ny and ny < w and not (visited[nx][ny]):
                cost[nx][ny] = min(cost[x][y] + 1, cost[nx][ny])
                q.append((nx, ny))
                visited[nx][ny] = True


h, w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
cost = [[float("inf")] * w for _ in range(h)]
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]
ans = -1

for i in range(h):
    for j in range(w):
        if grid[i][j] == "#":
            bfs(i, j)

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


Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
A = [input() for i in range(H)]
q = deque()
c = [[-1] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append((i, j))
            c[i][j] = 0
D = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while q:
    y, x = q.popleft()
    for dy, dx in D:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and c[ny][nx] == -1:
            q.append((ny, nx))
            c[ny][nx] = c[y][x]+1
print(max([max(l) for l in c]))

Buggy python submission (verdict: Time Limit Exceeded):
def bfs(sx, sy):
    for dx, dy in nb:
        nx = sx + dx
        ny = sy + dy
        if 0 <= nx and nx < h and 0 <= ny and ny < w:
            if grid[nx][ny] == "#":
                darker[sx][sy] = True
                break
 
 
h, w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]
ans = 0
 
while True:
    darker = [[False] * w for _ in range(h)]
    flag = False
    for i in range(h):
        for j in range(w):
            if grid[i][j] == ".":
                bfs(i, j)
    for i in range(h):
        for j in range(w):
            if darker[i][j]:
                grid[i][j] = "#"
            flag = flag or darker[i][j]
    if not (flag):
        break
    ans += 1
print(ans)

Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
A = [input() for i in range(H)]
q = deque()
c = [[-1] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append((i, j))
            c[i][j] = 0
D = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while q:
    y, x = q.popleft()
    for dy, dx in D:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and c[ny][nx] == -1:
            q.append((ny, nx))
            c[ny][nx] = c[y][x]+1
print(max([max(l) for l in c]))

Buggy python submission (verdict: Time Limit Exceeded):
def bfs(sx, sy):
    for dx, dy in nb:
        nx = sx + dx
        ny = sy + dy
        if 0 <= nx and nx < h and 0 <= ny and ny < w:
            if grid[nx][ny] == "#":
                darker[sx][sy] = True
                break


h, w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]
ans = 0

while True:
    darker = [[False] * w for _ in range(h)]
    flag = False
    for i in range(h):
        for j in range(w):
            if grid[i][j] == ".":
                bfs(i, j)
    for i in range(h):
        for j in range(w):
            if darker[i][j]:
                grid[i][j] = "#"
            flag = flag or darker[i][j]
    if not (flag):
        break
    ans += 1
print(ans)

Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
A = [input() for i in range(H)]
q = deque()
c = [[-1] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append((i, j))
            c[i][j] = 0
D = [[0, 1], [1, 0], [0, -1], [-1, 0]]
while q:
    y, x = q.popleft()
    for dy, dx in D:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and c[ny][nx] == -1:
            q.append((ny, nx))
            c[ny][nx] = c[y][x]+1
print(max([max(l) for l in c]))