r, c = gets.split.map(&:to_i)

maze = []
r.times {maze << gets.chomp.split('')}

nxy = [[0, 1], [1, 0], [0, -1], [-1, 0]]

cnt = 0
while maze.flatten.select {|e| e == '.'}.size.positive? && (cnt += 1)
  (0..r - 1).each do |rt|
    (0..c - 1).each do |ct|
      if maze[rt][ct] == '.'
        nxy.each do |xy|
          ap_x = rt + xy[0]
          ap_y = ct + xy[1]
          next if ap_x < 0 || ap_x >= r || ap_y < 0 || ap_y >= c
          next if maze[ap_x][ap_y] == '.' || maze[ap_x][ap_y] == cnt
          maze[rt][ct] = cnt
        end
      end
    end
  end
end

p cnt
