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

#dp[i][j]=最も近い黒マスまでの最小マンハッタン距離
INF=10**9
dp = [[INF] * W for _ in range(H)]

#黒マスを起点にDFS
def dfs(si,sj):
    dp[si][sj] = 0
    stk = [(si,sj,0)]
    while stk:
        i,j,d = stk.pop()
        for di,dj in ((1,0),(-1,0),(0,1),(0,-1)):
            ti,tj,td = i+di,j+dj,d+1
            if (not 0<=ti<H) or (not 0<=tj<W): continue
            if dp[ti][tj] <= td: continue
            if A[ti][tj] == '#': td = 0
            dp[ti][tj] = td
            stk.append((ti,tj,td))

for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            dfs(i,j)
            break

ans=0
for i in range(H):
    for j in range(W):
        ans = max(ans, dp[i][j])
print(ans)

Fixed submission:
from collections import deque
H,W=map(int,input().split())
A=[list(input()) for _ in range(H)]

q=deque()

#黒マスを探す
for w in range(W):
    for h in range(H):
        if A[h][w]=='#':
            q.append((h,w,0))

#BFS
ans=0
while q:
    h,w,d = q.popleft()
    ans=max(ans,d)
    for dh,dw in [(0,1),(1,0),(0,-1),(-1,0)]:
        nh,nw = h+dh, w+dw
        if nh<0 or nh>=H or nw<0 or nw>=W:
            continue
        if A[nh][nw]=='#':
            continue
        A[nh][nw]='#'
        q.append((nh,nw,d+1))

print(ans)

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

q=deque()

#黒マスを探す
for w in range(W):
    for h in range(H):
        if A[h][w]=='#':
            q.append((h,w,0))

#BFS
ans=0
while q:
    h,w,d = q.popleft()
    ans=max(ans,d)
    for dh,dw in [(0,1),(1,0),(0,-1),(-1,0)]:
        nh,nw = h+dh, w+dw
        if nh<0 or nh>=H or nw<0 or nw>=W:
            continue
        if A[nh][nw]=='#':
            continue
        A[nh][nw]='#'
        q.append((nh,nw,d+1))

print(ans)

Fixed submission:
from collections import deque
H,W=map(int,input().split())
A=[list(input()) for _ in range(H)]

q=deque()

#黒マスを探す
for w in range(W):
    for h in range(H):
        if A[h][w]=='#':
            q.append((h,w,0))

#BFS
ans=0
while q:
    h,w,d = q.popleft()
    ans=max(ans,d)
    for dh,dw in [(0,1),(1,0),(0,-1),(-1,0)]:
        nh,nw = h+dh, w+dw
        if nh<0 or nh>=H or nw<0 or nw>=W:
            continue
        if A[nh][nw]=='#':
            continue
        A[nh][nw]='#'
        q.append((nh,nw,d+1))

print(ans)

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

blacks=[]
whites=[]

#黒マス白マスをリストアップ
for w in range(W):
    for h in range(H):
        if A[h][w]=='#':
            blacks.append((h,w))
        else:
            whites.append((h,w))

ans=0
#各白マスについて黒マスとの最短距離を求め、その最大値を探す(マンハッタン距離)
for white in whites:
    dist=H+W+1
    for black in blacks:
        dist=min(dist,abs(black[0]-white[0])+abs(black[1]-white[1]))
    ans=max(ans,dist)

print(ans)

Fixed submission:
from collections import deque
H,W=map(int,input().split())
A=[list(input()) for _ in range(H)]

q=deque()

#黒マスを探す
for w in range(W):
    for h in range(H):
        if A[h][w]=='#':
            q.append((h,w,0))

#BFS
ans=0
while q:
    h,w,d = q.popleft()
    ans=max(ans,d)
    for dh,dw in [(0,1),(1,0),(0,-1),(-1,0)]:
        nh,nw = h+dh, w+dw
        if nh<0 or nh>=H or nw<0 or nw>=W:
            continue
        if A[nh][nw]=='#':
            continue
        A[nh][nw]='#'
        q.append((nh,nw,d+1))

print(ans)