Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from collections import deque
    import sys
    # from time import time

    # start = time()

    H, W = map(int, sys.stdin.readline().split())
    field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

    # print(time() - start)

    queue = deque([])

    for h in range(H):
        for w in range(W):
            if field[h][w] == "#":
                queue.append([h, w])

    dist_from_nearest_black = [[0] * W for i in range(H)]
    direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    def bfs():
        while len(queue) != 0:

            h, w = queue.popleft()

            for dh, dw in direction:
                next_h = h + dh
                next_w = w + dw

                if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                    continue
                elif field[next_h][next_w] == "#":
                    continue
                else:
                    dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                    field[next_h][next_w] = "#"
                    queue.append([next_h, next_w])

    bfs()

    ans = 0

    for d in dist_from_nearest_black:
        ans = max(ans, max(d))

    print(ans)
    # print(time() - start)


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque
import sys
# from time import time

# start = time()

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

# print(time() - start)

queue = deque([])

for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            queue.append([h, w])

dist_from_nearest_black = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs():
    while len(queue) != 0:

        h, w = queue.popleft()

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            elif field[next_h][next_w] == "#":
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                field[next_h][next_w] = "#"
                queue.append([next_h, next_w])


bfs()

ans = 0

for d in dist_from_nearest_black:
    ans = max(ans, max(d))

print(ans)
# print(time() - start)

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

# start = time()

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

# print(time() - start)

queue = deque([])

for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            queue.append([h, w])

dist_from_nearest_black = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs():
    while len(queue) != 0:

        h, w = queue.popleft()

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            elif field[next_h][next_w] == "#":
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                field[next_h][next_w] = "#"
                queue.append([next_h, next_w])


bfs()

ans = 0

for d in dist_from_nearest_black:
    ans = max(ans, max(d))

print(ans)
# print(time() - start)


Fixed submission:
from collections import deque
import sys
# from time import time

# start = time()

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

# print(time() - start)

queue = deque([])

for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            queue.append([h, w])

dist_from_nearest_black = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs():
    while len(queue) != 0:

        h, w = queue.popleft()

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            elif field[next_h][next_w] == "#":
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                field[next_h][next_w] = "#"
                queue.append([next_h, next_w])


bfs()

ans = 0

for d in dist_from_nearest_black:
    ans = max(ans, max(d))

print(ans)
# print(time() - start)

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

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

queue = deque([])

for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            queue.append([h, w])

dist_from_nearest_black = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs():
    while len(queue) != 0:

        h, w = queue.popleft()

        field[h][w] = "#"

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            elif field[next_h][next_w] == "#":
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                queue.append([next_h, next_w])


bfs()

ans = 0

for d in dist_from_nearest_black:
    ans = max(ans, max(d))

print(ans)


Fixed submission:
from collections import deque
import sys
# from time import time

# start = time()

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

# print(time() - start)

queue = deque([])

for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            queue.append([h, w])

dist_from_nearest_black = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs():
    while len(queue) != 0:

        h, w = queue.popleft()

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            elif field[next_h][next_w] == "#":
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                field[next_h][next_w] = "#"
                queue.append([next_h, next_w])


bfs()

ans = 0

for d in dist_from_nearest_black:
    ans = max(ans, max(d))

print(ans)
# print(time() - start)

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

sys.setrecursionlimit(10 ** 9)

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

dist_from_nearest_black = [[10 ** 9] * W for i in range(H)]
seen = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(h, w):
    if field[h][w] != "#":
        return

    queue = deque([])
    queue.append([h, w])
    dist_from_nearest_black[h][w] = 0

    while len(queue) != 0:

        h, w = queue.popleft()

        if seen[h][w] == 1:
            continue

        seen[h][w] = 1

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = min(
                    dist_from_nearest_black[h][w] + 1, dist_from_nearest_black[next_h][next_w])
                queue.append([next_h, next_w])


