h, w = gets.split(" ").map(&:to_i)
boards = []
blanks = []
h.times do
  boards.push(gets.split("").map(&:to_s))
end

(w + 2).times do
  blanks.push(".")
end

h.times do |index|
  boards[index].insert(w, ".")
  boards[index].unshift(".")
end

boards.push(blanks)
boards.insert(0, blanks)

changed = true
count = 0
loop_times = 0

while changed == true do
  changed = false
  loop_times += 1
  (h + 1).times do |index|
    if index < 1
      next
    end
    (w + 1).times do |index2|
      if index2 < 1
        next
      end
      if (boards[index + 1][index2] != "." && boards[index + 1][index2] != loop_times) || (boards[index - 1][index2] != "." && boards[index - 1][index2] != loop_times) || (boards[index][index2 + 1] != "." && boards[index][index2 + 1] != loop_times) || (boards[index][index2 - 1] != "." && boards[index][index2 - 1] != loop_times)
        if boards[index][index2] == "."
          boards[index][index2] = loop_times
          changed = true
        end
      end
    end
  end
if changed == true
  count += 1
end
end

puts count 