Scanf.scanf "%d" (fun n ->
    let isBiparateGraph g =
        let col = Array.make (Array.length g) (-1) in
        let rec dfs cur c =
            let rec loop = function
                | [] -> true
                | hd :: tl ->
                        if col.(hd) = -1 then (
                            col.(hd) <- c;
                            if dfs hd (1 - c) then loop tl else false
                        ) else if col.(hd) = c then loop tl
                          else false
            in
            loop g.(cur)
        in
        col.(0) <- 0;
        dfs 0 1
    in

    let warshallFloyd g =
        let n = Array.length g in
        let d = Array.make_matrix n n (n + 1) in
        Array.iteri (fun i l -> List.iter (fun j -> d.(i).(j) <- 1) l) g;
        for k = 0 to n - 1 do
            for i = 0 to n - 1 do
                for j = 0 to n - 1 do
                    d.(i).(j) <- min d.(i).(j) (d.(i).(k) + d.(k).(j))
                done
            done
        done;
        d
    in

    let a = Array.make n [] in
    for i = 0 to n - 1 do
        Scanf.scanf " %s" (fun s -> 
            for j = 0 to n - 1 do
                if s.[j] = '1' then a.(j) <- i :: a.(j)
            done
        )
    done;
    Printf.printf "%d\n" @@
        if isBiparateGraph a then (
            let d = warshallFloyd a in
            1 + Array.fold_left (fun acc arr -> Array.fold_left (fun a b -> if b < n then max a b else a) acc arr) 0 d
        ) else (-1)
)