Buggy python submission (verdict: Runtime Error):
from collections import deque
import sys

readline = sys.stdin.readline

H, W = map(int, readline().split())
a = tuple(readline() for _ in range(H))


def adjacent_vertices(v):
    r, c = divmod(v, H)

    if r > 0:
        yield v - H
    if r < H - 1:
        yield v + H
    if c > 0:
        yield v - 1
    if c < W - 1:
        yield v + 1


gen = (r * H + c for r in range(H) for c in range(W) if a[r][c] == '#')

# BFSで黒から最遠点までの距離を求める
dist = [-1] * (H * W)
q = deque()
for v in gen:
    dist[v] = 0
    q.append(v)
while q:
    v = q.popleft()
    d = dist[v]
    for nv in filter(lambda x: dist[x] == -1, adjacent_vertices(v)):
        dist[nv] = d + 1
        q.append(nv)

print(d)


Fixed submission:
from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)

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

readline = sys.stdin.readline

H, W = map(int, readline().split())
a = [readline() for _ in range(H)]


def adjacent_vertices(r, c):
    yield r - 1, c
    yield r + 1, c
    yield r, c - 1
    yield r, c + 1


def can_use(arg):
    r, c = arg
    return (0 <= r < H and 0 <= c < W) and (dist[r][c] == -1)


gen = ((r, c) for r in range(H) for c in range(W) if a[r][c] == '#')

# BFSで黒から最遠点までの距離を求める
dist = [[-1] * W for _ in range(H)]
q = deque()
for r, c in gen:
    dist[r][c] = 0
    q.append((r, c))
while q:
    r, c = q.popleft()
    d = dist[r][c]
    for nr, nc in filter(can_use, adjacent_vertices(r, c)):
        dist[nr][nc] = d + 1
        q.append((nr, nc))

print(d)


Fixed submission:
from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)

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

input = sys.stdin.readline

from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)


Fixed submission:
from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)

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

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

dist = np.full((H, W), -1, dtype='int8')
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r, c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)


Fixed submission:
from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)

Buggy python submission (verdict: Runtime Error):
from collections import deque
import numpy as np

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

dist = np.full((H, W), -1, dtype='int8')
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r, c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)


Fixed submission:
from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)

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

inf = float('inf')

H, W = map(int, input().split())
d = defaultdict(bool)  # False

dq = deque()
for r in range(H):
    a = input()
    for c in range(W):
        if a[c] == '#':
            d[(r, c)] = 0
            dq.append((r, c, 0))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


while dq:
    r, c, dist = dq.popleft()
    if not d[(r + 1, c)] and in_range(r + 1, c):
        d[(r + 1, c)] = dist + 1
        dq.append((r + 1, c, dist + 1))
    if not d[(r - 1, c)] and in_range(r - 1, c):
        d[(r - 1, c)] = dist + 1
        dq.append((r - 1, c, dist + 1))
    if not d[(r, c + 1)] and in_range(r, c + 1):
        d[(r, c + 1)] = dist + 1
        dq.append((r, c + 1, dist + 1))
    if not d[(r, c - 1)] and in_range(r, c - 1):
        d[(r, c - 1)] = dist + 1
        dq.append((r, c - 1, dist + 1))

# print(d)
print(dist)


Fixed submission:
from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)

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

white_set = set()
for r in range(H):
    for c in range(W):
        if a[r][c] == '.':
            white_set.add((r, c))

ans = 0
while white_set:
    # print(white_set)
    to_del_set = set()
    for r, c in white_set:
        for dr, dc in (-1, 0), (1, 0), (0, -1), (0, 1):
            nr = r + dr
            if not(0<= nr < H):continue
            nc = c + dc
            if not (0 <= nc < W): continue
            if (nr, nc) not in white_set:
                # 隣接は黒
                to_del_set.add((r, c))
                break
    white_set -= to_del_set
    ans += 1

print(ans)


Fixed submission:
from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
e = [[False for _ in range(W)] for _ in range(H)]
cnt = 0
for r in range(H):
    a = input()
    for c in range(W):
        if a[c] == '#':
            cnt += 1
            e[r][c] = True

ans = 0
while cnt < H * W:
    st = set()
    for r in range(H):
        for c in range(W):
            if e[r][c]:
                for dr, dc in (-1, 0), (1, 0), (0, -1), (0, 1):
                    nr = r + dr
                    if not (0 <= nr < H): continue
                    nc = c + dc
                    if not (0 <= nc < W): continue
                    if e[nr][nc]: continue
                    st.add((nr, nc))
    for nr, nc in st:
        e[nr][nc] = True
        cnt += 1
    ans += 1
print(ans)


Fixed submission:
from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '#':
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)