Buggy python submission (verdict: Time Limit Exceeded):


def main():
    import sys
    input = sys.stdin.readline
    
    H,W=map(int, input().split())
    S=[]
    
    import queue
    q=queue.Queue()
    
    for i in range(H):
        S.append(list(input()))
    
    for i in range(H):
        for j in range(W):
            if S[i][j]=="#":
                q.put((i,j))
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    dis=[[0]*W for _ in range(H)]
    
    while not q.empty():
        ii,jj=q.get()
        d=dis[ii][jj]
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            
            if 0<=nxt_i<=H-1:
                if 0<=nxt_j<=W-1:
                    if S[nxt_i][nxt_j]==".":
                        q.put((nxt_i,nxt_j))
                        S[nxt_i][nxt_j]="#"
                        dis[nxt_i][nxt_j]=d+1
    print(d)

                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def main():
    import sys
    input = sys.stdin.readline
    
    H,W=map(int, input().split())
    S=[]
    
    import queue
    q=queue.Queue()
    
    for i in range(H):
        S.append(list(input()))
    
    for i in range(H):
        for j in range(W):
            if S[i][j]=="#":
                q.put((i,j,0))
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    while not q.empty():
        ii,jj,d=q.get()
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            
            if 0<=nxt_i<=H-1:
                if 0<=nxt_j<=W-1:
                    if S[nxt_i][nxt_j]==".":
                        q.put((nxt_i,nxt_j,d+1))
                        S[nxt_i][nxt_j]="#"
    print(d)

                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def main():
    import sys
    input = sys.stdin.readline
    
    H,W=map(int, input().split())
    S=[]
    
    A=[[1]*(W)for _ in range(H)]
    import queue
    q=queue.Queue()
    
    for i in range(H):
        S.append(list(input()))
    
    for i in range(H):
        for j in range(W):
            if S[i][j]=="#":
                q.put((i,j,0))
            else:
                A[i][j]=0  
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    while not q.empty():
        ii,jj,d=q.get()
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            
            if 0<=nxt_i<=H-1:
                if 0<=nxt_j<=W-1:
                    if A[nxt_i][nxt_j]==0:
                        q.put((nxt_i,nxt_j,d+1))
                        A[nxt_i][nxt_j]=1
    print(d)

                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def main():
    import sys
    input = sys.stdin.readline
    
    H,W=map(int, input().split())
    S=[]
    
    A=[[1]*(W+2)for _ in range(H+2)]
    import queue
    q=queue.Queue()
    
    for i in range(H):
        S.append(list(input()))
    
    for i in range(H):
        for j in range(W):
            if S[i][j]=="#":
                q.put((i+1,j+1,0))
            else:
                A[i+1][j+1]=0  
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    while not q.empty():
        ii,jj,d=q.get()
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if A[nxt_i][nxt_j]==0:
                q.put((nxt_i,nxt_j,d+1))
                A[nxt_i][nxt_j]=1
    print(d)

                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def main():
    import sys
    input = sys.stdin.readline
    
    H,W=map(int, input().split())
    A=[[1]*(W+2)for _ in range(H+2)]
    import queue
    q=queue.Queue()
    
    for i in range(H):
        S=input()
        for j in range(W):
            if S[j]=="#":
                q.put((i+1,j+1,0))
            else:
                A[i+1][j+1]=0  
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    while not q.empty():
        ii,jj,d=q.get()
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if A[nxt_i][nxt_j]==0:
                q.put((nxt_i,nxt_j,d+1))
                A[nxt_i][nxt_j]=1
    print(d)

                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def main():
    import sys
    input = sys.stdin.readline
    
    H,W=map(int, input().split())
    A=[["#"]*(W+2)for _ in range(H+2)]
    import queue
    q=queue.Queue()
    
    for i in range(H):
        S=input()
        for j in range(W):
            A[i+1][j+1]=S[j]
            if S[j]=="#":
                q.put((i+1,j+1,0))
                
    
    

    
    inf=-1

                
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    ans=0
    
    while not q.empty():
        ii,jj,d=q.get()
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if A[nxt_i][nxt_j]==".":
                q.put((nxt_i,nxt_j,d+1))
                A[nxt_i][nxt_j]="#"
    print(d)

                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def main():
    H,W=map(int, input().split())
    A=[["#"]*(W+2)for _ in range(H+2)]
    import queue
    q=queue.Queue()
    
    for i in range(H):
        S=input()
        for j in range(W):
            A[i+1][j+1]=S[j]
            if S[j]=="#":
                q.put((i+1,j+1,0))
                
    
    

    
    inf=-1

                
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    ans=0
    
    while not q.empty():
        ii,jj,d=q.get()
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if A[nxt_i][nxt_j]==".":
                q.put((nxt_i,nxt_j,d+1))
                A[nxt_i][nxt_j]="#"
    print(d)

                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def main():
    H,W=map(int, input().split())
    A=[["#"]*(W+2)for _ in range(H+2)]
    
    for i in range(H):
        S=input()
        for j in range(W):
            A[i+1][j+1]=S[j]
    
    
    
    import queue
    q=queue.Queue()
    
    inf=-1
    for i in range(1,H+1):
        for j in range(1,W+1):
            if A[i][j]=="#":
                q.put((i,j,0))
                
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    ans=0
    
    while not q.empty():
        ii,jj,d=q.get()
        ans=max(ans,d)
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if A[nxt_i][nxt_j]==".":
                q.put((nxt_i,nxt_j,d+1))
                A[nxt_i][nxt_j]="#"
    print(ans)

                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    H,W=MI()
    A=[["#"]*(W+2)for _ in range(H+2)]
    
    for i in range(H):
        S=input()
        for j in range(W):
            A[i+1][j+1]=S[j]
    
    
    
    import queue
    q=queue.Queue()
    
    inf=-1
    dMap=[[inf]*(W+2)for _ in range(H+2)]
    for i in range(1,H+1):
        for j in range(1,W+1):
            if A[i][j]=="#":
                q.put((i,j,0))
                dMap[i][j]=0
                
    for i in range(H+2):
        dMap[i][0]=0
        dMap[i][-1]=0
        
    for j in range(W+2):
        dMap[0][j]=0
        dMap[-1][j]=0
        
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    while not q.empty():
        ii,jj,d=q.get()
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if dMap[nxt_i][nxt_j]==inf:
                q.put((nxt_i,nxt_j,d+1))
                dMap[nxt_i][nxt_j]=d+1
         
                
    ans=0
    for i in range(H+2):
        temp=max(dMap[i])
        ans=max(temp,ans)
    print(ans)
                
                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    H,W=MI()
    A=[]
    A.append(list("#"*(W+2)))
    for i in range(H):
        A.append(list("#"+input()+"#"))
    A.append(list("#"*(W+2)))
    
    
    import queue
    q=queue.Queue()
    
    inf=-1
    dMap=[[inf]*(W+2)for _ in range(H+2)]
    for i in range(1,H+1):
        for j in range(1,W+1):
            if A[i][j]=="#":
                q.put((i,j,0))
                dMap[i][j]=0
                
    for i in range(H+2):
        dMap[i][0]=0
        dMap[i][-1]=0
        
    for j in range(W+2):
        dMap[0][j]=0
        dMap[-1][j]=0
        
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    

    while not q.empty():
        ii,jj,d=q.get()
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if dMap[nxt_i][nxt_j]==inf:
                q.put((nxt_i,nxt_j,d+1))
                dMap[nxt_i][nxt_j]=d+1
                
    ans=0
    for i in range(H+2):
        temp=max(dMap[i])
        ans=max(temp,ans)
    print(ans)
                
                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    H,W=MI()
    A=[]
    A.append(list("#"*(W+2)))
    for i in range(H):
        A.append(list("#"+input()+"#"))
    A.append(list("#"*(W+2)))
    
    
    import queue
    q=queue.Queue()
    
    inf=-1
    dMap=[[inf]*(W+2)for _ in range(H+2)]
    for i in range(1,H+1):
        for j in range(1,W+1):
            if A[i][j]=="#":
                q.put((i,j,0))
                dMap[i][j]=0
                
    for i in range(H+2):
        dMap[i][0]=0
        dMap[i][-1]=0
        
    for j in range(W+2):
        dMap[0][j]=0
        dMap[-1][j]=0
        
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    ans=0
    while not q.empty():
        ii,jj,d=q.get()
        dMap[ii][jj]=d
        ans=max(ans,dMap[ii][jj])
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if dMap[nxt_i][nxt_j]==inf:
                q.put((nxt_i,nxt_j,d+1))
                
    print(ans)
                
                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    H,W=MI()
    A=[]
    A.append(list("#"*(W+2)))
    for i in range(H):
        A.append(list("#"+input()+"#"))
    A.append(list("#"*(W+2)))
    
    
    import queue
    q=queue.Queue()
    
    for i in range(1,H+1):
        for j in range(1,W+1):
            if A[i][j]=="#":
                q.put((i,j,0))
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    inf=10**10
    dMap=[[inf]*(W+2)for _ in range(H+2)]
    
    ans=0
    while not q.empty():
        ii,jj,d=q.get()
        dMap[ii][jj]=min(dMap[ii][jj],d)
        ans=max(ans,dMap[ii][jj])
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if dMap[nxt_i][nxt_j]==inf:
                if A[nxt_i][nxt_j]==".":
                    q.put((nxt_i,nxt_j,d+1))
                
    print(ans)
                
                
    
            

main()


Fixed submission:
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

print(ans)