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

H, W = map(int, input().split())
data = deque()

for y in range(H):
    for x, v in enumerate(input()):
        if v == '#':
            data.append([y, x, [], 0])

inf = H * W + 10
ans = [[inf for _ in range(W)] for __ in range(H)]

for v in data:
    ans[v[0]][v[1]] = 0

def check(destination, origin):
    return [destination[0], destination[1]] not in origin[2]

def cnt_update(ans, v):
    ans[v[0], v[1]] = v[3] + 1
"""
BFSまとめ
0. 現在位置、履歴、アクション数の3要素が必要
1. 更新する, アクション数を加算する
2. 条件を一致しているか、履歴に入っていないかを確認
3. popをループ
"""

fin = 0

while len(data):
    v = data.popleft()
    v[2].append([v[0], v[1]])

    if v[1] - 1 >= 0 and ans[v[0]][v[1] - 1] > v[3] + 1:
        v_left = [v[0], v[1] - 1, v[2], v[3] + 1]
        if check(v_left, v):
            ans[v_left[0]][v_left[1]] = v_left[3]
            data.append(v_left)
            if fin < v[3] + 1:
                fin = v[3] + 1

    if v[1] + 1 <= W - 1 and ans[v[0]][v[1] + 1] > v[3] + 1:
        v_right = [v[0], v[1] + 1, v[2], v[3] + 1]
        if check(v_right, v):
            ans[v_right[0]][v_right[1]] = v_right[3]
            data.append(v_right)
            if fin < v[3] + 1:
                fin = v[3] + 1

    if v[0] - 1 >= 0 and ans[v[0] - 1][v[1]] > v[3] + 1:
        v_up = [v[0] - 1, v[1], v[2], v[3] + 1]
        if check(v_up, v):
            ans[v_up[0]][v_up[1]] = v_up[3]
            data.append(v_up)
            if fin < v[3] + 1:
                fin = v[3] + 1

    if v[0] + 1 <= H - 1 and ans[v[0] + 1][v[1]] > v[3] + 1:
        v_down = [v[0] + 1, v[1], v[2], v[3] + 1]
        if check(v_down, v):
            ans[v_down[0]][v_down[1]] = v_down[3]
            data.append(v_down)
            if fin < v[3] + 1:
                fin = v[3] + 1

# print(max([a for aa in ans for a in aa]))
print(fin)

Fixed submission:
from collections import deque

H, W = map(int, input().split())
data = deque()

for y in range(H):
    for x, v in enumerate(input()):
        if v == '#':
            data.append((y, x))

ans = [[-1 for _ in range(W)] for __ in range(H)]

for v in data:
    ans[v[0]][v[1]] = 0

"""
BFSまとめ

0. 
現在位置、履歴、アクション数の3要素が必要.
処理をLIFOでなく、FIFOでやる
そのため、collection.dequeを使う
更にソートしつつ、FIFOしたい場合は、heapqを使う

1. 
更新する, アクション数を加算する

2. 
条件を一致しているか、履歴に入っていないかを確認

3. 
popをループ
"""

d = 0
while len(data):
    y, x = data.popleft()
    d = ans[y][x]

    for i, j in ((-1, 0), (1, 0), (0, -1), (0, 1)):
        new_y = y + i
        new_x = x + j
        if new_y < 0 or H <= new_y or new_x < 0 or W <= new_x:
            continue
        if ans[new_y][new_x] == -1:
            ans[new_y][new_x] = d + 1
            data.append((new_y, new_x))

print(max([a for aa in ans for a in aa]))

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

H, W = map(int, input().split())
data = deque()

for y in range(H):
    for x, v in enumerate(input()):
        if v == '#':
            data.append([y, x, [], 0])

inf = H * W + 10
ans = [[inf for _ in range(W)] for __ in range(H)]

for v in data:
    ans[v[0]][v[1]] = 0

def check(destination, origin):
    return [destination[0], destination[1]] not in origin[2]

def cnt_update(ans, v):
    ans[v[0], v[1]] = v[3] + 1
"""
BFSまとめ
0. 現在位置、履歴、アクション数の3要素が必要
1. 更新する, アクション数を加算する
2. 条件を一致しているか、履歴に入っていないかを確認
3. popをループ
"""

fin = 0

