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

# reduce q.append()
# (constant-factor optimization)


def append(d, i, j):
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        return
    dists[i][j] = d
    q.append((d, i, j))


q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            append(0, i, j)

while len(q):
    d, i, j = q.popleft()
    append(d+1, i-1, j)
    append(d+1, i+1, j)
    append(d+1, i, j-1)
    append(d+1, i, j+1)

# fast (0.01s)
ans = max(map(max, dists))
# very slow (4.2s)
# ans = max(sum(dists, []))

print(ans)


Fixed submission:
from time import time
from collections import deque

# reduce q.append()
# (constant-factor optimization)


def append(d, i, j):
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        return
    dists[i][j] = d
    q.append((d, i, j))


q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            append(0, i, j)

while len(q):
    d, i, j = q.popleft()
    append(d+1, i-1, j)
    append(d+1, i+1, j)
    append(d+1, i, j-1)
    append(d+1, i, j+1)

# fast (0.01s)
ans = max(map(max, dists))
# very slow (4.2s)
# ans = max(sum(dists, []))

print(ans)

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

q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            q.append((0, i, j))

while len(q):
    d, i, j = q.popleft()
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        continue
    dists[i][j] = d
    q.append((d+1, i-1, j))
    q.append((d+1, i+1, j))
    q.append((d+1, i, j-1))
    q.append((d+1, i, j+1))

ans = max(map(max, dists))
print(ans)
# very slow
# ans = max(sum(dists, []))
# print(ans)


Fixed submission:
from time import time
from collections import deque

# reduce q.append()
# (constant-factor optimization)


def append(d, i, j):
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        return
    dists[i][j] = d
    q.append((d, i, j))


q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            append(0, i, j)

while len(q):
    d, i, j = q.popleft()
    append(d+1, i-1, j)
    append(d+1, i+1, j)
    append(d+1, i, j-1)
    append(d+1, i, j+1)

# fast (0.01s)
ans = max(map(max, dists))
# very slow (4.2s)
# ans = max(sum(dists, []))

print(ans)

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

q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            q.append((0, i, j))

while len(q):
    d, i, j = q.popleft()
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        continue
    dists[i][j] = d
    q.append((d+1, i-1, j))
    q.append((d+1, i+1, j))
    q.append((d+1, i, j-1))
    q.append((d+1, i, j+1))

ans = max(map(max, dists))
print(ans)
# very slow
# ans = max(sum(dists, []))
# print(ans)


Fixed submission:
from time import time
from collections import deque

# reduce q.append()
# (constant-factor optimization)


def append(d, i, j):
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        return
    dists[i][j] = d
    q.append((d, i, j))


q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            append(0, i, j)

while len(q):
    d, i, j = q.popleft()
    append(d+1, i-1, j)
    append(d+1, i+1, j)
    append(d+1, i, j-1)
    append(d+1, i, j+1)

# fast (0.01s)
ans = max(map(max, dists))
# very slow (4.2s)
# ans = max(sum(dists, []))

print(ans)

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

q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            q.append((0, i, j))

while len(q):
    d, i, j = q.popleft()
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        continue
    dists[i][j] = d
    q.append((d+1, i-1, j))
    q.append((d+1, i+1, j))
    q.append((d+1, i, j-1))
    q.append((d+1, i, j+1))

ans = max(sum(dists, []))
print(ans)


Fixed submission:
from time import time
from collections import deque

# reduce q.append()
# (constant-factor optimization)


def append(d, i, j):
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        return
    dists[i][j] = d
    q.append((d, i, j))


q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            append(0, i, j)

while len(q):
    d, i, j = q.popleft()
    append(d+1, i-1, j)
    append(d+1, i+1, j)
    append(d+1, i, j-1)
    append(d+1, i, j+1)

# fast (0.01s)
ans = max(map(max, dists))
# very slow (4.2s)
# ans = max(sum(dists, []))

print(ans)

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

q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            q.append((0, i, j))

while len(q):
    d, i, j = q.popleft()
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        continue
    dists[i][j] = d
    q.append((d+1, i-1, j))
    q.append((d+1, i+1, j))
    q.append((d+1, i, j-1))
    q.append((d+1, i, j+1))

ans = max(sum(dists, []))
print(ans)


Fixed submission:
from time import time
from collections import deque

# reduce q.append()
# (constant-factor optimization)


def append(d, i, j):
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        return
    dists[i][j] = d
    q.append((d, i, j))


q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "#":
            append(0, i, j)

while len(q):
    d, i, j = q.popleft()
    append(d+1, i-1, j)
    append(d+1, i+1, j)
    append(d+1, i, j-1)
    append(d+1, i, j+1)

# fast (0.01s)
ans = max(map(max, dists))
# very slow (4.2s)
# ans = max(sum(dists, []))

print(ans)