Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W=map(int,input().split())
A=[input() for _ in range(H)]
visited_area=[[-1]*W for _ in range(H)]
stack=deque([])
for h,c_h in enumerate(A):
    for w,c_w in enumerate(c_h):
        if c_w=='#':
            stack.append([h,w])
def bfs(A,visited_area,stack):
    kotae = 0
    for a in range(H):
        kotae += A[a].count('#')
    while kotae<H*W:
        h, w = stack.pop()
        for x, y in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_h = h + x
            new_w = w + y
            if new_h < 0 or new_h >= H or new_w < 0 or new_w >= W:
                continue
            if A[new_h][new_w] == '.' and visited_area[new_h][new_w] == - 1:
                visited_area[new_h][new_w] = visited_area[h][w]+1
                stack.appendleft([new_h, new_w])
                kotae+=1
bfs(A,visited_area,stack)
answers=[]
for a in visited_area:
    answers.append(max(a))
print(max(answers)+1)


Fixed submission:
from collections import deque
H,W=map(int,input().split())
A=[input() for _ in range(H)]
visited_area=[[-1]*W for _ in range(H)]
stack=deque([])
for h,c_h in enumerate(A):
    for w,c_w in enumerate(c_h):
        if c_w=='#':
            stack.append([h,w])
def bfs(A,visited_area,stack):
    kotae = 0
    for a in range(H):
        kotae += A[a].count('#')
    while kotae<H*W:
        h, w = stack.pop()
        for x, y in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_h = h + x
            new_w = w + y
            if new_h < 0 or new_h >= H or new_w < 0 or new_w >= W:
                continue
            if A[new_h][new_w] == '.' and visited_area[new_h][new_w] == - 1:
                visited_area[new_h][new_w] = visited_area[h][w]+1
                stack.appendleft([new_h, new_w])
                kotae+=1
bfs(A,visited_area,stack)
answers=[]
for a in visited_area:
    answers.append(max(a))
print(max(answers)+1)

Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())
A=[input() for _ in range(H)]
visited_area=[[-1]*W for _ in range(H)]
stack=[]
for h,c_h in enumerate(A):
    for w,c_w in enumerate(c_h):
        if c_w=='#':
            stack.append([h,w])
def bfs(A,visited_area,stack):
    kotae = 0
    for a in range(H):
        kotae += A[a].count('#')
    while kotae<H*W:
        h, w = stack.pop()
        for x, y in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_h = h + x
            new_w = w + y
            if new_h < 0 or new_h >= H or new_w < 0 or new_w >= W:
                continue
            if A[new_h][new_w] == '.' and visited_area[new_h][new_w] == - 1:
                visited_area[new_h][new_w] = visited_area[h][w]+1
                stack.insert(0, [new_h, new_w])
                kotae+=1
bfs(A,visited_area,stack)
answers=[]
for a in visited_area:
    answers.append(max(a))
print(max(answers)+1)


Fixed submission:
from collections import deque
H,W=map(int,input().split())
A=[input() for _ in range(H)]
visited_area=[[-1]*W for _ in range(H)]
stack=deque([])
for h,c_h in enumerate(A):
    for w,c_w in enumerate(c_h):
        if c_w=='#':
            stack.append([h,w])
def bfs(A,visited_area,stack):
    kotae = 0
    for a in range(H):
        kotae += A[a].count('#')
    while kotae<H*W:
        h, w = stack.pop()
        for x, y in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_h = h + x
            new_w = w + y
            if new_h < 0 or new_h >= H or new_w < 0 or new_w >= W:
                continue
            if A[new_h][new_w] == '.' and visited_area[new_h][new_w] == - 1:
                visited_area[new_h][new_w] = visited_area[h][w]+1
                stack.appendleft([new_h, new_w])
                kotae+=1
bfs(A,visited_area,stack)
answers=[]
for a in visited_area:
    answers.append(max(a))
print(max(answers)+1)