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


def resolve():
    H, W = map(int, input().split())
    A, q = [], deque([])
    for i in range(H):
        A.append(list(input()))
        if '#' in A[i]:
            for j in range(W):
                if A[i][j] == '#':
                    q.append((i, j))

    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    cnt = -1

    # 1回目で各黒マスを拡張して，2回目以降は拡張した黒マスから拡張してく
    nq = deque([])
    while q:
        y, x = q.popleft()
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if 0 <= ny < H and 0 <= nx < W and A[ny][nx] == '.':
                nq.append((ny, nx))
                A[ny][nx] = '#'
        if q == deque([]):
            q = nq
            nq = deque([])
            cnt += 1
    print(cnt)


if __name__ == "__main__":
    resolve()

Fixed submission:
from collections import deque


def resolve():
    H, W = map(int, input().split())
    A, q = [], deque([])
    for i in range(H):
        A.append(list(input()))
        if '#' in A[i]:
            for j in range(W):
                if A[i][j] == '#':
                    q.append((i, j))

    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    cnt = -1

    # 1回目で各黒マスを拡張して，2回目以降は拡張した黒マスから拡張してく
    nq = deque([])
    while q:
        y, x = q.popleft()
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if 0 <= ny < H and 0 <= nx < W and A[ny][nx] == '.':
                nq.append((ny, nx))
                A[ny][nx] = '#'
        if q == deque([]):
            q = nq
            nq = deque([])
            cnt += 1
    print(cnt)


if __name__ == "__main__":
    resolve()

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


H, W = map(int, input().split())
A, white = [], []
for i in range(H):
    A.append(list(input()))
    if '.' in A[i]:
        for j in range(W):
            if A[i][j] == '.':
                white.append([i, j])

dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
cnt = 0

# 各白マスから黒マスまでの最短距離を探索し，その最大値を出力
for sy, sx in white:
    dis = [[float('inf')] * W for _ in range(H)]
    dis[sy][sx] = 0
    q = deque([(sy, sx)])
    update = False
    while q:
        y, x = q.popleft()
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if (1
                    and 0 <= ny < H
                    and 0 <= nx < W
                    and dis[ny][nx] == float('inf')):
                dis[ny][nx] = dis[y][x] + 1
                q.append((ny, nx))
                if A[ny][nx] == '#':
                    cnt = max(dis[ny][nx], cnt)
                    update = True
        if update:
            break
print(cnt)

Fixed submission:
from collections import deque


def resolve():
    H, W = map(int, input().split())
    A, q = [], deque([])
    for i in range(H):
        A.append(list(input()))
        if '#' in A[i]:
            for j in range(W):
                if A[i][j] == '#':
                    q.append((i, j))

    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    cnt = -1

    # 1回目で各黒マスを拡張して，2回目以降は拡張した黒マスから拡張してく
    nq = deque([])
    while q:
        y, x = q.popleft()
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if 0 <= ny < H and 0 <= nx < W and A[ny][nx] == '.':
                nq.append((ny, nx))
                A[ny][nx] = '#'
        if q == deque([]):
            q = nq
            nq = deque([])
            cnt += 1
    print(cnt)


if __name__ == "__main__":
    resolve()

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


def resolve():
    H, W = map(int, input().split())
    A, white = [], []
    for i in range(H):
        A.append(list(input()))
        if '.' in A[i]:
            for j in range(W):
                if A[i][j] == '.':
                    white.append([i, j])

    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    cnt = 0

    # 各白マスから黒マスまでの最短距離を探索し，その最大値を出力
    for sy, sx in white:
        dis = [[float('inf')] * W for _ in range(H)]
        dis[sy][sx] = 0
        q = deque([(sy, sx)])
        update = False
        while q:
            y, x = q.popleft()
            for i in range(4):
                ny = y + dy[i]
                nx = x + dx[i]
                if (1
                        and 0 <= ny < H
                        and 0 <= nx < W
                        and dis[ny][nx] == float('inf')):
                    dis[ny][nx] = dis[y][x] + 1
                    q.append((ny, nx))
                    if A[ny][nx] == '#':
                        cnt = max(dis[ny][nx], cnt)
                        update = True
            if update:
                break
    print(cnt)


if __name__ == "__main__":
    resolve()

Fixed submission:
from collections import deque


def resolve():
    H, W = map(int, input().split())
    A, q = [], deque([])
    for i in range(H):
        A.append(list(input()))
        if '#' in A[i]:
            for j in range(W):
                if A[i][j] == '#':
                    q.append((i, j))

    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    cnt = -1

    # 1回目で各黒マスを拡張して，2回目以降は拡張した黒マスから拡張してく
    nq = deque([])
    while q:
        y, x = q.popleft()
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if 0 <= ny < H and 0 <= nx < W and A[ny][nx] == '.':
                nq.append((ny, nx))
                A[ny][nx] = '#'
        if q == deque([]):
            q = nq
            nq = deque([])
            cnt += 1
    print(cnt)


if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Runtime Error):
def resolve():
    H, W = map(int, input().split())
    A, white = [], []
    for i in range(H):
        A.append(list(input()))
        if '.' in A[i]:
            for j in range(W):
                if A[i][j] == '.':
                    white.append([i, j])

    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    cnt = 0

    # 各白マスから黒マスまでの最短距離を探索し，その最大値を出力
    for sy, sx in white:
        dis = [[float('inf')] * W for _ in range(H)]
        dis[sy][sx] = 0
        q = deque([(sy, sx)])
        update = False
        while q:
            y, x = q.popleft()
            for i in range(4):
                ny = y + dy[i]
                nx = x + dx[i]
                if (1
                        and 0 <= ny < H
                        and 0 <= nx < W
                        and dis[ny][nx] == float('inf')):
                    dis[ny][nx] = dis[y][x] + 1
                    q.append((ny, nx))
                    if A[ny][nx] == '#':
                        cnt = max(dis[ny][nx], cnt)
                        update = True
            if update:
                break
    print(cnt)


if __name__ == "__main__":
    resolve()

Fixed submission:
from collections import deque


def resolve():
    H, W = map(int, input().split())
    A, q = [], deque([])
    for i in range(H):
        A.append(list(input()))
        if '#' in A[i]:
            for j in range(W):
                if A[i][j] == '#':
                    q.append((i, j))

    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    cnt = -1

    # 1回目で各黒マスを拡張して，2回目以降は拡張した黒マスから拡張してく
    nq = deque([])
    while q:
        y, x = q.popleft()
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if 0 <= ny < H and 0 <= nx < W and A[ny][nx] == '.':
                nq.append((ny, nx))
                A[ny][nx] = '#'
        if q == deque([]):
            q = nq
            nq = deque([])
            cnt += 1
    print(cnt)


if __name__ == "__main__":
    resolve()