using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using static System.Math;
using static System.Array;
using static AtCoder.Tool;

namespace AtCoder
{
    class AC
    {
        const int mod = 1000000007;
        const int INF = int.MaxValue / 2;
        const long SINF = long.MaxValue / 2;
        static int[] dx = { 0, 1, 0, -1 };
        static int[] dy = { 1, 0, -1, 0 };

        static int N;
        static Queue<Edge> Q = new Queue<Edge>();
        static List<List<int>> G = new List<List<int>>();
        static bool ok = true;


        static void Main(string[] args)
        {
            //var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            //Console.SetOut(sw);
            var cin = new Scanner();

            N = int.Parse(Console.ReadLine());
            long ans = -1;
            
            for (var i = 0; i < N; i++)
            {
                G.Add(new List<int>());
            }
           

            for (var i = 0; i < N; i++)
            {
                var s = Console.ReadLine();
                for (var j = 0; j < N; j++)
                {
                    if (s[j] == '1')
                    {
                        G[i].Add(j);
                    }
                }
            }



            for (var i = 0; i < N; i++)
            {
                int[] num = new int[N];
                Initialize(ref num, -1);
                num[i] = 1;
                foreach (var x in G[i])
                {
                    Q.Enqueue(new Edge(i, x));
                }

                while (Q.Count() > 0)
                {
                    int f = Q.Peek().from;
                    int t = Q.Dequeue().to;
                    //Console.WriteLine($"{f} {t}");
                    if (num[t] != -1)
                    {
                        if (Abs(num[f] - num[t]) != 1)
                        {
                            ok = false;
                            break;
                        }
                    }
                    else
                    {
                        num[t] = num[f] + 1;
                        foreach (var x in G[t])
                        {
                            Q.Enqueue(new Edge(t, x));
                        }
                    }

                }
                if (!ok)
                {
                    Console.WriteLine(-1);
                    return;
                }
                ans = Max(ans, num.Max());

            }

            Console.WriteLine(ans);

            //Console.Out.Flush();
        }


        

        struct Edge
        {
            public int from;
            public int to;
            public Edge(int f,int t)
            {
                from = f;
                to = t;
            }
        }

        
    }
    public class Scanner
    {
        public int[] SplitRead()
        {
            int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return array;
        }
        public long[] SplitReadL()
        {
            long[] array = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return array;
        }
    }

    public static class Tool
    {
        static public void Initialize<T>(ref T[] array, T initialvalue)
        {
            for (var i = 0; i < array.Length; i++)
            {
                array[i] = initialvalue;
            }
        }
        static public long Modulo(long value, long mod)
        {
            long r = value % mod;

            if (r * mod < 0)
            {
                r += mod;
            }
            return r;
        }
    }

    

}






Measured memory_bytes: 13524