while len(data):
    v = data.popleft()
    v[2].append([v[0], v[1]])

    if v[1] - 1 >= 0 and ans[v[0]][v[1] - 1] > v[3] + 1:
        v_left = [v[0], v[1] - 1, v[2], v[3] + 1]
        if check(v_left, v):
            ans[v_left[0]][v_left[1]] = v_left[3]
            data.append(v_left)
            if fin < v[3] + 1:
                fin = v[3] + 1

    if v[1] + 1 <= W - 1 and ans[v[0]][v[1] + 1] > v[3] + 1:
        v_right = [v[0], v[1] + 1, v[2], v[3] + 1]
        if check(v_right, v):
            ans[v_right[0]][v_right[1]] = v_right[3]
            data.append(v_right)
            if fin < v[3] + 1:
                fin = v[3] + 1

    if v[0] - 1 >= 0 and ans[v[0] - 1][v[1]] > v[3] + 1:
        v_up = [v[0] - 1, v[1], v[2], v[3] + 1]
        if check(v_up, v):
            ans[v_up[0]][v_up[1]] = v_up[3]
            data.append(v_up)
            if fin < v[3] + 1:
                fin = v[3] + 1

    if v[0] + 1 <= H - 1 and ans[v[0] + 1][v[1]] > v[3] + 1:
        v_down = [v[0] + 1, v[1], v[2], v[3] + 1]
        if check(v_down, v):
            ans[v_down[0]][v_down[1]] = v_down[3]
            data.append(v_down)
            if fin < v[3] + 1:
                fin = v[3] + 1

# print(max([a for aa in ans for a in aa]))
print(fin)

Fixed submission:
from collections import deque

H, W = map(int, input().split())
data = deque()

for y in range(H):
    for x, v in enumerate(input()):
        if v == '#':
            data.append((y, x))

ans = [[-1 for _ in range(W)] for __ in range(H)]

for v in data:
    ans[v[0]][v[1]] = 0

"""
BFSまとめ

0. 
現在位置、履歴、アクション数の3要素が必要.
処理をLIFOでなく、FIFOでやる
そのため、collection.dequeを使う
更にソートしつつ、FIFOしたい場合は、heapqを使う

1. 
更新する, アクション数を加算する

2. 
条件を一致しているか、履歴に入っていないかを確認

3. 
popをループ
"""

d = 0
while len(data):
    y, x = data.popleft()
    d = ans[y][x]

    for i, j in ((-1, 0), (1, 0), (0, -1), (0, 1)):
        new_y = y + i
        new_x = x + j
        if new_y < 0 or H <= new_y or new_x < 0 or W <= new_x:
            continue
        if ans[new_y][new_x] == -1:
            ans[new_y][new_x] = d + 1
            data.append((new_y, new_x))

print(max([a for aa in ans for a in aa]))

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())

#A = [input() for _ in range(H)]

data = []

for y in range(H):
    for x, v in enumerate(input()):
        if v == '#':
            data.append([y, x, [], 0])

inf = H * W + 10
ans = [[inf for _ in range(W)] for __ in range(H)]

for v in data:
    ans[v[0]][v[1]] = 0

def check(destination, origin):
    return [destination[0], destination[1]] not in origin[2]

def cnt_update(ans, v):
    ans[v[0], v[1]] = v[3] + 1
"""
BFSまとめ
0. 現在位置、履歴、アクション数の3要素が必要
1. 更新する, アクション数を加算する
2. 条件を一致しているか、履歴に入っていないかを確認
3. popをループ
"""


while len(data):
    v = data.pop()
    v[2].append([v[0], v[1]])

    if v[1] - 1 >= 0 and ans[v[0]][v[1] - 1] > v[3] + 1:
        v_left = [v[0], v[1] - 1, v[2], v[3] + 1]
        if check(v_left, v):
            ans[v_left[0]][v_left[1]] = v_left[3]
            data.append(v_left)
    if v[1] + 1 <= W - 1 and ans[v[0]][v[1] + 1] > v[3] + 1:
        v_right = [v[0], v[1] + 1, v[2], v[3] + 1]
        if check(v_right, v):
            ans[v_right[0]][v_right[1]] = v_right[3]
            data.append(v_right)
    if v[0] - 1 >= 0 and ans[v[0] - 1][v[1]] > v[3] + 1:
        v_up = [v[0] - 1, v[1], v[2], v[3] + 1]
        if check(v_up, v):
            ans[v_up[0]][v_up[1]] = v_up[3]
            data.append(v_up)
    if v[0] + 1 <= H - 1 and ans[v[0] + 1][v[1]] > v[3] + 1:
        v_down = [v[0] + 1, v[1], v[2], v[3] + 1]
        if check(v_down, v):
            ans[v_down[0]][v_down[1]] = v_down[3]
            data.append(v_down)

