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 PHP (7.4.4) (judge verdict: Wrong Answer)

<?php
list($n) = ints();
for ($i = 0; $i < $n; ++$i) {
    list($s) = strs();
    for ($j = 0; $j < $n; ++$j)
        if ($s[$j] === '1') $g[$i][] = $j;
}
if (!isNipartite()) exit('-1');
bfs();
$max = 0;
foreach ($dist as $i => $d) {
    if ($d > $max) {
        $max = $d;
        $maxi = $i;
    }
}
bfs($maxi);
echo max($dist) + 1;
function isNipartite()
{
    global $n, $g, $dist;
    // BFS：二部グラフ判定
    $is_nipartite = true; // 二部グラフかどうか
    $q = new SplQueue(); // キューを用意
    $dist = array_fill(0, $n, -1); // 距離を格納する配列（-1の場合はその頂点が未発見）

    for ($i = 0; $i < $n; $i++) {
        if ($dist[$i] !== -1) continue; // 発見済み
        $dist[$i] = 0; // 頂点$iからの距離格納配列
        $q->enqueue($i); // キューに$iを追加
        while (!$q->isEmpty()) {
            $v = $q->dequeue();

            foreach ($g[$v] as $next_v) {
                if ($dist[$next_v] === -1) {
                    $dist[$next_v] = $dist[$v] + 1;
                    $q->enqueue($next_v);
                } else {
                    if ($dist[$next_v] === $dist[$v]) { // 隣接する頂点の距離が同じ場合二部グラフにならない
                        $is_nipartite = false;
                        break 3;
                    }
                }
            }
        }
    }
    return $is_nipartite;
}
function bfs($s = 0)
{
    global $n, $g, $dist;
    // BFS（幅優先探索）：最短経路アルゴリズムとして活用
    $q = new SplQueue(); // キューを用意
    $dist = array_fill(0, $n, -1); // 距離を格納する配列（-1の場合はその頂点が未発見）

    $dist[$s] = 0; // 始点からの距離格納配列
    $q->enqueue($s); // キューに始点を追加

    while (!$q->isEmpty()) {
        $v = $q->dequeue();

        foreach ($g[$v] as $next_v) {
            if ($dist[$next_v] !== -1) continue; // 発見済み

            $dist[$next_v] = $dist[$v] + 1;
            $q->enqueue($next_v);
        }
    }
}
function strs()
{
    return explode(' ', trim(fgets(STDIN)));
}
function ints()
{
    return array_map('intval', explode(' ', trim(fgets(STDIN))));
}