let () = Scanf.scanf "%d %d\n" @@ fun h w ->
  let ass = Array.init h @@ fun _ -> Scanf.scanf "%s\n" @@ fun s -> s in
  let rec bfs n ijs =
    match
      List.fold_left (fun (b, ijs) (i, j) ->
        if
          i < 0 || h <= i ||
          j < 0 || w <= j ||
          ass.(i).[j] = '#'
        then (b, ijs)
        else begin
          ass.(i).[j] <- '#';
          (false, (i - 1, j) :: (i + 1, j) :: (i, j - 1) :: (i, j + 1) :: ijs)
        end) (true, []) ijs
    with
    | (true, _) -> n
    | (false, ijs) -> bfs (n + 1) ijs in
  Printf.printf "%d\n" @@ bfs 0 @@
  List.concat @@ Array.to_list @@ Array.init h @@ fun i ->
    List.concat @@ Array.to_list @@ Array.init w @@ fun j ->
      if ass.(i).[j] = '.'
      then []
      else [(i - 1, j); (i + 1, j); (i, j - 1); (i, j + 1)]
