Problem:
Score : 500 points

Problem Statement

Given is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are described by a grid of characters S.
If S_{i,j} is 1, there is an edge connecting Vertex i and j; otherwise, there is no such edge.

Determine whether it is possible to divide the vertices into non-empty sets V_1, \dots, V_k such that the following condition is satisfied. If the answer is yes, find the maximum possible number of sets, k, in such a division.

Every edge connects two vertices belonging to two "adjacent" sets. More formally, for every edge (i,j), there exists 1\leq t\leq k-1 such that i\in V_t,j\in V_{t+1} or i\in V_{t+1},j\in V_t holds.

Constraints

2 \leq N \leq 200

S_{i,j} is 0 or 1.

S_{i,i} is 0.

S_{i,j}=S_{j,i}

The given graph is connected.

N is an integer.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}...S_{1,N}
:
S_{N,1}...S_{N,N}

Output

If it is impossible to divide the vertices into sets so that the condition is satisfied, print -1.
Otherwise, print the maximum possible number of sets, k, in a division that satisfies the condition.

Sample Input 1

2
01
10

Sample Output 1

2

We can put Vertex 1 in V_1 and Vertex 2 in V_2.

Sample Input 2

3
011
101
110

Sample Output 2

-1

Sample Input 3

6
010110
101001
010100
101000
100000
010000

Sample Output 3

4

Solution in Rust (1.42.0) (judge verdict: Accepted)

use std::cmp::max;
use std::cmp::min;

fn main() {
    input! {
        n: usize,
        s: [chars; n]
    }
    let mut edges = vec![vec![]; n];
    for i in 0..n {
        for j in 0..n {
            if s[i][j] == '1' {
                edges[i].push(j);
            }
        }
    }
    // 二部グラフかどうか調べる
    let mut reached = vec![false; n];
    let mut colors = vec![-1; n];
    let mut stack = vec![0];
    reached[0] = true;
    colors[0] = 0;
    while let Some(c) = stack.pop() {
        for edge in &edges[c] {
            let edge_color = if colors[c] == 0 { 1 } else { 0 };
            if colors[*edge] >= 0 && colors[*edge] != edge_color {
                println!("-1");
                return;
            }
            colors[*edge] = edge_color;
            if reached[*edge] {
                continue;
            }
            reached[*edge] = true;
            stack.push(*edge);
        }
    }
    // 直径を調べる
    let mut dist_mat = vec![vec![205; n]; n];
    for i in 0..n {
        dist_mat[i][i] = 0;
    }
    for (i, edge) in edges.iter().enumerate() {
        for e in edge {
            dist_mat[i][*e] = 1
        }
    }
    for k in 0..n {
        for i in 0..n {
            for j in 0..n {
                if dist_mat[i][j] > dist_mat[i][k] + dist_mat[k][j] {
                    dist_mat[i][j] = dist_mat[i][k] + dist_mat[k][j];
                    dist_mat[j][i] = dist_mat[i][k] + dist_mat[k][j];
                }
            }
        }
    }

    let ans = dist_mat
        .iter()
        .map(|a| a.iter().filter(|b| **b != 205).fold(0, |b, c| max(b, *c)))
        .fold(0, |b, c| max(b, c));

    println!("{}", ans + 1);
}

#[macro_export(local_inner_maccros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export(lcal_inner_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export(lcal_inner_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}