Problem p03053 (python)

Fixed submission:
from collections import deque

class Grid():
    def __init__(self,i,j,isblack):
        self.i=i
        self.j=j
        self.isblack=isblack
        self.distance=0

H,W=map(int,input().split())

g=[]
for i in range(H):
    Ai=input()
    gi=[]    
    for j in range(W):
        gi.append(Grid(i,j,Ai[j]=='#'))
    g.append(gi)

d=deque()

# 黒マスのreachedをTrueにし，dequeに入れる
for row in g:
    for grid in row:
        if grid.isblack:
            d.append(grid)

while d:
    tg=d.popleft()
    i=tg.i
    j=tg.j
    if i>0 and not g[i-1][j].isblack:
        g[i-1][j].isblack=True
        g[i-1][j].distance=tg.distance+1
        d.append(g[i-1][j])
    if i<H-1 and not g[i+1][j].isblack:
        g[i+1][j].isblack=True
        g[i+1][j].distance=tg.distance+1
        d.append(g[i+1][j])
    if j>0 and not g[i][j-1].isblack:
        g[i][j-1].isblack=True
        g[i][j-1].distance=tg.distance+1
        d.append(g[i][j-1])
    if j<W-1 and not g[i][j+1].isblack:
        g[i][j+1].isblack=True
        g[i][j+1].distance=tg.distance+1
        d.append(g[i][j+1])

'''
for row in g:
    for grid in row:
#        print(grid.distance,end=' ')
#    print()
'''

print(max([max([grid.distance for grid in row]) for row in g]))

Buggy submission (verdict: Runtime Error):
from collections import deque

class Grid():
    def __init__(self,i,j,isblack):
        self.i=i
        self.j=j
        self.isblack=isblack
        self.distance=0
    def __str__(self):
        print(f'i={i},j={j},isblack={isblack},distance={distance}')

H,W=map(int,input().split())

g=[]
for i in range(H):
    Ai=input()
    gi=[]    
    for j in range(W):
        gi.append(Grid(i,j,Ai[j]=='#'))
    g.append(gi)

d=deque()

# 黒マスのreachedをTrueにし，dequeに入れる
for row in g:
    for grid in row:
        if grid.isblack:
            d.append(grid)

while d:
    tg=d.popleft()
    i=tg.i
    j=tg.j
    if i>0 and not g[i-1][j].isblack:
        g[i-1][j].isblack=True
        g[i-1][j].distance=tg.distance+1
        d.append(g[i-1][j])
    if i<H-1 and not g[i+1][j].isblack:
        g[i+1][j].isblack=True
        g[i+1][j].distance=tg.distance+1
        d.append(g[i+1][j])
    if j>0 and not g[i][j-1].isblack:
        g[i][j-1].isblack=True
        g[i][j-1].distance=tg.distance+1
        d.append(g[i][j-1])
    if j<W-1 and not g[i][j+1].isblack:
        g[i][j+1].isblack=True
        g[i][j+1].distance=tg.distance+1
        d.append(g[i][j+1])

'''
for row in g:
    for grid in row:
#        print(grid.distance,end=' ')
#    print()
'''

print(max([max([grid.distance for grid in row]) for row in g]))

Buggy submission (verdict: Runtime Error):
from collections import deque

class Grid():
    def __init__(self,i,j,isblack):
        self.i=i
        self.j=j
        self.isblack=isblack
        self.distance=0
    def __str__(self):
        print(f'i={i},j={j},isblack={isblack},distance={distance}')

H,W=map(int,input().split())

g=[]
for i in range(H):
    Ai=input()
    gi=[]    
    for j in range(W):
        gi.append(Grid(i,j,Ai[j]=='#'))
    g.append(gi)

d=deque()

# 黒マスのreachedをTrueにし，dequeに入れる
for row in g:
    for grid in row:
        if grid.isblack:
            d.append(grid)

