Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

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

ans = 0
while Q:
    i, j, dist = Q.popleft()
    ans = max(ans, dist)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == ".":
            A[ni][nj] = "#"
            Q.append((ni, nj, dist + 1))

print(ans)


Fixed submission:
from collections import deque
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

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

ans = 0
while Q:
    i, j, dist = Q.popleft()
    ans = max(ans, dist)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == ".":
            A[ni][nj] = "#"
            Q.append((ni, nj, dist + 1))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

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

memo = [[1e10 for _ in range(W)] for _ in range(H)]
ans = 0
while Q:
    i, j, dist = Q.popleft()
    if memo[i][j] <= dist:
        continue
    memo[i][j] = dist
    ans = max(ans, dist)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == "." and dist + 1 < memo[ni][nj]:
            Q.append((ni, nj, dist + 1))

print(ans)


Fixed submission:
from collections import deque
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

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

ans = 0
while Q:
    i, j, dist = Q.popleft()
    ans = max(ans, dist)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == ".":
            A[ni][nj] = "#"
            Q.append((ni, nj, dist + 1))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

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

memo = [[1e10 for _ in range(W)] for _ in range(H)]
ans = 0
while Q:
    i, j, dist = Q.popleft()
    if memo[i][j] <= dist:
        continue
    memo[i][j] = dist
    ans = max(ans, dist)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == "." and dist + 1 < memo[ni][nj]:
            Q.append((ni, nj, dist + 1))

print(ans)


Fixed submission:
from collections import deque
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

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

ans = 0
while Q:
    i, j, dist = Q.popleft()
    ans = max(ans, dist)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == ".":
            A[ni][nj] = "#"
            Q.append((ni, nj, dist + 1))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

from collections import deque

def bfs(ii, jj):
    Q = deque()
    visited = [[False for _ in range(W)] for _ in range(H)]

    Q.append((ii, jj, 0))
    while len(Q) > 0:
        i, j, dist = Q.popleft()
        visited[i][j] = True
        if A[i][j] == "#":
            return dist

        for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
            ni, nj = i + di, j + dj
            if 0 <= ni < H and 0 <= nj < W and not visited[ni][nj]:
                Q.append((ni, nj, dist + 1))

ma = 0
for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            continue
        ma = max(ma, bfs(i, j))
print(ma)

Fixed submission:
from collections import deque
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

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

ans = 0
while Q:
    i, j, dist = Q.popleft()
    ans = max(ans, dist)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == ".":
            A[ni][nj] = "#"
            Q.append((ni, nj, dist + 1))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]
nA = [["_" for _ in range(W)] for _ in range(H)]
for i in range(H):
    nA[i] = A[i][:]

result = 0
while True:
    count = 0
    for i in range(H):
        count += A[i].count("#")

    if count == H * W:
        break

    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                    if 0 <= i + di < H and 0 <= j + dj < W:
                        nA[i + di][j + dj] = "#"

    for i in range(H):
        A[i] = nA[i][:]

    result += 1


print(result)

Fixed submission:
from collections import deque
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

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

ans = 0
while Q:
    i, j, dist = Q.popleft()
    ans = max(ans, dist)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == ".":
            A[ni][nj] = "#"
            Q.append((ni, nj, dist + 1))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]
nA = [["_" for _ in range(W)] for _ in range(H)]
for i in range(H):
    nA[i] = A[i][:]

result = 0
while True:
    count = 0
    for i in range(H):
        count += A[i].count("#")

    if count == H * W:
        break

    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                    if 0 <= i + di < H and 0 <= j + dj < W:
                        nA[i + di][j + dj] = "#"

    for i in range(H):
        A[i] = nA[i][:]

    result += 1


print(result)

Fixed submission:
from collections import deque
H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

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

ans = 0
while Q:
    i, j, dist = Q.popleft()
    ans = max(ans, dist)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == ".":
            A[ni][nj] = "#"
            Q.append((ni, nj, dist + 1))

print(ans)