h, w = gets.strip.split.map(&:to_i)
aa = readlines.map {|line| line.strip.split('') }

def blacken(aa)
  h=aa.length
  w = aa.first.length
  d = [0, 1, 0, -1]

  targets = []
  h.times do |i|
    w.times do|j|
      if aa[i][j]=='#'
        4.times do |idx|
          y, x = [i+d[idx], j+d[idx^1]]
          next if !((0...h)===y) || !((0...w)===x) || aa[y][x]=='#'
          targets.push( [y, x] )
        end
      end
    end
  end

  targets.uniq.each do |y, x|
    aa[y][x]='#'
  end

  aa
end

def all_black?(aa)
  !aa.any? {|line| line.any?{|e| e=='.' } }
end

if aa.map {|l| l.count {|e| e=='#' } }.inject(:+)==1
  h.times do |i|
    w.times do|j|
      if aa[i][j]=='#'
        b_y, b_x = [i, j]
        long_h = [h - b_y - 1, b_y].max
        long_w = [h - b_x - 1, b_x].max

        p long_h + long_w
        exit;
      end
    end
  end

end


result = 0
while true
  if all_black?(aa)
    p result; exit
  end
  aa = blacken(aa)
  result += 1
end



