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


fin = sys.stdin.readline
H, W = [int(elem) for elem in fin().split()]
A_list = [fin().rstrip("\n") for _ in range(H)]
assert len(A_list[0]) == W

distances = [[float('inf')] * W for _ in range(H)]
que = deque((i, j, 0) for i in range(H) for j in range(W) if A_list[i][j] == "#")
max_dist = 0
while que:
    i, j, dist = que.popleft()
    for next_i, next_j in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
        if not (0 <= next_i < H and 0 <= next_j < W):
            continue
        elif distances[next_i][next_j] < float('inf'):
            continue
        elif A_list[next_i][next_j] == "#":
            continue
        else:
            que.append((next_i, next_j, dist + 1))
            distances[next_i][next_j] = dist + 1
            max_dist = max(max_dist, dist + 1)
print(max_dist)


Fixed submission:
import sys
from collections import deque


fin = sys.stdin.readline
H, W = [int(elem) for elem in fin().split()]
A_list = [fin().rstrip("\n") for _ in range(H)]
assert len(A_list[0]) == W

visited = [[False] * W for _ in range(H)]
que = deque((i, j, 0) for i in range(H) for j in range(W) if A_list[i][j] == "#")
max_dist = 0
while que:
    i, j, dist = que.popleft()
    for next_i, next_j in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
        if not (0 <= next_i < H and 0 <= next_j < W):
            continue
        elif visited[next_i][next_j]:
            continue
        elif A_list[next_i][next_j] == "#":
            continue
        else:
            que.append((next_i, next_j, dist + 1))
            visited[next_i][next_j] = True
            max_dist = max(max_dist, dist + 1)
print(max_dist)

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


fin = sys.stdin.readline
H, W = [int(elem) for elem in fin().split()]
A_list = [list(fin().rstrip("\n")) for _ in range(H)]
assert len(A_list[0]) == W

distances = [[float('inf')] * W for _ in range(H)]
que = deque((i, j, 0) for i in range(H) for j in range(W) if A_list[i][j] == "#")
max_dist = 0
while que:
    i, j, dist = que.popleft()
    for next_i, next_j in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
        if not (0 <= next_i < H and 0 <= next_j < W):
            continue
        elif distances[next_i][next_j] < float('inf'):
            continue
        elif A_list[next_i][next_j] == "#":
            continue
        else:
            que.append((next_i, next_j, dist + 1))
            distances[next_i][next_j] = dist + 1
            max_dist = max(max_dist, dist + 1)
print(max_dist)


Fixed submission:
import sys
from collections import deque


fin = sys.stdin.readline
H, W = [int(elem) for elem in fin().split()]
A_list = [fin().rstrip("\n") for _ in range(H)]
assert len(A_list[0]) == W

visited = [[False] * W for _ in range(H)]
que = deque((i, j, 0) for i in range(H) for j in range(W) if A_list[i][j] == "#")
max_dist = 0
while que:
    i, j, dist = que.popleft()
    for next_i, next_j in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
        if not (0 <= next_i < H and 0 <= next_j < W):
            continue
        elif visited[next_i][next_j]:
            continue
        elif A_list[next_i][next_j] == "#":
            continue
        else:
            que.append((next_i, next_j, dist + 1))
            visited[next_i][next_j] = True
            max_dist = max(max_dist, dist + 1)
print(max_dist)