Buggy python submission (verdict: Time Limit Exceeded):
import copy

if __name__ == '__main__':
    h, w = map(int, input().split())
    field = [list(input()) for i in range(h)]
    queue = []
    for y in range(h):
        for x in range(w):
            if field[y][x] == "#":
                queue.append((x, y))

    queue_cand = []
    d = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    cnt = 0
    while True:
        cnt += 1
        queue_cand = []
        for (x, y) in queue:
            for (dx, dy) in d:
                if 0 <= (x + dx) < w and 0 <= (y + dy) < h:
                    if field[y + dy][x + dx] == ".":
                        field[y + dy][x + dx] = cnt
                        queue_cand.append((x + dx, y + dy))

                    elif field[y + dy][x + dx] != "#":
                        if field[y + dy][x + dx] > cnt:
                            field[y + dy][x + dx] = min(field[y + dy][x + dx], cnt)
                            queue_cand.append((x + dx, y + dy))
        if len(queue_cand) == 0:
            break
        queue = copy.deepcopy(queue_cand)

    ans = 0
    for y in range(h):
        for x in range(w):
            if field[y][x] != "#":
                ans = max(ans, field[y][x])
    print(ans)


Fixed submission:
from collections import deque

h, w = map(int, input().split())

a = [[False] * (w + 2)]
for y in range(h):
    a.append([False] + [x != '#' for x in input()] + [False])
a.append([False] * (w + 2))

s = deque()
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if not a[i][j]:
            s.append((i, j))

ans = -1
while s:
    ns = deque()
    for i, j in s:
        if a[i - 1][j]:
            a[i - 1][j] = False
            ns.append((i - 1, j))
        if a[i + 1][j]:
            a[i + 1][j] = False
            ns.append((i + 1, j))
        if a[i][j - 1]:
            a[i][j - 1] = False
            ns.append((i, j - 1))
        if a[i][j + 1]:
            a[i][j + 1] = False
            ns.append((i, j + 1))

    s = ns
    ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import copy

if __name__ == '__main__':
    h, w = map(int, input().split())
    field = [list(input()) for i in range(h)]
    queue = []
    for y in range(h):
        for x in range(w):
            if field[y][x] == "#":
                queue.append((x, y))

    queue_cand = []
    d = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    cnt = 0
    while True:
        cnt += 1
        queue_cand = []
        for (x, y) in queue:
            for (dx, dy) in d:
                if 0 <= (x + dx) < w and 0 <= (y + dy) < h:
                    if field[y + dy][x + dx] == ".":
                        field[y + dy][x + dx] = cnt
                        queue_cand.append((x + dx, y + dy))

                    elif field[y + dy][x + dx] != "#":
                        if field[y + dy][x + dx] > cnt:
                            field[y + dy][x + dx] = min(field[y + dy][x + dx], cnt)
                            queue_cand.append((x + dx, y + dy))
        if len(queue_cand) == 0:
            break
        queue = copy.deepcopy(queue_cand)

    ans = 0
    for y in range(h):
        for x in range(w):
            if field[y][x] != "#":
                ans = max(ans, field[y][x])
    print(ans)


Fixed submission:
from collections import deque

h, w = map(int, input().split())

a = [[False] * (w + 2)]
for y in range(h):
    a.append([False] + [x != '#' for x in input()] + [False])
a.append([False] * (w + 2))

s = deque()
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if not a[i][j]:
            s.append((i, j))

ans = -1
while s:
    ns = deque()
    for i, j in s:
        if a[i - 1][j]:
            a[i - 1][j] = False
            ns.append((i - 1, j))
        if a[i + 1][j]:
            a[i + 1][j] = False
            ns.append((i + 1, j))
        if a[i][j - 1]:
            a[i][j - 1] = False
            ns.append((i, j - 1))
        if a[i][j + 1]:
            a[i][j + 1] = False
            ns.append((i, j + 1))

    s = ns
    ans += 1

print(ans)