Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
dxy=[(0,1),(1,0),(-1,0),(0,-1)]
inf=float("inf")

dp=[[inf]*w for _ in range(h)]
q=deque()

for y,l in enumerate(a):
    for x,c in enumerate(l):
        if c=="#":
            dp[y][x]=0
            q.append((x,y))
ans=0
while q:
    x,y=q.popleft()
    d=dp[y][x]
    for dx,dy in dxy:
        nx,ny=x+dx,y+dy
        if not 0<=nx<w: continue
        if not 0<=ny<h: continue
        if dp[ny][nx]<=d+1: continue
        dp[ny][nx]=d+1
        q.append((nx,ny))
        ans=max(ans,d+1)
print(ans)


Fixed submission:
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
dxy=[(0,1),(1,0),(-1,0),(0,-1)]
inf=float("inf")

dp=[[inf]*w for _ in range(h)]
q=deque()

for y,l in enumerate(a):
    for x,c in enumerate(l):
        if c=="#":
            dp[y][x]=0
            q.append((x,y))
ans=0
while q:
    x,y=q.popleft()
    d=dp[y][x]
    for dx,dy in dxy:
        nx,ny=x+dx,y+dy
        if not 0<=nx<w: continue
        if not 0<=ny<h: continue
        if dp[ny][nx]<=d+1: continue
        dp[ny][nx]=d+1
        q.append((nx,ny))
        ans=max(ans,d+1)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
