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

h, w = [int(_) for _ in input().split()]
G = [list(input()) for _ in range(h)]

que = deque()
for i in range(h):
    for j in range(w):
        if G[i][j] == '#':
            que.append((i, j, 0))
cnt = 0
while que:
    i, j, c = que.popleft()

    for ni, nj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        if i + ni < 0 or j + nj < 0: continue
        if i + ni >= h or j + nj >= w: continue

        if G[i + ni][j + nj] == '.':
            G[i + ni][j + nj] = '#'
            que.append((i + ni, j + nj, c + 1))
            cnt = max(cnt, c + 1)
print(cnt)


Fixed submission:
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]

from collections import deque

lst = deque()

for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            lst.append((i, j, 0))


def chg(i, j):
    if i < 0 or j < 0 or H <= i or W <= j: return False
    if A[i][j] == '.':
        A[i][j] = '#'
        return True
    return False


ans = 0
while lst:
    h, w, v = lst.popleft()
    ans = max(v, ans)
    for i in range(-1, 2):
        for j in range(-1, 2):
            if i != 0 and j != 0: continue

            if chg(h + i, w + j):
                lst.append((h + i, w + j, v + 1))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            A[i][j] = 0
        else:
            A[i][j] = -1


def chg(i, j, c):
    if i < 0 or j < 0 or H <= i or W <= j: return False
    if A[i][j] >= 0:
        return False

    A[i][j] = c
    return True


def f(c):
    edit = 0
    for i in range(H):
        for j in range(W):
            if 0 <= A[i][j] < c:
                A[i][j] = c
                edit |= chg(i - 1, j, c)
                edit |= chg(i + 1, j, c)
                edit |= chg(i, j - 1, c)
                edit |= chg(i, j + 1, c)
    return edit


ans = 0
while 1:
    if f(ans + 1):
        ans += 1
    else:
        break
print(ans)


Fixed submission:
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]

from collections import deque

lst = deque()

for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            lst.append((i, j, 0))


def chg(i, j):
    if i < 0 or j < 0 or H <= i or W <= j: return False
    if A[i][j] == '.':
        A[i][j] = '#'
        return True
    return False


ans = 0
while lst:
    h, w, v = lst.popleft()
    ans = max(v, ans)
    for i in range(-1, 2):
        for j in range(-1, 2):
            if i != 0 and j != 0: continue

            if chg(h + i, w + j):
                lst.append((h + i, w + j, v + 1))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            A[i][j] = 0
        else:
            A[i][j] = -1


def chg(i, j, c):
    if i < 0 or j < 0 or H <= i or W <= j: return
    A[i][j] = A[i][j] if A[i][j] >= 0 else c


def f(c):
    for i in range(H):
        for j in range(W):
            if 0 <= A[i][j] < c:
                A[i][j] = c
                chg(i - 1, j, c)
                chg(i + 1, j, c)
                chg(i, j - 1, c)
                chg(i, j + 1, c)


def chk():
    for i in range(H):
        for j in range(W):
            if A[i][j] == -1: return False
    return True


ans = 0
while 1:
    if chk():
        break
    ans += 1
    f(ans)
print(ans)


Fixed submission:
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]

from collections import deque

lst = deque()

for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            lst.append((i, j, 0))


def chg(i, j):
    if i < 0 or j < 0 or H <= i or W <= j: return False
    if A[i][j] == '.':
        A[i][j] = '#'
        return True
    return False


ans = 0
while lst:
    h, w, v = lst.popleft()
    ans = max(v, ans)
    for i in range(-1, 2):
        for j in range(-1, 2):
            if i != 0 and j != 0: continue

            if chg(h + i, w + j):
                lst.append((h + i, w + j, v + 1))
print(ans)

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

sys.setrecursionlimit(10 ** 7)

INF = 10 ** 9

H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]

s = []
d = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            s.append((i, j))
        else:
            d.append((i, j))

ans = 0
for di in d:
    m = INF
    for si in s:
        m = min(m, abs(di[0] - si[0]) + abs(di[1] - si[1]))
    ans = max(ans, m)
print(ans)


Fixed submission:
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]

from collections import deque

lst = deque()

for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            lst.append((i, j, 0))


def chg(i, j):
    if i < 0 or j < 0 or H <= i or W <= j: return False
    if A[i][j] == '.':
        A[i][j] = '#'
        return True
    return False


ans = 0
while lst:
    h, w, v = lst.popleft()
    ans = max(v, ans)
    for i in range(-1, 2):
        for j in range(-1, 2):
            if i != 0 and j != 0: continue

            if chg(h + i, w + j):
                lst.append((h + i, w + j, v + 1))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]


def f():
    B = [['.'] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if A[i][j] == '#':
                B[i][j] = '#'
                if i > 0:
                    B[i - 1][j] = '#'
                if j > 0:
                    B[i][j - 1] = '#'
                if i < H - 1:
                    B[i + 1][j] = '#'
                if j < W - 1:
                    B[i][j + 1] = '#'
    return B


def c():
    for i in range(H):
        for j in range(W):
            if A[i][j] == '.': return False
    return True


ans = 0
while 1:
    if c():
        break
    ans += 1
    A = f()
print(ans)


Fixed submission:
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]

from collections import deque

lst = deque()

for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            lst.append((i, j, 0))


def chg(i, j):
    if i < 0 or j < 0 or H <= i or W <= j: return False
    if A[i][j] == '.':
        A[i][j] = '#'
        return True
    return False


ans = 0
while lst:
    h, w, v = lst.popleft()
    ans = max(v, ans)
    for i in range(-1, 2):
        for j in range(-1, 2):
            if i != 0 and j != 0: continue

            if chg(h + i, w + j):
                lst.append((h + i, w + j, v + 1))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]


def f():
    B = [['.'] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if A[i][j] == '#':
                B[i][j] = '#'
                if i > 0:
                    B[i - 1][j] = '#'
                if j > 0:
                    B[i][j - 1] = '#'
                if i < H - 1:
                    B[i + 1][j] = '#'
                if j < W - 1:
                    B[i][j + 1] = '#'
    return B


def c():
    for i in range(H):
        for j in range(W):
            if A[i][j] == '.': return False
    return True


ans = 0
while 1:
    if c():
        break
    ans += 1
    A = f()
print(ans)


Fixed submission:
H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]

from collections import deque

lst = deque()

for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            lst.append((i, j, 0))


def chg(i, j):
    if i < 0 or j < 0 or H <= i or W <= j: return False
    if A[i][j] == '.':
        A[i][j] = '#'
        return True
    return False


ans = 0
while lst:
    h, w, v = lst.popleft()
    ans = max(v, ans)
    for i in range(-1, 2):
        for j in range(-1, 2):
            if i != 0 and j != 0: continue

            if chg(h + i, w + j):
                lst.append((h + i, w + j, v + 1))
print(ans)