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

dr=[-1, 0, 1, 0]
dc=[0, -1, 0, 1]
res=0
while !q.empty?
    t=q.shift
    r=t/m
    c=t%m
    k=a[r][c]
    res=k-1 if res<k-1
    (0..3).each do |i|
        nr=r+dr[i]
        nc=c+dc[i]
        next if nr<0 or nr>=n or nc<0 or nc>=m or a[nr][nc]>0
        q << nr*m+nc
        a[nr][nc]=k+1
    end
end

p res