while d:
    tg=d.popleft()
    i=tg.i
    j=tg.j
    if i>0 and not g[i-1][j].isblack:
        g[i-1][j].isblack=True
        g[i-1][j].distance=tg.distance+1
        d.append(g[i-1][j])
    if i<H-1 and not g[i+1][j].isblack:
        g[i+1][j].isblack=True
        g[i+1][j].distance=tg.distance+1
        d.append(g[i+1][j])
    if j>0 and not g[i][j-1].isblack:
        g[i][j-1].isblack=True
        g[i][j-1].distance=tg.distance+1
        d.append(g[i][j-1])
    if j<W-1 and not g[i][j+1].isblack:
        g[i][j+1].isblack=True
        g[i][j+1].distance=tg.distance+1
        d.append(g[i][j+1])

'''
for row in g:
    for grid in row:
#        print(grid.distance,end=' ')
#    print()
'''

print(max([max([grid.distance for grid in row]) for row in g]))

Buggy submission (verdict: Runtime Error):
INF=1999
H,W=map(int,input().split())
A=[]
for i in range(H):
    A.append(input())
c=[[INF for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        if A[i][j]=='#':
            c[i][j]=0
            l=1
            while i-l>=0 and A[i-l][j]=='.':
                c[i-l][j]=min(c[i-l][j],l)
                l+=1
            r=1
            while i+r<=W-1 and A[i+r][j]=='.':
                c[i+r][j]=min(c[i+r][j],r)
                r+=1
            u=1
            while j-u>=0 and A[i][j-u]=='.':
                c[i][j-u]=min(c[i][j-u],u)
                u+=1
            d=1
            while j+d<=H-1 and A[i][j+d]=='.':
                c[i][j+d]=min(c[i][j+d],d)
                d+=1

for i in range(H):
    for j in range(W):
        if 0<c[i][j]<INF:
            l=1
            while i-l>=0 and A[i-l][j]=='.':
                c[i-l][j]=min(c[i-l][j],c[i][j]+l)
                l+=1
            r=1
            while i+r<=W-1 and A[i+r][j]=='.':
                c[i+r][j]=min(c[i+r][j],c[i][j]+r)
                r+=1
            u=1
            while j-u>=0 and A[i][j-u]=='.':
                c[i][j-u]=min(c[i][j-u],c[i][j]+u)
                u+=1
            d=1
            while j+d<=H-1 and A[i][j+d]=='.':
                c[i][j+d]=min(c[i][j+d],c[i][j]+d)
                d+=1

ans=max([max(c[i]) for i in range(H)])

print(ans)

Buggy submission (verdict: Runtime Error):
INF=1999
H,W=map(int,input().split())
A=[]
for i in range(H):
    A.append(input())
c=[[INF for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        if A[i][j]=='#':
            c[i][j]=0
            l=1
            while i-l>=0 and A[i-l][j]=='.':
                c[i-l][j]=min(c[i-l][j],l)
                l+=1
            r=1
            while i+r<=W-1 and A[i+r][j]=='.':
                c[i+r][j]=min(c[i+r][j],r)
                r+=1
            u=1
            while j-u>=0 and A[i][j-u]=='.':
                c[i][j-u]=min(c[i][j-u],u)
                u+=1
            d=1
            while j+d<=H-1 and A[i][j+d]=='.':
                c[i][j+d]=min(c[i][j+d],d)
                d+=1

for i in range(H):
    for j in range(W):
        if 0<c[i][j]<INF:
            l=1
            while i-l>=0 and A[i-l][j]=='.':
                c[i-l][j]=min(c[i-l][j],c[i][j]+l)
                l+=1
            r=1
            while i+r<=W-1 and A[i+r][j]=='.':
                c[i+r][j]=min(c[i+r][j],c[i][j]+r)
                r+=1
            u=1
            while j-u>=0 and A[i][j-u]=='.':
                c[i][j-u]=min(c[i][j-u],c[i][j]+u)
                u+=1
            d=1
            while j+d<=H-1 and A[i][j+d]=='.':
                c[i][j+d]=min(c[i][j+d],c[i][j]+d)
                d+=1

ans=max([max(c[i]) for i in range(H)])

print(ans)