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.15.1) (judge verdict: Runtime Error)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

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

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

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

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

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*);
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};
#[allow(unused_imports)]
use std::io::{stdout, stdin, BufWriter, Write};

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }

    input!{
      n: usize,
      ss: [chars; n],
    }

    let mut grp = vec![0; n];
    let mut cur = 1;
    for i in 0..n {
        if grp[i] == 0 {
            grp[i] = cur;
            cur += 1;
        }

        use std::collections::HashSet;
        let mut adjs = HashSet::new();
        for j in 0..n {
            if ss[i][j] == '1' {
                adjs.insert(j);
            }
        }

        // 同一グループ or 隣接しないので、adjs同士に辺が存在してはいけない
        for &j in adjs.iter() {
            for k in 0..n {
                if ss[j][k] == '1' && adjs.contains(&k) {
                    return puts!("{}\n", -1);
                }
            }
        }

        let head = *adjs.iter().next().unwrap();
        let mut head_set = HashSet::new();
        for k in 0..n {
            if ss[head][k] == '1' {
                head_set.insert(k);
            }
        }
        if grp[head] == 0 {
            grp[head] = cur;
            cur += 1;
        }

        let mut other_grps = vec![];
        for &j in adjs.iter() {
            if head == j {
                continue;
            }

            let mut ok = true;
            for k in 0..n {
                if ss[j][k] == '1' {
                    if grp[k] == grp[i] {
                        continue;
                    }

                    // 何らかの共通項があるとき
                    if head_set.contains(&k) {
                        grp[j] = grp[head];
                        ok = false;
                        break;
                    }
                }
            }

            if ok {
                other_grps.push(j);
            }
        }

        let mut other_grp = 0;
        for &j in other_grps.iter() {
            if grp[j] != 0 {
                other_grp = grp[j];
            }
        }
        if other_grp == 0 {
            other_grp = cur;
            cur += 1;
        }

        for &j in other_grps.iter() {
            if grp[j] != 0 && grp[j] != other_grp {
                // debug!(grp, i, j, other_grps, adjs);
                unreachable!();
            }
            grp[j] = other_grp;
        }
    }

    // debug!(grp);
    grp.sort();
    grp.dedup();
    puts!("{}\n", grp.len());
}