io = STDIN
h,w=io.gets.split.map(&:to_i)
$board=[]
$board << ["."]*(w+2)
h.times do
  $board << ["."]+io.gets.chomp.split(//)+["."]
end
$board << ["."]*(w+2)
require 'pp'
pp $board
def check(h,w)
  res = false
  stack=[]
  (1..h).each do |i|
    (1..w).each do |j|
      if $board[i][j] == "."
        if [[1,0],[0,-1],[-1,0],[0,1]].any?{|x,y| $board[i+x][j+y]=="#"}
          stack << [i,j]
        end
        res=true
      end
    end
  end
  stack.each do |i,j|
    $board[i][j]="#"
  end
  res
end
cnt=0
while (check(h,w))
  cnt+=1
  pp $board
end
p cnt
