Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

h,w = map(int,readline().split())
b = "".join(read().split())

from collections import deque
q = deque()

dist = [-1]*h*w
for i,ci in enumerate(b):
    if ci == "#":
        dist[i] = 0
        q.append(i)
tot = h*w

def update(nv,dv):
    if dist[nv] == -1:
        q.append((nv,dv+1))
        dist[nv] = dv+1

while q:
    dv,v = q.popleft()
    # if b[v]==".":
    if v%w != w-1: update(v+1,dv)
    if v%w:        update(v-1,dv)
    if v >= w:     update(v-w,dv)
    if v+w < tot:  update(v+w,dv)
    #if v%w != w-1 and b[v+1]==".": update(v+1,dv)
    #if v%w        and b[v-1]==".": update(v-1,dv)
    #if v >= w     and b[v-w]==".": update(v-w,dv)
    #if v+w < tot  and b[v+w]==".": update(v+w,dv)


print(max(dist))

if h==3 and w==3: print(1)


Fixed submission:
# coding: utf-8
# Your code here!
import collections

h,w = [int(i) for i in input().split()]
board = [input() for j in range(h)] # 縦「n」行

d4 = [[1,0],[0,1],[-1,0],[0,-1]]
memo = [[-1]*w for j in range(h)]
dist = [[100000]*w for j in range(h)]


q = collections.deque()
for i in range(h):
    for j in range(w):
        if board[i][j] == "#":
            memo[i][j] = 1
            q.append((i,j,0))

while len(q) != 0:
#    print(memo)
    i,j,c = q.popleft()
    dist[i][j] = c
    for k in d4:
#        print(k)
        ii,jj = k
        if  i+ii < 0 or i+ii>=h or j+jj <0 or j+jj>=w:
#            print("out",i+ii,j+jj)
            continue
        if memo[i+ii][j+jj] < 0:
#            print("put")
            q.append((i+ii,j+jj,c+1))
            memo[i+ii][j+jj] = 1

ans = 0      
for i in range(h):
    for j in range(w):
        ans = max(ans,dist[i][j])

print(ans)
#print(dist)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!
import collections

h,w = [int(i) for i in input().split()]
board = [input() for j in range(h)] # 縦「n」行

d4 = [[1,0],[0,1],[-1,0],[0,-1]]
memo = [[100000]*w for j in range(h)]


q = collections.deque()
for i in range(h):
    for j in range(w):
        if board[i][j] == "#":
            memo[i][j] = 0
            q.append((i,j,0))

while len(q) != 0:
#    print(memo)
    i,j,c = q.popleft()
#    print(i,j)
    for k in d4:
#        print(k)
        ii,jj = k
        if  i+ii < 0 or i+ii>=h or j+jj <0 or j+jj>=w:
#            print("out",i+ii,j+jj)
            continue
        if memo[i+ii][j+jj] > 10000:
#            print("put")
            q.append((i+ii,j+jj,c+1))
        memo[i][j] = min(c,memo[i][j])
    
    
ans = 0      
for i in range(h):
    for j in range(w):
        ans = max(ans,memo[i][j])

print(ans)





                
            
        

Fixed submission:
# coding: utf-8
# Your code here!
import collections

h,w = [int(i) for i in input().split()]
board = [input() for j in range(h)] # 縦「n」行

d4 = [[1,0],[0,1],[-1,0],[0,-1]]
memo = [[-1]*w for j in range(h)]
dist = [[100000]*w for j in range(h)]


q = collections.deque()
for i in range(h):
    for j in range(w):
        if board[i][j] == "#":
            memo[i][j] = 1
            q.append((i,j,0))

while len(q) != 0:
#    print(memo)
    i,j,c = q.popleft()
    dist[i][j] = c
    for k in d4:
#        print(k)
        ii,jj = k
        if  i+ii < 0 or i+ii>=h or j+jj <0 or j+jj>=w:
#            print("out",i+ii,j+jj)
            continue
        if memo[i+ii][j+jj] < 0:
#            print("put")
            q.append((i+ii,j+jj,c+1))
            memo[i+ii][j+jj] = 1

ans = 0      
for i in range(h):
    for j in range(w):
        ans = max(ans,dist[i][j])

print(ans)
#print(dist)