Buggy python submission (verdict: Time Limit Exceeded):
import sys
H, W = map(int, sys.stdin.readline().split())
INF = float('inf')
S = [[INF] * W for i in range(H)]
d = []
num = 0
for i in range(H):
    a = list(sys.stdin.readline())
    for j in range(W):
        if a[j] == '#':
            d.append((i, j, 0))
            S[i][j] = 0
            num += 1

ma = 0
while len(d) > 0:
    x, y, k = d.pop(0)
    ma = max(ma, k)
    for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
        nx, ny = x + dx, y + dy
        if 0 <= nx < H and 0 <= ny < W and S[nx][ny] > k:
            S[nx][ny] = k
            d.append((nx, ny, k + 1))
            if num == H * W:
                break

print(ma)

Fixed submission:
import sys
H, W = map(int, sys.stdin.readline().split())
INF = float('inf')
S = [[INF] * W for i in range(H)]
for i in range(H):
    a = list(sys.stdin.readline())
    for j in range(W):
        if a[j] == '#':
            S[i][j] = 0

ma = 0
for i in range(1, H):
    for j in range(W):
        S[i][j] = min(S[i][j], S[i - 1][j] + 1)

for i in range(H - 2, -1, -1):
    for j in range(W):
        S[i][j] = min(S[i][j], S[i + 1][j] + 1)

for i in range(H):
    for j in range(1, W):
        S[i][j] = min(S[i][j], S[i][j - 1] + 1)

for i in range(H):
    for j in range(W - 2, -1, -1):
        S[i][j] = min(S[i][j], S[i][j + 1] + 1)

for i in range(H):
    for j in range(W):
        ma = max(ma, S[i][j])

print(ma)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
T = [[] for i in range(H)]
d = []
num = 0
for i in range(H):
    a = list(input())
    T[i] = a
    for j in range(W):
        if a[j] == '#':
            d.append((i, j, 0))
            num += 1

ma = 0
while len(d) > 0:
    x, y, k = d.pop(0)
    ma = max(ma, k)
    for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
        nx, ny = x + dx, y + dy
        if 0 <= nx < H and 0 <= ny < W and T[nx][ny] == '.':
            T[nx][ny] = '#'
            d.append((nx, ny, k + 1))
            if num == H * W:
                break

print(ma)


Fixed submission:
import sys
H, W = map(int, sys.stdin.readline().split())
INF = float('inf')
S = [[INF] * W for i in range(H)]
for i in range(H):
    a = list(sys.stdin.readline())
    for j in range(W):
        if a[j] == '#':
            S[i][j] = 0

ma = 0
for i in range(1, H):
    for j in range(W):
        S[i][j] = min(S[i][j], S[i - 1][j] + 1)

for i in range(H - 2, -1, -1):
    for j in range(W):
        S[i][j] = min(S[i][j], S[i + 1][j] + 1)

for i in range(H):
    for j in range(1, W):
        S[i][j] = min(S[i][j], S[i][j - 1] + 1)

for i in range(H):
    for j in range(W - 2, -1, -1):
        S[i][j] = min(S[i][j], S[i][j + 1] + 1)

for i in range(H):
    for j in range(W):
        ma = max(ma, S[i][j])

print(ma)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
T = [[] for i in range(H)]
d = []
for i in range(H):
    a = list(input())
    T[i] = a
    for j in range(W):
        if a[j] == '#':
            d.append((i, j, 0))

ma = 0
while len(d) > 0:
    x, y, k = d.pop(0)
    ma = max(ma, k)
    for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
        nx, ny = x + dx, y + dy
        if 0 <= nx < H and 0 <= ny < W and T[nx][ny] == '.':
            T[nx][ny] = '#'
            d.append((nx, ny, k + 1))

print(ma)


Fixed submission:
import sys
H, W = map(int, sys.stdin.readline().split())
INF = float('inf')
S = [[INF] * W for i in range(H)]
for i in range(H):
    a = list(sys.stdin.readline())
    for j in range(W):
        if a[j] == '#':
            S[i][j] = 0

ma = 0
for i in range(1, H):
    for j in range(W):
        S[i][j] = min(S[i][j], S[i - 1][j] + 1)

for i in range(H - 2, -1, -1):
    for j in range(W):
        S[i][j] = min(S[i][j], S[i + 1][j] + 1)

for i in range(H):
    for j in range(1, W):
        S[i][j] = min(S[i][j], S[i][j - 1] + 1)

for i in range(H):
    for j in range(W - 2, -1, -1):
        S[i][j] = min(S[i][j], S[i][j + 1] + 1)

for i in range(H):
    for j in range(W):
        ma = max(ma, S[i][j])

print(ma)