print(max([a for aa in ans for a in aa]))

Fixed submission:
from collections import deque

H, W = map(int, input().split())
data = deque()

for y in range(H):
    for x, v in enumerate(input()):
        if v == '#':
            data.append((y, x))

ans = [[-1 for _ in range(W)] for __ in range(H)]

for v in data:
    ans[v[0]][v[1]] = 0

"""
BFSまとめ

0. 
現在位置、履歴、アクション数の3要素が必要.
処理をLIFOでなく、FIFOでやる
そのため、collection.dequeを使う
更にソートしつつ、FIFOしたい場合は、heapqを使う

1. 
更新する, アクション数を加算する

2. 
条件を一致しているか、履歴に入っていないかを確認

3. 
popをループ
"""

d = 0
while len(data):
    y, x = data.popleft()
    d = ans[y][x]

    for i, j in ((-1, 0), (1, 0), (0, -1), (0, 1)):
        new_y = y + i
        new_x = x + j
        if new_y < 0 or H <= new_y or new_x < 0 or W <= new_x:
            continue
        if ans[new_y][new_x] == -1:
            ans[new_y][new_x] = d + 1
            data.append((new_y, new_x))

print(max([a for aa in ans for a in aa]))

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())

#A = [input() for _ in range(H)]

data = []

for y in range(H):
    for x, v in enumerate(input()):
        if v == '#':
            data.append([y, x, [], 0])

inf = H * W + 10
ans = [[inf for _ in range(W)] for __ in range(H)]

for v in data:
    ans[v[0]][v[1]] = 0

def check(destination, origin):
    return [destination[0], destination[1]] not in origin[2]

def cnt_update(ans, v):
    ans[v[0], v[1]] = v[3] + 1

while len(data):
    v = data.pop(0)
    v[2].append([v[0], v[1]])

    if v[1] - 1 >= 0 and ans[v[0]][v[1] - 1] > v[3] + 1:
        v_left = [v[0], v[1] - 1, v[2], v[3] + 1]
        if check(v_left, v):
            ans[v_left[0]][v_left[1]] = v_left[3]
            data.append(v_left)
    if v[1] + 1 <= W - 1 and ans[v[0]][v[1] + 1] > v[3] + 1:
        v_right = [v[0], v[1] + 1, v[2], v[3] + 1]
        if check(v_right, v):
            ans[v_right[0]][v_right[1]] = v_right[3]
            data.append(v_right)
    if v[0] - 1 >= 0 and ans[v[0] - 1][v[1]] > v[3] + 1:
        v_up = [v[0] - 1, v[1], v[2], v[3] + 1]
        if check(v_up, v):
            ans[v_up[0]][v_up[1]] = v_up[3]
            data.append(v_up)
    if v[0] + 1 <= H - 1 and ans[v[0] + 1][v[1]] > v[3] + 1:
        v_down = [v[0] + 1, v[1], v[2], v[3] + 1]
        if check(v_down, v):
            ans[v_down[0]][v_down[1]] = v_down[3]
            data.append(v_down)

print(max([a for aa in ans for a in aa]))

Fixed submission:
from collections import deque

H, W = map(int, input().split())
data = deque()

for y in range(H):
    for x, v in enumerate(input()):
        if v == '#':
            data.append((y, x))

ans = [[-1 for _ in range(W)] for __ in range(H)]

for v in data:
    ans[v[0]][v[1]] = 0

"""
BFSまとめ

0. 
現在位置、履歴、アクション数の3要素が必要.
処理をLIFOでなく、FIFOでやる
そのため、collection.dequeを使う
更にソートしつつ、FIFOしたい場合は、heapqを使う

1. 
更新する, アクション数を加算する

2. 
条件を一致しているか、履歴に入っていないかを確認

3. 
popをループ
"""

d = 0
while len(data):
    y, x = data.popleft()
    d = ans[y][x]

    for i, j in ((-1, 0), (1, 0), (0, -1), (0, 1)):
        new_y = y + i
        new_x = x + j
        if new_y < 0 or H <= new_y or new_x < 0 or W <= new_x:
            continue
        if ans[new_y][new_x] == -1:
            ans[new_y][new_x] = d + 1
            data.append((new_y, new_x))

print(max([a for aa in ans for a in aa]))