h, w = gets.split.map(&:to_i)
cells_to_color = Array.new(h) {Array.new(w, nil)}
color_to_cells = Hash.new {|h, k| h[k] = []}

h.times do |i|
  gets.chomp.each_char.with_index do |ch, j|
    is_black = (ch == '#')
    cells_to_color[i][j] = is_black
    color_to_cells[is_black] << [i, j]
  end
end

def round(idx, upper_limit)
  return 0 if idx.negative?
  return upper_limit if idx > upper_limit
  idx
end

def get_neighbour(row, column, hight, width)
  upper = round(row - 1, hight)
  lower = round(row + 1, hight)
  right = round(column - 1, width)
  left = round(column + 1, width)
  [[upper, column], [lower, column], [row, right], [row, left]]
end

cnt = 0

while true
  break if color_to_cells[false].empty?
  black = []
  white = []
  while white_cell = color_to_cells[false].shift
    i, j = white_cell
    is_black = get_neighbour(i, j, h-1, w-1).any? {|n_i,n_j| cells_to_color[n_i][n_j]}
    if is_black
      black << [i, j]
    else
      white << [i, j]
    end
  end
  color_to_cells[false] = white
  black.each {|b_i, b_j| cells_to_color[b_i][b_j] = true}
  cnt += 1
end

puts cnt