Buggy python submission (verdict: Runtime Error):
from collections import deque
def main():
    h, w = map(int, input().split())
    A = [list(input()) for _ in range(h)]
    B = [(i, j) for i, a in enumerate(A) for j, b in enumerate(a) if b == '#']
    dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    stack = deque(B)
    colored = len(B)
    tot = h * w
    count = 0
    while colored < tot:
        next = deque([])
        while stack:
            y, x = stack.popleft()
            for dx, dy in dxy:
                X = x + dx
                Y = y + dy
                if X < 0 or X >= w or Y < 0 or Y >= h:
                    continue
                if A[X][Y] == '.':
                    next.append((X, Y))
                    A[X][Y] = '#'
        stack = next
        count += 1
        colored += len(next)
    print(count)

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque
def main():
    h, w = map(int, input().split())
    A = [list(input()) for _ in range(h)]
    B = [(i, j) for i, a in enumerate(A) for j, b in enumerate(a) if b == '#']
    dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    stack = deque(B)
    colored = len(B)
    tot = h * w
    count = 0
    while colored < tot:
        next = deque([])
        while stack:
            y, x = stack.popleft()
            for dx, dy in dxy:
                X = x + dx
                Y = y + dy
                if X < 0 or X >= w or Y < 0 or Y >= h:
                    continue
                if A[Y][X] == '.':
                    next.append((Y, X))
                    A[Y][X] = '#'
        stack = next
        count += 1
        colored += len(next)
    print(count)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    H, W = list(map(int, input().split()))
    A = [['!'] * (W + 2)] + [['!', ] + list(input()) + ['!', ] for _ in range(H)] + [['!'] * (W + 2)]
    printl = lambda A:[print(''.join(a)) for a in A]
    INF = 100000
    ans = 0
    dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    for h in range(1, H + 1):
        for w in range(1, W + 1):
            if A[h][w] == '#': continue
            stack = deque([[h, w], ])
            count = 0
            visited = [[0] * (W + 2) for _ in range(H + 2)]
            score = [[INF] * (W + 2) for _ in range(H + 2)]
            score[h][w] = 0
            f = False
            while stack:
                x, y = stack.popleft()
                if visited[x][y]:
                    continue
                else:
                    visited[x][y] = 1
                for dx, dy in dxy:
                    X, Y = x + dx, y + dy
                    if A[X][Y] == '#':
                        f = True
                        count = score[x][y] + 1
                        break
                    elif A[X][Y] == '.' and visited[X][Y] == 0:
                        stack.append([X, Y])
                        score[X][Y] = min(score[X][Y], score[x][y] + 1)
                if f:
                    break
            ans = max(ans, count)
    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
from collections import deque
def main():
    h, w = map(int, input().split())
    A = [list(input()) for _ in range(h)]
    B = [(i, j) for i, a in enumerate(A) for j, b in enumerate(a) if b == '#']
    dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    stack = deque(B)
    colored = len(B)
    tot = h * w
    count = 0
    while colored < tot:
        next = deque([])
        while stack:
            y, x = stack.popleft()
            for dx, dy in dxy:
                X = x + dx
                Y = y + dy
                if X < 0 or X >= w or Y < 0 or Y >= h:
                    continue
                if A[Y][X] == '.':
                    next.append((Y, X))
                    A[Y][X] = '#'
        stack = next
        count += 1
        colored += len(next)
    print(count)

if __name__ == '__main__':
    main()