h,w = gets.split.map(&:to_i)
a = h.times.map do
  gets.chomp.chars
end

total = h*w

n = 0
while true
  count = a.map {|s| s.count("#")}.reduce(:+)
  break if count == total
  b = (0...h).map do |y|
    (0...w).map do |x|
      a[y][x] == "#" || (x > 0 && a[y][x-1] == "#") || (x < w-1 && a[y][x+1] == "#") ||
        (y > 0 && a[y-1][x] == "#") || (y < h-1 && a[y+1][x] == "#") ? "#" : "."
    end.join
  end
  n += 1
  a = b
end

puts n