let () = Scanf.scanf "%d\n" @@ fun n ->
  let ss = Array.init n @@ fun _ ->
    Scanf.scanf "%s\n" @@ fun s -> s in
  let dp = Array.make_matrix n n (-1) in
  for i = 0 to n - 1 do
    for j = 0 to n - 1 do
      if ss.(i).[j] = '1' then dp.(i).(j) <- 1
    done
  done;
  for i = 0 to n - 1 do
    dp.(i).(i) <- 0
  done;
  try
    for k = 0 to n - 1 do
      for i = 0 to n - 1 do
        for j = 0 to n - 1 do
          if 0 <= dp.(i).(k) && 0 <= dp.(k).(j)
          then begin
            if dp.(i).(j) < 0
            then dp.(i).(j) <- dp.(i).(k) + dp.(k).(j)
            else if dp.(i).(j) mod 2 <> (dp.(i).(k) + dp.(k).(j)) mod 2
            then raise Not_found
            else dp.(i).(j) <- min dp.(i).(j) (dp.(i).(k) + dp.(k).(j))
          end
        done
      done
    done;
    Printf.printf "%d\n" @@ succ @@
    Array.fold_left (Array.fold_left max) min_int dp
  with Not_found -> print_endline "-1"

