n, m=gets.split.map &:to_i
a=(1..n).map{gets.chomp.bytes.map{|e|e%2}}

q=[]
a.each_with_index do |v, i|
    v.each_with_index do |c, j|
        q << i*m+j if a[i][j]==1
    end
end

res=0
h=-1
while t=q[h+=1]
    res=a[r=t/m][c=t%m]-1
    if r>0 && a[nr=r-1][nc=c]==0
        a[nr][nc]=a[r][c]+1
        q << nr*m+nc
    end
    if r<n-1 && a[nr=r+1][nc=c]==0
        a[nr][nc]=a[r][c]+1
        q << nr*m+nc
    end
    if c>0 && a[nr=r][nc=c-1]==0
        a[nr][nc]=a[r][c]+1
        q << nr*m+nc
    end
    if c<m-1 && a[nr=r][nc=c+1]==0
        a[nr][nc]=a[r][c]+1
        q << nr*m+nc
    end
end

p res
