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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;

public class Program
{
    private int N;
    HashSet<int>[] Edge;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        Edge = new HashSet<int>[N];
        for (int i = 0; i < N; i++)
        {
            Edge[i] = new HashSet<int>();
        }

        for (int i = 0; i < N; i++)
        {
            string s = sc.Next();
            for (int j = 0; j < N; j++)
            {
                if (s[j] == '1')
                {
                    Edge[i].Add(j);
                }
            }
        }

        var uf = new UnionFind(N);
        for (int i = 0; i < N; i++)
        {
            foreach (int i1 in Edge[i])
            {
                foreach (int i2 in Edge[i1])
                {
                    if (Edge[i].Contains(i2))
                    {
                        Console.WriteLine(-1);
                        return;
                    }

                    uf.Union(i, i2);
                }
            }
        }

        var map = new HashMap<int, int>();

        for (int i = 0; i < N; i++)
        {
            map[uf.Find(i)]++;
        }

        int ans = 0;
        foreach (var p in map)
        {
            ans += Math.Min(2, p.Value);
        }
        
        Console.WriteLine(ans);
    }

    public static void Main(string[] args)
    {
        new Program().Solve();
    }
}

class HashMap<K, V> : Dictionary<K, V>
{
    new public V this[K i]
    {
        get
        {
            V v;
            return TryGetValue(i, out v) ? v : base[i] = default(V);
        }
        set { base[i] = value; }
    }
}

class UnionFind
{
    private int[] Par, Rank;

    public UnionFind(int max)
    {
        Par = new int[max];
        Rank = new int[max];
        for (int i = 0; i < max; i++)
        {
            Par[i] = i;
        }
    }

    public int Find(int n)
    {
        if (Par[n] == n)
        {
            return n;
        }
        else
        {
            return Find(Par[n]);
        }
    }

    public bool Same(int a, int b)
    {
        return Find(a) == Find(b);
    }

    public void Union(int a, int b)
    {
        a = Find(a);
        b = Find(b);
        if (a == b) return;
        if (Rank[a] < Rank[b])
        {
            Par[a] = b;
        }
        else
        {
            Par[b] = a;
            if (Rank[a] == Rank[b]) Rank[a]++;
        }
    }
}

class Scanner
{
    public Scanner()
    {
        _pos = 0;
        _line = new string[0];
    }

    const char Separator = ' ';
    private int _pos;
    private string[] _line;

    #region スペース区切りで取得

    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = Console.ReadLine().Split(Separator);
            _pos = 0;
        }

        return _line[_pos++];
    }

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }

    #endregion

    #region 型変換

    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }

    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }

    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }

    #endregion

    #region 配列取得

    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = Console.ReadLine().Split(Separator);

        _pos = _line.Length;
        return _line;
    }

    public int[] IntArray()
    {
        return ToIntArray(Array());
    }

    public long[] LongArray()
    {
        return ToLongArray(Array());
    }

    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }

    #endregion
}