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

H, W = map(int, input().split())
A = list()
q = deque()
for i in range(H):
    s = input()
    for j, c in enumerate(s):
        if c == '#':
            q.append((i, j, 0))
    A.append([c for c in s])

ans = 0
while len(q) > 0:
    i, j, x = q.popleft()
    A[i][j] = '#'
    ans = max(ans, x)
    if i > 0 and A[i - 1][j] == '.':
        q.append((i - 1, j, x + 1))
    if j > 0 and A[i][j - 1] == '.':
        q.append((i, j - 1, x + 1))
    if i < H - 1 and A[i + 1][j] == '.':
        q.append((i + 1, j, x + 1))
    if j < W - 1 and A[i][j + 1] == '.':
        q.append((i, j + 1, x + 1))
print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import deque

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


H, W = MI()
grid = [list(input()) for _ in range(H)]

seen = set()
q = deque()
for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            q.append((i, j, 0))
            seen.add(i * W + j)
while len(q) > 0:
    h, w, d = q.popleft()
    for dh, dw in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
        if 0 <= h + dh < H and 0 <= w + dw < W and (h + dh) * W + w + dw not in seen:
            q.append((h + dh, w + dw, d + 1))
            seen.add((h + dh) * W + w + dw)
print(d)

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

H, W = map(int, input().split())
A = list()
seen = set()
q = deque()
for i in range(H):
    s = input()
    for j, c in enumerate(s):
        if c == '#':
            q.append((i, j, 0))
            seen.add((i, j))
    A.append(s)

ans = 0
while len(q) > 0:
    i, j, x = q.popleft()
    ans = max(ans, x)
    if i > 0 and (i - 1, j) not in seen:
        q.append((i - 1, j, x + 1))
        seen.add((i - 1, j))
    if j > 0 and (i, j - 1) not in seen:
        q.append((i, j - 1, x + 1))
        seen.add((i, j - 1))
    if i < H - 1 and (i + 1, j) not in seen:
        q.append((i + 1, j, x + 1))
        seen.add((i + 1, j))
    if j < W - 1 and (i, j + 1) not in seen:
        q.append((i, j + 1, x + 1))
        seen.add((i, j + 1))
print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import deque

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


H, W = MI()
grid = [list(input()) for _ in range(H)]

seen = set()
q = deque()
for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            q.append((i, j, 0))
            seen.add(i * W + j)
while len(q) > 0:
    h, w, d = q.popleft()
    for dh, dw in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
        if 0 <= h + dh < H and 0 <= w + dw < W and (h + dh) * W + w + dw not in seen:
            q.append((h + dh, w + dw, d + 1))
            seen.add((h + dh) * W + w + dw)
print(d)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = list()
blacks = list()
for i in range(H):
    s = input()
    for j, c in enumerate(s):
        if c == '#':
            blacks.append((i, j))
    A.append(s)

ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == '.':
            dist = float('inf')
            for bi, bj in blacks:
                dist = min(dist, abs(bi - i) + abs(bj - j))
            ans = max(ans, dist)
print(ans)



Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import deque

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


H, W = MI()
grid = [list(input()) for _ in range(H)]

seen = set()
q = deque()
for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            q.append((i, j, 0))
            seen.add(i * W + j)
while len(q) > 0:
    h, w, d = q.popleft()
    for dh, dw in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
        if 0 <= h + dh < H and 0 <= w + dw < W and (h + dh) * W + w + dw not in seen:
            q.append((h + dh, w + dw, d + 1))
            seen.add((h + dh) * W + w + dw)
print(d)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
s = set()
for i in range(H):
    for j, c in enumerate(input()):
        if c == '#':
            s.add((i, j))

cnt = 0
while len(s) < H * W:
    ns = set()
    for i, j in s:
        ns.add((i, j))
        if i > 0:
            ns.add((i - 1, j))
        if j > 0:
            ns.add((i, j - 1))
        if i < H - 1:
            ns.add((i + 1, j))
        if j < W - 1:
            ns.add((i, j + 1))
    s = ns
    cnt += 1
print(cnt)



Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import deque

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


H, W = MI()
grid = [list(input()) for _ in range(H)]

seen = set()
q = deque()
for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            q.append((i, j, 0))
            seen.add(i * W + j)
while len(q) > 0:
    h, w, d = q.popleft()
    for dh, dw in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
        if 0 <= h + dh < H and 0 <= w + dw < W and (h + dh) * W + w + dw not in seen:
            q.append((h + dh, w + dw, d + 1))
            seen.add((h + dh) * W + w + dw)
print(d)