Buggy python submission (verdict: Time Limit Exceeded):
# https://atcoder.jp/contests/agc033/tasks/agc033_a

from heapq import heappop, heappush
H, W = map(int, input().split())
M = [list(input()) for _ in range(H)]
dist = [[10000000] * W for _ in range(H)]

def search(h, w):
    queue = [(0, (h, w))]
    while len(queue) > 0:
        cd, (ch, cw) = heappop(queue)
        for (dh, dw) in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            nh, nw = ch + dh, cw + dw
            if nh < 0 or H <= nh or nw < 0 or W <= nw:
                continue
            if dist[nh][nw] > cd + 1:
                dist[nh][nw] = cd + 1
                heappush(queue, (cd + 1, (nh, nw)))


for h in range(H):
    for w in range(W):
        if M[h][w] == '#':
            dist[h][w] = 0
            search(h, w)

ans = 0
for row in dist:
    ans = max(ans, max(row))
print(ans)

Fixed submission:
import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)

def solve():
    H, W = map(int, input().split())
    M = [[] for _ in range(H)]
    queue = deque()
    for i in range(H):
        M[i] = list(input())
        for j in range(W):
            if M[i][j] == '#':
                queue.append((i, j))
    ans = 0
    while queue:
        # 今の地点から1ステップ変化
        n = len(queue)
        flag = False
        for _ in range(n):
            cy, cx = queue.popleft()
            for dy, dx in ((-1, 0), (0, 1), (1, 0), (0, -1)):
                ny, nx = cy + dy, cx + dx
                if 0 <= nx < W and 0 <= ny < H and M[ny][nx] == '.':
                    M[ny][nx] = '#'
                    flag = True
                    queue.append((ny, nx))
        if flag:
            ans += 1
    print(ans)


if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
# https://atcoder.jp/contests/agc033/tasks/agc033_a

from heapq import heappop, heappush
H, W = map(int, input().split())
M = [list(input()) for _ in range(H)]
dist = [[-1] * W for _ in range(H)]

def search(h, w):
    queue = [(0, (h, w))]
    while len(queue) > 0:
        cd, (ch, cw) = heappop(queue)
        for (dh, dw) in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            nh, nw = ch + dh, cw + dw
            if nh < 0 or H <= nh or nw < 0 or W <= nw:
                continue
            if dist[nh][nw] == -1 or (dist[nh][nw] >= 0 and dist[nh][nw] > cd + 1):
                dist[nh][nw] = cd + 1
                heappush(queue, (cd + 1, (nh, nw)))


for h in range(H):
    for w in range(W):
        if M[h][w] == '#':
            search(h, w)

ans = 0
for row in dist:
    ans = max(ans, max(row))
print(ans)

Fixed submission:
import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)

def solve():
    H, W = map(int, input().split())
    M = [[] for _ in range(H)]
    queue = deque()
    for i in range(H):
        M[i] = list(input())
        for j in range(W):
            if M[i][j] == '#':
                queue.append((i, j))
    ans = 0
    while queue:
        # 今の地点から1ステップ変化
        n = len(queue)
        flag = False
        for _ in range(n):
            cy, cx = queue.popleft()
            for dy, dx in ((-1, 0), (0, 1), (1, 0), (0, -1)):
                ny, nx = cy + dy, cx + dx
                if 0 <= nx < W and 0 <= ny < H and M[ny][nx] == '.':
                    M[ny][nx] = '#'
                    flag = True
                    queue.append((ny, nx))
        if flag:
            ans += 1
    print(ans)


if __name__ == '__main__':
    solve()