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

import numpy as np

input = sys.stdin.readline


def bfs(H, W, A, dist):
    queue = deque([])
    for h, w in zip(*np.where(A == "#")):
        dist[h, w] = 0
        queue.append((h, w))
    while queue:
        h, w = queue.popleft()
        for y, x in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            next_h, next_w = h + y, w + x
            if not (0 <= next_h < H and 0 <= next_w < W):
                continue
            if dist[next_h][next_w] == -1:
                dist[next_h][next_w] = dist[h][w] + 1
                queue.append((next_h, next_w))

    return dist.max()


def main():
    H, W = map(int, input().split())
    A = np.zeros(shape=(H, W), dtype=str)
    for i in range(H):
        A[i] = list(input().rstrip())

    dist = np.full(shape=(H, W), fill_value=-1, dtype=np.int32)
    ans = bfs(H, W, A, dist)
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import deque

input = sys.stdin.readline


def bfs(H, W, A, dist):
    queue = deque([])
    for h in range(H):
        for w in range(W):
            if A[h][w] == "#":
                dist[h][w] = 0
                queue.append((h, w))
    while queue:
        h, w = queue.popleft()
        for y, x in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            next_h, next_w = h + y, w + x
            if dist[next_h][next_w] == -1:
                dist[next_h][next_w] = dist[h][w] + 1
                queue.append((next_h, next_w))


def main():
    H, W = map(int, input().split())
    A = [None] * (H + 2)
    A[0] = A[-1] = "." * (W + 2)
    for i in range(1, H + 1):
        A[i] = "".join([".", input().rstrip(), "."])

    dist = [[0] + [-1] * W + [0] for _ in range(H + 2)]
    dist[0] = dist[-1] = [0] * (W + 2)
    bfs(H + 2, W + 2, A, dist)

    ans = max(map(max, dist))
    print(ans)


if __name__ == "__main__":
    main()

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

input = sys.stdin.readline


def bfs(H, W, A, dist):
    queue = deque([])
    for h in range(H):
        for w in range(W):
            if A[h][w] == "#":
                dist[h][w] = 0
                queue.append((h, w))
    while queue:
        h, w = queue.popleft()
        for y, x in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            next_h, next_w = h + y, w + x
            if not (0 <= next_h < H and 0 <= next_w < W):
                continue
            if dist[next_h][next_w] == -1:
                dist[next_h][next_w] = dist[h][w] + 1
                queue.append((next_h, next_w))


def main():
    H, W = map(int, input().split())
    A = [None] * H
    for i in range(H):
        A[i] = input().rstrip()

    dist = [[-1] * W for _ in range(H)]
    bfs(H, W, A, dist)

    ans = max(map(max, dist))
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import deque

input = sys.stdin.readline


def bfs(H, W, A, dist):
    queue = deque([])
    for h in range(H):
        for w in range(W):
            if A[h][w] == "#":
                dist[h][w] = 0
                queue.append((h, w))
    while queue:
        h, w = queue.popleft()
        for y, x in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            next_h, next_w = h + y, w + x
            if dist[next_h][next_w] == -1:
                dist[next_h][next_w] = dist[h][w] + 1
                queue.append((next_h, next_w))


def main():
    H, W = map(int, input().split())
    A = [None] * (H + 2)
    A[0] = A[-1] = "." * (W + 2)
    for i in range(1, H + 1):
        A[i] = "".join([".", input().rstrip(), "."])

    dist = [[0] + [-1] * W + [0] for _ in range(H + 2)]
    dist[0] = dist[-1] = [0] * (W + 2)
    bfs(H + 2, W + 2, A, dist)

    ans = max(map(max, dist))
    print(ans)


if __name__ == "__main__":
    main()

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

input = sys.stdin.readline


def bfs(H, W, dist, black_induces):
    queue = deque(black_induces)
    while queue:
        h, w = queue.popleft()
        for y, x in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
            hh, ww = h + y, w + x
            if not (0 <= hh < H and 0 <= ww < W):
                continue
            if dist[hh][ww] == -1:
                dist[hh][ww] = dist[h][w] + 1
                queue.append((hh, ww))


def main():
    H, W = map(int, input().split())
    dist = [[] for _ in range(H)]
    black_induces = []
    for i in range(H):
        A = input().rstrip()
        for j, s in enumerate(A):
            if s == "#":
                dist[i].append(0)
                black_induces.append((i, j))
            else:
                dist[i].append(-1)

    bfs(H, W, dist, black_induces)

    ans = max(max(d) for d in dist)
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import deque

input = sys.stdin.readline


def bfs(H, W, A, dist):
    queue = deque([])
    for h in range(H):
        for w in range(W):
            if A[h][w] == "#":
                dist[h][w] = 0
                queue.append((h, w))
    while queue:
        h, w = queue.popleft()
        for y, x in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            next_h, next_w = h + y, w + x
            if dist[next_h][next_w] == -1:
                dist[next_h][next_w] = dist[h][w] + 1
                queue.append((next_h, next_w))


def main():
    H, W = map(int, input().split())
    A = [None] * (H + 2)
    A[0] = A[-1] = "." * (W + 2)
    for i in range(1, H + 1):
        A[i] = "".join([".", input().rstrip(), "."])

    dist = [[0] + [-1] * W + [0] for _ in range(H + 2)]
    dist[0] = dist[-1] = [0] * (W + 2)
    bfs(H + 2, W + 2, A, dist)

    ans = max(map(max, dist))
    print(ans)


if __name__ == "__main__":
    main()

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

input = sys.stdin.readline


def bfs(H, W, dist):
    queue = deque()
    for h in range(H):
        for w in range(W):
            if dist[h][w] == 0:
                queue.append((h, w))
    while queue:
        h, w = queue.popleft()
        for y, x in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
            hh, ww = h + y, w + x
            if not (0 <= hh < H and 0 <= ww < W):
                continue
            if dist[hh][ww] == -1:
                dist[hh][ww] = dist[h][w] + 1
                queue.append((hh, ww))


def main():
    H, W = map(int, input().split())
    A = [None] * H
    for i in range(H):
        A[i] = input().rstrip()

    distance = [[0 if A[h][w] == "#" else -1
        for w in range(W)] for h in range(H)]

    bfs(H, W, distance)

    ans = max(max(d) for d in distance)
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import deque

input = sys.stdin.readline


def bfs(H, W, A, dist):
    queue = deque([])
    for h in range(H):
        for w in range(W):
            if A[h][w] == "#":
                dist[h][w] = 0
                queue.append((h, w))
    while queue:
        h, w = queue.popleft()
        for y, x in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            next_h, next_w = h + y, w + x
            if dist[next_h][next_w] == -1:
                dist[next_h][next_w] = dist[h][w] + 1
                queue.append((next_h, next_w))


def main():
    H, W = map(int, input().split())
    A = [None] * (H + 2)
    A[0] = A[-1] = "." * (W + 2)
    for i in range(1, H + 1):
        A[i] = "".join([".", input().rstrip(), "."])

    dist = [[0] + [-1] * W + [0] for _ in range(H + 2)]
    dist[0] = dist[-1] = [0] * (W + 2)
    bfs(H + 2, W + 2, A, dist)

    ans = max(map(max, dist))
    print(ans)


if __name__ == "__main__":
    main()