Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H,W=map(int,input().split())
d={}
for i in range(H):    
    for j,c in enumerate(input()):
        d[(i,j)]=0 if c=='#' else -1
q=deque()
for s in d:
    if not d[s]:
        q.append(s)
while q:
    u=q.popleft()
    ui,uj=u
    for v in [(ui-1,uj),(ui+1,uj),(ui,uj-1),(ui,uj+1)]:
        vi,vj=v
        if 0<=vi<=H-1 and 0<=vj<=W-1 and d[v]==-1:
            d[v]=d[u]+1
            q.append(v)
print(max(d.values()))

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 python submission (verdict: Time Limit Exceeded):
from collections import deque

H,W=map(int,input().split())
d={}
for i in range(H):    
    for j,c in enumerate(input()):
        d[(i,j)]=0 if c=='#' else -1
q=deque()
for s in d:
    if not d[s]:
        q.append(s)
while q:
    u=q.popleft()
    ui,uj=u
    for v in [(ui-1,uj),(ui+1,uj),(ui,uj-1),(ui,uj+1)]:
        vi,vj=v
        if 0<=vi<=H-1 and 0<=vj<=W-1 and d[v]==-1:
            d[v]=d[u]+1
            q.append(v)
print(max(d.values()))

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 python submission (verdict: Time Limit Exceeded):
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]))

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 python 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]))

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 python 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]))

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 python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())
INF=H+W-1
a=[]
for i in range(H):
    ai=[]
    Ai=input()
    for j in range(W):
        if Ai[j]=='.':
            ai.append(False)
        else:
            ai.append(True)
    a.append(ai)

# print(a)

c=[[-1 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
        else:
            c[i][j]=INF

# print(c)

for i in range(H):
    for j in range(W):
        if a[i][j]:
            k=1
            while i-k>=0 and not a[i-k][j]:
                c[i-k][j]=min(c[i-k][j],k)
                k+=1
            k=1
            while i+k<=H-1 and not a[i+k][j]:
                c[i+k][j]=min(c[i+k][j],k)
                k+=1
            k=1
            while j-k>=0 and not a[i][j-k]:
                c[i][j-k]=min(c[i][j-k],k)
                k+=1
            k=1
            while j+k<=W-1 and not a[i][j+k]:
                c[i][j+k]=min(c[i][j+k],k)
                k+=1

# print(c)

for i in range(H):
    for j in range(W):
        if 0<c[i][j]<INF:
            k=1
            while i-k>=0 and not a[i-k][j]:
                c[i-k][j]=min(c[i-k][j],c[i][j]+k)
                k+=1
            k=1
            while i+k<=H-1 and not a[i+k][j]:
                c[i+k][j]=min(c[i+k][j],c[i][j]+k)
                k+=1
            k=1
            while j-k>=0 and not a[i][j-k]:
                c[i][j-k]=min(c[i][j-k],c[i][j]+k)
                k+=1
            k=1
            while j+k<=W-1 and not a[i][j+k]:
                c[i][j+k]=min(c[i][j+k],c[i][j]+k)
                k+=1

# print(c)

ans=max([max(ci) for ci in c])

print(ans)

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 python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())
INF=H+W-1
a=[]
for i in range(H):
    ai=[]
    Ai=input()
    for j in range(W):
        if Ai[j]=='.':
            ai.append(False)
        else:
            ai.append(True)
    a.append(ai)

# print(a)

c=[[-1 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
        else:
            c[i][j]=INF

# print(c)

for i in range(H):
    for j in range(W):
        if a[i][j]:
            k=1
            while i-k>=0 and not a[i-k][j]:
                c[i-k][j]=min(c[i-k][j],k)
                k+=1
            k=1
            while i+k<=H-1 and not a[i+k][j]:
                c[i+k][j]=min(c[i+k][j],k)
                k+=1
            k=1
            while j-k>=0 and not a[i][j-k]:
                c[i][j-k]=min(c[i][j-k],k)
                k+=1
            k=1
            while j+k<=W-1 and not a[i][j+k]:
                c[i][j+k]=min(c[i][j+k],k)
                k+=1

# print(c)

for i in range(H):
    for j in range(W):
        if 0<c[i][j]<INF:
            k=1
            while i-k>=0 and not a[i-k][j]:
                c[i-k][j]=min(c[i-k][j],c[i][j]+k)
                k+=1
            k=1
            while i+k<=H-1 and not a[i+k][j]:
                c[i+k][j]=min(c[i+k][j],c[i][j]+k)
                k+=1
            k=1
            while j-k>=0 and not a[i][j-k]:
                c[i][j-k]=min(c[i][j-k],c[i][j]+k)
                k+=1
            k=1
            while j+k<=W-1 and not a[i][j+k]:
                c[i][j+k]=min(c[i][j+k],c[i][j]+k)
                k+=1

# print(c)

ans=max([max(ci) for ci in c])

print(ans)

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 python 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)                  

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 python 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)                  

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 python submission (verdict: Time Limit Exceeded):
def isallblack(a):
    h=len(a)-2
    w=len(a[0])-2
    for i in range(1,h+1):
        for j in range(1,w+1):
            if a[i][j]=='.':
                return False
    return True

H,W=map(int,input().split())
a=['.'*(W+2)]
for i in range(H):
    a.append('.'+input()+'.')
a.append('.'*(W+2))

c=[[False for j in range(W+2)] for i in range(H+2)]
cnt=0

while not isallblack(a):
    for i in range(1,H+1):
        for j in range(1,W+1):
            c[i][j]=(a[i][j]=='#' or a[i-1][j]=='#' or a[i+1][j]=='#' or a[i][j-1]=='#' or a[i][j+1]=='#')
    for i in range(1,H+1):
        for j in range(1,W+1):
            if c[i][j]:
                a[i]=a[i][:j]+'#'+a[i][j+1:]
    cnt+=1

print(cnt)

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 python submission (verdict: Time Limit Exceeded):
def isallblack(a):
    h=len(a)-2
    w=len(a[0])-2
    for i in range(1,h+1):
        for j in range(1,w+1):
            if a[i][j]=='.':
                return False
    return True

H,W=map(int,input().split())
a=['.'*(W+2)]
for i in range(H):
    a.append('.'+input()+'.')
a.append('.'*(W+2))

c=[[False for j in range(W+2)] for i in range(H+2)]
cnt=0

while not isallblack(a):
    for i in range(1,H+1):
        for j in range(1,W+1):
            c[i][j]=(a[i][j]=='#' or a[i-1][j]=='#' or a[i+1][j]=='#' or a[i][j-1]=='#' or a[i][j+1]=='#')
    for i in range(1,H+1):
        for j in range(1,W+1):
            if c[i][j]:
                a[i]=a[i][:j]+'#'+a[i][j+1:]
    cnt+=1

print(cnt)

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]))