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

buf = []

for i in range(H):
    for j in range(W):
        if table[i][j] == '#':
            buf.append((i, j))


ans = 0
while True:
    n = sum([row.count('#') for row in table])
    if n == H * W:
        break


    buf2 = []
    for i, j in buf:
        if i-1 >= 0 and table[i-1][j] != '#':
            table[i - 1][j] = '#'
            buf2.append((i-1, j))

        if i+1 < H and table[i+1][j] != '#':
            table[i + 1][j] = '#'
            buf2.append((i+1, j))

        if j-1 >= 0 and table[i][j-1] != '#':
            table[i][j-1] = '#'
            buf2.append((i, j-1))

        if j+1 < W and table[i][j+1] != '#':
            table[i][j+1] = '#'
            buf2.append((i, j+1))

    ans += 1
    buf = buf2


    # print('-- -- --')
    # print(buf)
    # for row in table:
    #     print(''.join(row))


print(ans)


Fixed submission:
# -*- coding: utf-8 -*-
H, W =  map(int, input().split(' '))
table = [list(input()) for _ in range(H)]

buf = []

n = 0
for i in range(H):
    for j in range(W):
        if table[i][j] == '#':
            buf.append((i, j))
            n += 1


ans = 0
while n != H * W:
    ans += 1

    buf2 = []
    for i, j in buf:
        if i-1 >= 0 and table[i-1][j] != '#':
            table[i - 1][j] = '#'
            buf2.append((i-1, j))
            n += 1

        if i+1 < H and table[i+1][j] != '#':
            table[i + 1][j] = '#'
            buf2.append((i+1, j))
            n += 1

        if j-1 >= 0 and table[i][j-1] != '#':
            table[i][j-1] = '#'
            buf2.append((i, j-1))
            n += 1

        if j+1 < W and table[i][j+1] != '#':
            table[i][j+1] = '#'
            buf2.append((i, j+1))
            n += 1

    buf = buf2

    # print('-- -- --')
    # print(buf)
    # for row in table:
    #     print(''.join(row))


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
H, W =  map(int, input().split(' '))
table = [list(input()) for _ in range(H)]

buf = []

for i in range(H):
    for j in range(W):
        if table[i][j] == '#':
            buf.append((i, j))


ans = 0
while True:
    n = 0
    for i in range(H):
        for j in range(W):
            if table[i][j] == '#':
                n += 1
    if n == H * W:
        break


    buf2 = []
    for i, j in buf:
        if i-1 >= 0 and table[i-1][j] != '#':
            table[i - 1][j] = '#'
            buf2.append((i-1, j))

        if i+1 < H and table[i+1][j] != '#':
            table[i + 1][j] = '#'
            buf2.append((i+1, j))

        if j-1 >= 0 and table[i][j-1] != '#':
            table[i][j-1] = '#'
            buf2.append((i, j-1))

        if j+1 < W and table[i][j+1] != '#':
            table[i][j+1] = '#'
            buf2.append((i, j+1))

    ans += 1
    buf = buf2


    # print('-- -- --')
    # print(buf)
    # for row in table:
    #     print(''.join(row))


print(ans)

Fixed submission:
# -*- coding: utf-8 -*-
H, W =  map(int, input().split(' '))
table = [list(input()) for _ in range(H)]

buf = []

n = 0
for i in range(H):
    for j in range(W):
        if table[i][j] == '#':
            buf.append((i, j))
            n += 1


ans = 0
while n != H * W:
    ans += 1

    buf2 = []
    for i, j in buf:
        if i-1 >= 0 and table[i-1][j] != '#':
            table[i - 1][j] = '#'
            buf2.append((i-1, j))
            n += 1

        if i+1 < H and table[i+1][j] != '#':
            table[i + 1][j] = '#'
            buf2.append((i+1, j))
            n += 1

        if j-1 >= 0 and table[i][j-1] != '#':
            table[i][j-1] = '#'
            buf2.append((i, j-1))
            n += 1

        if j+1 < W and table[i][j+1] != '#':
            table[i][j+1] = '#'
            buf2.append((i, j+1))
            n += 1

    buf = buf2

    # print('-- -- --')
    # print(buf)
    # for row in table:
    #     print(''.join(row))


print(ans)