Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
inf=10**10
dist=[[inf if c=='.' else 0 for c in r] for r in a]
dq=deque()
for i in range(h):
    for j in range(w):
        if dist[i][j]==0:
            dq.append([i,j])
diff=[[1,0],[0,1],[-1,0],[0,-1]]
ans=0
while(dq):
    y,x=dq.popleft()
    for dy,dx in diff:
        y2=y+dy
        x2=x+dx
        if 0<=y2<h and 0<=x2<w and dist[y2][x2]>dist[y][x]+1:
            dq.append([y2,x2])
            dist[y2][x2]=dist[y][x]+1
            ans=max(ans,dist[y2][x2])
print(ans)

Fixed submission:
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
inf=10**10
dist=[[inf if c=='.' else 0 for c in r] for r in a]
dq=deque()
for i in range(h):
    for j in range(w):
        if dist[i][j]==0:
            dq.append([i,j])
diff=[[1,0],[0,1],[-1,0],[0,-1]]
ans=0
while(dq):
    y,x=dq.popleft()
    for dy,dx in diff:
        y2=y+dy
        x2=x+dx
        if 0<=y2<h and 0<=x2<w and dist[y2][x2]>dist[y][x]+1:
            dq.append([y2,x2])
            dist[y2][x2]=dist[y][x]+1
            ans=max(ans,dist[y2][x2])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h,w=map(int,input().split())
a=[input() for i in range(h)]
t=[[10000]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        if a[i][j]=='#':
            t[i][j]=0
flg=True
step=1
ans=0
while flg:
    flg=False
    for i in range(h):
        for j in range(w):
            if t[i][j]==step-1:
                if i-1>=0:
                    if t[i-1][j]>step:
                        t[i-1][j]=step
                        flg=True
                if i+1<h:
                    if t[i+1][j]>step:
                        t[i+1][j]=step
                        flg=True
                if j-1>=0:
                    if t[i][j-1]>step:
                        t[i][j-1]=step
                        flg=True
                if j+1<w:
                    if t[i][j+1]>step:
                        t[i][j+1]=step
                        flg=True
    if flg:
        ans=step
        step+=1
print(ans)

Fixed submission:
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
inf=10**10
dist=[[inf if c=='.' else 0 for c in r] for r in a]
dq=deque()
for i in range(h):
    for j in range(w):
        if dist[i][j]==0:
            dq.append([i,j])
diff=[[1,0],[0,1],[-1,0],[0,-1]]
ans=0
while(dq):
    y,x=dq.popleft()
    for dy,dx in diff:
        y2=y+dy
        x2=x+dx
        if 0<=y2<h and 0<=x2<w and dist[y2][x2]>dist[y][x]+1:
            dq.append([y2,x2])
            dist[y2][x2]=dist[y][x]+1
            ans=max(ans,dist[y2][x2])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h,w=map(int,input().split())
a=[input() for i in range(h)]
t=[[10000]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        if a[i][j]=='#':
            t[i][j]=0
flg=True
step=1
ans=0
while flg:
    flg=False
    for i in range(h):
        for j in range(w):
            if t[i][j]==step-1:
                if i-1>=0:
                    if t[i-1][j]>step:
                        t[i-1][j]=step
                        flg=True
                if i+1<h:
                    if t[i+1][j]>step:
                        t[i+1][j]=step
                        flg=True
                if j-1>=0:
                    if t[i][j-1]>step:
                        t[i][j-1]=step
                        flg=True
                if j+1<w:
                    if t[i][j+1]>step:
                        t[i][j+1]=step
                        flg=True
    if flg:
        ans=step
        step+=1
print(ans)

Fixed submission:
from collections import deque
h,w=map(int,input().split())
a=[input() for _ in range(h)]
inf=10**10
dist=[[inf if c=='.' else 0 for c in r] for r in a]
dq=deque()
for i in range(h):
    for j in range(w):
        if dist[i][j]==0:
            dq.append([i,j])
diff=[[1,0],[0,1],[-1,0],[0,-1]]
ans=0
while(dq):
    y,x=dq.popleft()
    for dy,dx in diff:
        y2=y+dy
        x2=x+dx
        if 0<=y2<h and 0<=x2<w and dist[y2][x2]>dist[y][x]+1:
            dq.append([y2,x2])
            dist[y2][x2]=dist[y][x]+1
            ans=max(ans,dist[y2][x2])
print(ans)