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

INF = 10**18

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

q = deque([])
dist = [[INF for _ in range(W)]for _ in range(H)]
for y, A_line in enumerate(A):
    for x, a in enumerate(A_line):
        if a == "#":
            dist[y][x] = 0
            q.append((x, y))

while q:
    t_pos = tx, ty = q.popleft()
    dist_t = dist[ty][tx]

    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        pos = x, y = tx+dx, ty+dy
        if not (0 <= x < W and 0 <= y < H):
            continue
        elif A[y][x] == "#":
            continue
        elif dist[y][x] <= dist_t + 1:
            continue
        dist[y][x] = dist_t + 1
        q.append(pos)
# print(dist)

r = max(chain(*dist))
print(r)

Fixed submission:
from collections import deque
from itertools import chain

INF = 10**18

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

q = deque([])
dist = [[INF for _ in range(W)]for _ in range(H)]
for y, A_line in enumerate(A):
    for x, a in enumerate(A_line):
        if a == "#":
            dist[y][x] = 0
            q.append((x, y))

while q:
    t_pos = tx, ty = q.popleft()
    dist_t = dist[ty][tx]

    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        pos = x, y = tx+dx, ty+dy
        if not (0 <= x < W and 0 <= y < H):
            continue
        elif A[y][x] == "#":
            continue
        elif dist[y][x] <= dist_t + 1:
            continue
        dist[y][x] = dist_t + 1
        q.append(pos)
# print(dist)

r = max(chain(*dist))
print(r)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
 
ADS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
 
H, W = map(int, input().split(" "))
A = [list(input()) for _ in range(H)]

q = deque()
for y in range(H):
    for x in range(W):
        if A[y][x] == "#":
            q.append((x, y))

count = 0
while q:
    r = list(q)
    q = deque()
    for q_i in r:
        this_pos = this_x, this_y = q_i
        
        for ad_x, ad_y in ADS:
            pos = x, y = this_x+ad_x, this_y+ad_y
            if not ((0 <= x < W) and (0 <= y < H)):
                continue
            if A[y][x] == "#":
                continue
            A[y][x] = "#"
            q.append(pos)
    count += 1
    # print(A, count, q)

print(count-1)

Fixed submission:
from collections import deque
from itertools import chain

INF = 10**18

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

q = deque([])
dist = [[INF for _ in range(W)]for _ in range(H)]
for y, A_line in enumerate(A):
    for x, a in enumerate(A_line):
        if a == "#":
            dist[y][x] = 0
            q.append((x, y))

while q:
    t_pos = tx, ty = q.popleft()
    dist_t = dist[ty][tx]

    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        pos = x, y = tx+dx, ty+dy
        if not (0 <= x < W and 0 <= y < H):
            continue
        elif A[y][x] == "#":
            continue
        elif dist[y][x] <= dist_t + 1:
            continue
        dist[y][x] = dist_t + 1
        q.append(pos)
# print(dist)

r = max(chain(*dist))
print(r)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
# import copy
 
ADS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
 
H, W = map(int, input().split(" "))
A = [list(input()) for _ in range(H)]
 
q = deque()
 
for y in range(H):
    for x in range(W):
        if A[y][x] == "#":
            q.append((x, y))
 
count = 0
while q: # 10**6
    q.append((-1, -1))
    while q:
        this_pos = this_x, this_y = q.popleft()
        if this_pos == (-1, -1):
            break

        for ad_x, ad_y in ADS:
            x, y = this_x+ad_x, this_y+ad_y
            if not ((0 <= x < W) and (0 <= y < H)):
                continue
            if A[y][x] == "#":
                continue
            A[y][x] = "#"
            q.append((x, y))
    count += 1
    # print(A, q, count)
 
print(count-1)

Fixed submission:
from collections import deque
from itertools import chain

INF = 10**18

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

q = deque([])
dist = [[INF for _ in range(W)]for _ in range(H)]
for y, A_line in enumerate(A):
    for x, a in enumerate(A_line):
        if a == "#":
            dist[y][x] = 0
            q.append((x, y))

while q:
    t_pos = tx, ty = q.popleft()
    dist_t = dist[ty][tx]

    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        pos = x, y = tx+dx, ty+dy
        if not (0 <= x < W and 0 <= y < H):
            continue
        elif A[y][x] == "#":
            continue
        elif dist[y][x] <= dist_t + 1:
            continue
        dist[y][x] = dist_t + 1
        q.append(pos)
# print(dist)

r = max(chain(*dist))
print(r)

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

ADS = [(0, 1), (1, 0), (0, -1), (-1, 0)]

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

q = deque()

for y in range(H):
    for x in range(W):
        if A[y][x] == "#":
            q.append((x, y))

count = 0
while q: # 10**6
    r = copy.copy(q)
    q = deque()
    while r:
        this_pos = this_x, this_y = r.popleft()

        for ad_x, ad_y in ADS:
            x, y = this_x+ad_x, this_y+ad_y
            if not ((0 <= x < W) and (0 <= y < H)):
                continue
            if A[y][x] == "#":
                continue
            A[y][x] = "#"
            q.append((x, y))
    count += 1
    # print(A, q, count)

print(count-1)

Fixed submission:
from collections import deque
from itertools import chain

INF = 10**18

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

q = deque([])
dist = [[INF for _ in range(W)]for _ in range(H)]
for y, A_line in enumerate(A):
    for x, a in enumerate(A_line):
        if a == "#":
            dist[y][x] = 0
            q.append((x, y))

while q:
    t_pos = tx, ty = q.popleft()
    dist_t = dist[ty][tx]

    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        pos = x, y = tx+dx, ty+dy
        if not (0 <= x < W and 0 <= y < H):
            continue
        elif A[y][x] == "#":
            continue
        elif dist[y][x] <= dist_t + 1:
            continue
        dist[y][x] = dist_t + 1
        q.append(pos)
# print(dist)

r = max(chain(*dist))
print(r)