g=[list(input()) for _ in range(h)]
q=[]
r=[[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if g[i][j]=="#":
            g[i][j]=0
            r[i][j]=1
            q.append([i,j])
dx=[1,-1,0,0]
dy=[0,0,1,-1]
ans=0
while q:
    i,j=q.pop(0)
    for x,y in zip(dx,dy):
        if i+y<0 or i+y>=h or j+x<0 or j+x>=w:
            continue
        if r[i+y][j+x]:
            continue
        g[i+y][j+x]=g[i][j]+1
        r[i+y][j+x]+=1
        ans=max(ans,g[i+y][j+x])
        q.append([i+y,j+x])
print(ans)


Fixed submission:
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
dxy=[(0,1),(1,0),(-1,0),(0,-1)]
inf=float("inf")

dp=[[inf]*w for _ in range(h)]
q=deque()

for y,l in enumerate(a):
    for x,c in enumerate(l):
        if c=="#":
            dp[y][x]=0
            q.append((x,y))
ans=0
while q:
    x,y=q.popleft()
    d=dp[y][x]
    for dx,dy in dxy:
        nx,ny=x+dx,y+dy
        if not 0<=nx<w: continue
        if not 0<=ny<h: continue
        if dp[ny][nx]<=d+1: continue
        dp[ny][nx]=d+1
        q.append((nx,ny))
        ans=max(ans,d+1)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
g=[list(input()) for _ in range(h)]
q=[]
r=[[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if g[i][j]=="#":
            g[i][j]=0
            r[i][j]=1
            q.append([i,j])
dx=[1,-1,0,0]
dy=[0,0,1,-1]
ans=0
while q:

    i,j=q.pop(0)
    for x,y in zip(dx,dy):
        if i+y<0 or i+y>=h or j+x<0 or j+x>=w:
            continue
        if r[i+y][j+x]:
            continue
        g[i+y][j+x]=g[i][j]+1
        r[i+y][j+x]+=1
        ans=max(ans,g[i+y][j+x])
        q.append([i+y,j+x])
print(ans)


Fixed submission:
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
dxy=[(0,1),(1,0),(-1,0),(0,-1)]
inf=float("inf")

dp=[[inf]*w for _ in range(h)]
q=deque()

for y,l in enumerate(a):
    for x,c in enumerate(l):
        if c=="#":
            dp[y][x]=0
            q.append((x,y))
ans=0
while q:
    x,y=q.popleft()
    d=dp[y][x]
    for dx,dy in dxy:
        nx,ny=x+dx,y+dy
        if not 0<=nx<w: continue
        if not 0<=ny<h: continue
        if dp[ny][nx]<=d+1: continue
        dp[ny][nx]=d+1
        q.append((nx,ny))
        ans=max(ans,d+1)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h,w=map(int,input().split())
g=[list(input()) for _ in range(h)]
q=[]
r=[[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if g[i][j]=="#":
            g[i][j]=0
            r[i][j]=1
            q.append([i,j])
dx=[1,-1,0,0]
dy=[0,0,1,-1]
ans=0
while q:

    i,j=q.pop(0)
    for x,y in zip(dx,dy):
        if i+y<0 or i+y>=h or j+x<0 or j+x>=w:
            continue
        if r[i+y][j+x]:
            continue
        g[i+y][j+x]=g[i][j]+1
        r[i+y][j+x]+=1
        ans=max(ans,g[i+y][j+x])
        q.append([i+y,j+x])
print(ans)


Fixed submission:
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
dxy=[(0,1),(1,0),(-1,0),(0,-1)]
inf=float("inf")

dp=[[inf]*w for _ in range(h)]
q=deque()

for y,l in enumerate(a):
    for x,c in enumerate(l):
        if c=="#":
            dp[y][x]=0
            q.append((x,y))
ans=0
while q:
    x,y=q.popleft()
    d=dp[y][x]
    for dx,dy in dxy:
        nx,ny=x+dx,y+dy
        if not 0<=nx<w: continue
        if not 0<=ny<h: continue
        if dp[ny][nx]<=d+1: continue
        dp[ny][nx]=d+1
        q.append((nx,ny))
        ans=max(ans,d+1)
print(ans)

Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
a=[]
for i in range(H):
    a.append(list(input()))

from collections import deque
d = [(1,0),(0,1),(-1,0),(0,-1)]
l = deque()
for i in range(H):
    for j in range(W):
        if a[i][j]=="#":
            l.append((j,i))
            a[i][j] = 0
            while l:
                x,y = l.popleft()
            for o,p in d:
                if y+p<0 or y+p>=H or x+o<0 or x+o>=W:
                    continue
                if a[y+p][x+o] == ".":
                    a[y+p][x+o] = a[y][x] + 1
                    l.append((x+i,y+j))
for k in range(3):
    for i in range(H):
        for j in range(W):
            if a[i][j]==k:
                l.append((j,i))
                while l:
                    x,y = l.popleft()
                for o,p in d:
                    if y+p<0 or y+p>=H or x+o<0 or x+o>=W:
                        continue
                    if a[y+p][x+o] == ".":
                        a[y+p][x+o] = a[y][x] + 1
                        l.append((x+i,y+j))

aa=[flatten for inner in a for flatten in inner]
print(max(aa))

Fixed submission:
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
dxy=[(0,1),(1,0),(-1,0),(0,-1)]
inf=float("inf")

dp=[[inf]*w for _ in range(h)]
q=deque()

for y,l in enumerate(a):
    for x,c in enumerate(l):
        if c=="#":
            dp[y][x]=0
            q.append((x,y))
ans=0
while q:
    x,y=q.popleft()
    d=dp[y][x]
    for dx,dy in dxy:
        nx,ny=x+dx,y+dy
        if not 0<=nx<w: continue
        if not 0<=ny<h: continue
        if dp[ny][nx]<=d+1: continue
        dp[ny][nx]=d+1
        q.append((nx,ny))
        ans=max(ans,d+1)
print(ans)

Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
a=[]
for i in range(H):
    a.append(list(input()))

from collections import deque
d = [(1,0),(0,1),(-1,0),(0,-1)]
l = deque()
for i in range(H):
    for j in range(W):
        if a[i][j]=="#":
            l.append((j,i))
            a[i][j] = 0
            while l:
                x,y = l.popleft()
            for o,p in d:
                if y+p<0 or y+p>=H or x+o<0 or x+o>=W:
                    continue
                if a[y+p][x+o] == ".":
                    a[y+p][x+o] = a[y][x] + 1
                    l.append((x+i,y+j))
for k in range(5):
    for i in range(H):
        for j in range(W):
            if a[i][j]==k:
                l.append((j,i))
                while l:
                    x,y = l.popleft()
                for o,p in d:
                    if y+p<0 or y+p>=H or x+o<0 or x+o>=W:
                        continue
                    if a[y+p][x+o] == ".":
                        a[y+p][x+o] = a[y][x] + 1
                        l.append((x+i,y+j))

aa=[flatten for inner in a for flatten in inner]
print(max(aa))


Fixed submission:
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
dxy=[(0,1),(1,0),(-1,0),(0,-1)]
inf=float("inf")

dp=[[inf]*w for _ in range(h)]
q=deque()

for y,l in enumerate(a):
    for x,c in enumerate(l):
        if c=="#":
            dp[y][x]=0
            q.append((x,y))
ans=0
while q:
    x,y=q.popleft()
    d=dp[y][x]
    for dx,dy in dxy:
        nx,ny=x+dx,y+dy
        if not 0<=nx<w: continue
        if not 0<=ny<h: continue
        if dp[ny][nx]<=d+1: continue
        dp[ny][nx]=d+1
        q.append((nx,ny))
        ans=max(ans,d+1)
print(ans)