Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W=map(int,input().split())

L=[]
Q=deque()
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]==".":
            a[j]=10000
        else:
            a[j]=0
            Q.append([i,j,0])
    L.append(a)
    

#print(L)
#print(Q)
cnt=0
X=[1,-1,0,0]
Y=[0,0,1,-1]
for i in range(10**6+5):
    if len(Q)==0:
        break
    else:
        #print(Q)
        QZ=Q.popleft()
        for j in range(4):
            
            if (QZ[0]+1)<=(H-1) and j==0:
                #print(L[Q[0][0]+X[j]][Q[0][1]+Y[j]])
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=Q[0][2]+1
            if (QZ[0]-1)>=(0) and j==1:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
            if (QZ[1]+1)<=(W-1) and j==2:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (QZ[1]-1)>=(0) and j==3:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
        #print(L)
      

print(cnt)
#print(L)

Fixed submission:
from collections import deque
H,W=map(int,input().split())

L=[]
Q=deque()
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]==".":
            a[j]=10000
        else:
            a[j]=0
            Q.append([i,j,0])
    L.append(a)
    

#print(L)
#print(Q)
cnt=0
X=[1,-1,0,0]
Y=[0,0,1,-1]
for i in range(10**6+5):
    if len(Q)==0:
        break
    else:
        #print(Q)
        QZ=Q.popleft()
        for j in range(4):
            
            if (QZ[0]+1)<=(H-1) and j==0:
                #print(L[Q[0][0]+X[j]][Q[0][1]+Y[j]])
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=Q[0][2]+1
            if (QZ[0]-1)>=(0) and j==1:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
            if (QZ[1]+1)<=(W-1) and j==2:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (QZ[1]-1)>=(0) and j==3:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
        #print(L)
      

print(cnt)
#print(L)

Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())

L=[]
Q=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]==".":
            a[j]=10000
        else:
            a[j]=0
            Q.append([i,j,0])
    L.append(a)
    

#print(L)
#print(Q)
cnt=0
X=[1,-1,0,0]
Y=[0,0,1,-1]
for i in range(10**6+5):
    if Q==[]:
        break
    else:
        for j in range(4):
            if (Q[0][0]+1)<=(H-1) and j==0:
                #print(L[Q[0][0]+X[j]][Q[0][1]+Y[j]])
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (Q[0][0]-1)>=(0) and j==1:
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (Q[0][1]+1)<=(W-1) and j==2:
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (Q[0][1]-1)>=(0) and j==3:
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
        del Q[0]

print(cnt)

Fixed submission:
from collections import deque
H,W=map(int,input().split())

L=[]
Q=deque()
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]==".":
            a[j]=10000
        else:
            a[j]=0
            Q.append([i,j,0])
    L.append(a)
    

#print(L)
#print(Q)
cnt=0
X=[1,-1,0,0]
Y=[0,0,1,-1]
for i in range(10**6+5):
    if len(Q)==0:
        break
    else:
        #print(Q)
        QZ=Q.popleft()
        for j in range(4):
            
            if (QZ[0]+1)<=(H-1) and j==0:
                #print(L[Q[0][0]+X[j]][Q[0][1]+Y[j]])
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=Q[0][2]+1
            if (QZ[0]-1)>=(0) and j==1:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
            if (QZ[1]+1)<=(W-1) and j==2:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (QZ[1]-1)>=(0) and j==3:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
        #print(L)
      

print(cnt)
#print(L)

Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())

L=[]
Q=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]==".":
            a[j]=10000
        else:
            a[j]=0
            Q.append([i,j,0])
    L.append(a)
    

#print(L)
#print(Q)
cnt=0
X=[1,-1,0,0]
Y=[0,0,1,-1]
for i in range(10**6+5):
    if Q==[]:
        break
    else:
        for j in range(4):
            if (Q[0][0]+1)<=(H-1) and j==0:
                #print(L[Q[0][0]+X[j]][Q[0][1]+Y[j]])
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (Q[0][0]-1)>=(0) and j==1:
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (Q[0][1]+1)<=(W-1) and j==2:
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (Q[0][1]-1)>=(0) and j==3:
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
        del Q[0]

print(cnt)

Fixed submission:
from collections import deque
H,W=map(int,input().split())

L=[]
Q=deque()
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]==".":
            a[j]=10000
        else:
            a[j]=0
            Q.append([i,j,0])
    L.append(a)
    

#print(L)
#print(Q)
cnt=0
X=[1,-1,0,0]
Y=[0,0,1,-1]
for i in range(10**6+5):
    if len(Q)==0:
        break
    else:
        #print(Q)
        QZ=Q.popleft()
        for j in range(4):
            
            if (QZ[0]+1)<=(H-1) and j==0:
                #print(L[Q[0][0]+X[j]][Q[0][1]+Y[j]])
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=Q[0][2]+1
            if (QZ[0]-1)>=(0) and j==1:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
            if (QZ[1]+1)<=(W-1) and j==2:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (QZ[1]-1)>=(0) and j==3:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
        #print(L)
      

print(cnt)
#print(L)

Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())

A=[]
for i in range(H):
    a=list(input())
    A.append(a)
    
L=[[10000 for i in range(W)]for j in range(H)]
Q=[]
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            L[i][j]=0
            Q.append([i,j,0])
#print(L)
#print(Q)
cnt=0
X=[1,-1,0,0]
Y=[0,0,1,-1]
for i in range(10**6+5):
    if Q==[]:
        break
    else:
        for j in range(4):
            if (Q[0][0]+1)<=(H-1) and j==0:
                #print(L[Q[0][0]+X[j]][Q[0][1]+Y[j]])
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
            if (Q[0][0]-1)>=(0) and j==1:
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
            if (Q[0][1]+1)<=(W-1) and j==2:
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
            if (Q[0][1]-1)>=(0) and j==3:
                if L[Q[0][0]+X[j]][Q[0][1]+Y[j]]>(Q[0][2]+1):
                    L[Q[0][0]+X[j]][Q[0][1]+Y[j]]=(Q[0][2]+1)
                    Q.append([Q[0][0]+X[j],Q[0][1]+Y[j],Q[0][2]+1])
        del Q[0]
ans=0
for i in range(H):
    for j in range(W):
        if L[i][j]>ans:
            ans=L[i][j]
print(ans)

Fixed submission:
from collections import deque
H,W=map(int,input().split())

L=[]
Q=deque()
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]==".":
            a[j]=10000
        else:
            a[j]=0
            Q.append([i,j,0])
    L.append(a)
    

#print(L)
#print(Q)
cnt=0
X=[1,-1,0,0]
Y=[0,0,1,-1]
for i in range(10**6+5):
    if len(Q)==0:
        break
    else:
        #print(Q)
        QZ=Q.popleft()
        for j in range(4):
            
            if (QZ[0]+1)<=(H-1) and j==0:
                #print(L[Q[0][0]+X[j]][Q[0][1]+Y[j]])
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=Q[0][2]+1
            if (QZ[0]-1)>=(0) and j==1:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
            if (QZ[1]+1)<=(W-1) and j==2:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<Q[0][2]+1:
                        cnt=Q[0][2]+1
            if (QZ[1]-1)>=(0) and j==3:
                if L[QZ[0]+X[j]][QZ[1]+Y[j]]>(QZ[2]+1):
                    L[QZ[0]+X[j]][QZ[1]+Y[j]]=(QZ[2]+1)
                    Q.append([QZ[0]+X[j],QZ[1]+Y[j],QZ[2]+1])
                    if cnt<QZ[2]+1:
                        cnt=QZ[2]+1
        #print(L)
      

print(cnt)
#print(L)