/*
Author：Owen_Q
*/
 
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
const int maxn = 205;
 
bool s[maxn][maxn];
int in[maxn];
queue<int> q;
 
int n;
/*
int dfs(int pre,int now,int num)
{
    in[now] = num;
    int re = 0;
    for(int i=1;i<=n;i++)
    {
        if(i==pre)
            continue;
        if(s[now][i])
        {
            if(in[i]>=0)
            {
                if(num-in[i]!=3)
                    continue;
                //cout << in[i] << "*" << num <<"*"<<i<<"*"<<now << "*" << s[now][i]<< endl;
                else
                    return -1;
            }
            else
            {
                int temp = dfs(now,i,num+1);
                if(temp>re)
                    re = temp;
            }
        }
    }
    in[now] = -1;
    return max(re,num);
}
int main()
{
    //freopen(".txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        char temp;
        scanf("%c",&temp);
        for(int j=1;j<=n;j++)
        {
            scanf("%c",&temp);
            //cout << i << "&" << "&"<<temp;
            //cout << temp;
            s[i][j] = (temp=='1' ? true : false);
        }
        //cout << "\n";
    }
    //cout << s[3][3] << "%" << endl;
    bool ok = true;
    int re = 0;
    for(int i=1;ok&&i<=n;i++)
    {
        memset(in,-1,sizeof in);
        int l1=0,l2=0;
        for(int j=1;ok&&j<=n;j++)
        {
            if(s[i][j])
            {
                in[i] = 0;
                int now = dfs(i,j,1);
                if(now == -1)
                    ok = false;
                else
                {
                    if(now>l1)
                    {
                        l2=l1;
                        l1=now;
                    }
                    else if(now>l2)
                        l2=now;
                }
            }
        }
        if(l1+l2+1>re)
        {
            re = l1 + l2 + 1;
            cout << i << "^" << l1 << "^" << l2 <<endl;
        }
    }
    if(ok)
        printf("%d\n",re);
    else
        printf("%d\n",-1);
    return 0;
}
*/
/*
dfs行不通，考虑bfs
*/
 
int myabs(int k)
{
    if(k<0)
        k = k * (-1);
    return k;
}
 
int main()
{
    //freopen(".txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        char temp;
        scanf("%c",&temp);
        for(int j=1;j<=n;j++)
        {
            scanf("%c",&temp);
            //cout << i << "&" << "&"<<temp;
            //cout << temp;
            s[i][j] = (temp=='1' ? true : false);
        }
        //cout << "\n";
    }
    //cout << s[3][3] << "%" << endl;
    int re = -1;
    for(int i=1;i<=n;i++)
    {
        memset(in,-1,sizeof in);
        q.push(i);
        in[i]=1;
        int other = n;
        int now;
        while(other)
        {
            now = q.front();
            //cout << now << endl;
            //exit(-1);
            q.pop();
            other--;
            for(int j=1;j<=n;j++)
            {
                if(s[now][j]&&in[j]==-1)
                {
                    //cout << j << "*" << endl;
                    q.push(j);
                    in[j] = in[now]+1;
                }
            }
            //exit(-1);
        }
        //cout << i << "*"<<in[now] << endl;
        bool ok = true;
        for(int j=1;ok&&j<n;j++)
            for(int k=j+1;ok&&k<=n;k++)
                if(s[j][k]&&(myabs(in[j]-in[k])!=1))
                    ok = false;
        if(ok&&in[now]>re)
            re = in[now];
    }
    printf("%d\n",re);
    return 0;
}

Measured memory_bytes: 3828