for h in range(H):
    for w in range(W):
        seen = [[0] * W for i in range(H)]
        bfs(h, w)

ans = 0

for d in dist_from_nearest_black:
    # print(d)
    ans = max(ans, max(d))

print(ans)


Fixed submission:
from collections import deque
import sys
# from time import time

# start = time()

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

# print(time() - start)

queue = deque([])

for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            queue.append([h, w])

dist_from_nearest_black = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs():
    while len(queue) != 0:

        h, w = queue.popleft()

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            elif field[next_h][next_w] == "#":
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                field[next_h][next_w] = "#"
                queue.append([next_h, next_w])


bfs()

ans = 0

for d in dist_from_nearest_black:
    ans = max(ans, max(d))

print(ans)
# print(time() - start)

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

sys.setrecursionlimit(10 ** 9)

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

dist_from_nearest_black = [[10 ** 9] * W for i in range(H)]
seen = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(h, w):
    if field[h][w] != "#":
        return

    queue = deque([])
    queue.append([h, w])
    dist_from_nearest_black[h][w] = 0

    while len(queue) != 0:

        h, w = queue.popleft()

        if seen[h][w] == 1:
            continue

        seen[h][w] = 1

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = min(
                    dist_from_nearest_black[h][w] + 1, dist_from_nearest_black[next_h][next_w])
                queue.append([next_h, next_w])


for h in range(H):
    for w in range(W):
        seen = [[0] * W for i in range(H)]
        bfs(h, w)

ans = 0

for d in dist_from_nearest_black:
    # print(d)
    ans = max(ans, max(d))

print(ans)


Fixed submission:
from collections import deque
import sys
# from time import time

# start = time()

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

# print(time() - start)

queue = deque([])

for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            queue.append([h, w])

dist_from_nearest_black = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs():
    while len(queue) != 0:

        h, w = queue.popleft()

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            elif field[next_h][next_w] == "#":
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                field[next_h][next_w] = "#"
                queue.append([next_h, next_w])


bfs()

ans = 0

for d in dist_from_nearest_black:
    ans = max(ans, max(d))

print(ans)
# print(time() - start)

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

setrecursionlimit(10 ** 9)

H, W = map(int, input().split())
field = [list(input()) for i in range(H)]

dist_from_nearest_black = [[10 ** 9] * W for i in range(H)]
seen = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(h, w):
    if field[h][w] != "#":
        return

    queue = deque([])
    queue.append([h, w])
    dist_from_nearest_black[h][w] = 0

    while len(queue) != 0:

        h, w = queue.popleft()

        if seen[h][w] == 1:
            continue

        seen[h][w] = 1

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = min(
                    dist_from_nearest_black[h][w] + 1, dist_from_nearest_black[next_h][next_w])
                queue.append([next_h, next_w])


for h in range(H):
    for w in range(W):
        seen = [[0] * W for i in range(H)]
        bfs(h, w)

ans = 0

for d in dist_from_nearest_black:
    # print(d)
    ans = max(ans, max(d))

print(ans)


Fixed submission:
from collections import deque
import sys
# from time import time

# start = time()

H, W = map(int, sys.stdin.readline().split())
field = [list(sys.stdin.readline().rstrip()) for i in range(H)]

# print(time() - start)

queue = deque([])

for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            queue.append([h, w])

dist_from_nearest_black = [[0] * W for i in range(H)]
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs():
    while len(queue) != 0:

        h, w = queue.popleft()

        for dh, dw in direction:
            next_h = h + dh
            next_w = w + dw

            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                continue
            elif field[next_h][next_w] == "#":
                continue
            else:
                dist_from_nearest_black[next_h][next_w] = dist_from_nearest_black[h][w] + 1
                field[next_h][next_w] = "#"
                queue.append([next_h, next_w])


bfs()

ans = 0

for d in dist_from_nearest_black:
    ans = max(ans, max(d))

print(ans)
# print(time() - start)