#include<bits/stdc++.h>
        #define ll  long long
        #define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        #define rep(i,a,b)  for(ll i=a;i<b;i++)
        #define pb push_back
        #define Max(x,y,z) max(x,max(y,z))
        #define f first
        #define s second
        #define sz(x)       (ll)x.size()
        #define pi 3.1415926536
        #define mod 1000000007
        #define MOD 998244353
        #define maxi 100000000000000000
        using namespace std;
       const int N=1e5+5;
       ll n;
       vector<ll> adj[205];
       ll col[205];
       bool vis[205];
       bool flag=1;
      /* bool dfs(ll x)
       {   vis[x]=1;
           for(auto y:adj[x])
           {
               if(!vis[y])
               {
                   if(col[y]==-1)
                   {
                       col[y]=1-col[x];
                        return flag=(flag && dfs(y));
                   }
               }
               else if(col[y]==col[x])
                    return 0;

           }
           return flag;
       }*/
       /*ll dp[N][25],dp2[N][25];
       ll logcal[N];*/
/*       void update(ll x,ll value,ll pos)
{
    while(pos<=n)
    {
        bit[x][pos]+=value;
        pos+=(pos)&(-pos);
    }
}
ll query(ll x,ll pos)
{  ll ans=0;
    while(pos>0)
    {
        ans+=bit[x][pos];
        pos-=(pos)&(-pos);
    }
    return ans;
}*/
    /*
    bool vis[N];
    ll cnt=0;
    void dfs(ll x)
    {
        vis[x]=1;
        cnt++;
        for(auto y:adj[x])
            if(!vis[y]) dfs(y);
    }*/
      /* ll dfs(ll x,bool vis[],bool rec[])
    {   //cout<<x<<" ";
        vis[x]=1;
        rec[x]=1;
        for(auto y:adj[x])
            if(!vis[y] && dfs(y,vis,rec)) return true;
            else if(rec[y]) return true;
        rec[x]=false;
        return false;
    }*/
        /*bool isprime(ll x)
        {
            for(ll i=2;(i*i)<=x;i++)
            {
                if(x%i==0) return 0;
            }
            return 1;
        }*/
        /*vector<ll> _lps(string &t)
        {   ll n=t.size();
            vector<ll> lps(n+1,0);
            ll i=1,len=0;
            while(i<n)
            {
                if(t[i]==t[len])
                {
                    len++;
                    lps[i]=len;
                    i++;
                }
                else
                {
                    if(len!=0)
                        len=lps[len-1];
                    else
                        lps[i++]=0;
                }
            }
            return lps;
        }
        ll KMP(string &s,string &t)
        {
            ll n=s.size();
            ll m=t.size();
            vector<ll> lps=_lps(t);
            ll i=max(0LL,n-m),j=0;
            while(i<n)
            {
                if(s[i]==t[j])
                {
                    i++;
                    j++;
                }
                else
                    if(j!=0) j=lps[j-1];
                    else i++;
            }
            return j;

        }*/
        void solve()
        {
                    ll m,y,z,h,b,c,d,e,k1,k2,p,x,k;
                    string s;
                    cin>>n;
                    ll dp[n][n],g[n][n];
                    rep(i,0,n)
                        rep(j,0,n) dp[i][j]=maxi;
                    rep(i,0,n) { cin>>s;
                        rep(j,0,n)
                            {g[i][j]=s[j]-'0';if(g[i][j]==1) dp[i][j]=1;}}
                    memset(col,-1,sizeof(col));
                    col[0]=1;
                    queue<ll> q;
                    q.push(0);
                    while(!q.empty())
                    {
                        ll u=q.front();q.pop();
                        if(g[u][u]==1){flag=0;}
                        rep(i,0,n)
                        {
                            if(g[u][i] && col[i]==-1)
                            {
                                col[i]=1-col[u];
                                q.push(i);
                            }
                            else if(g[u][i] && col[i]==col[u]) flag=0;
                        }
                    }
                    if(flag==0) {cout<<"-1";return;}

                    for(ll i=0;i<n;i++) dp[i][i]=0;
                    for(ll k=0;k<n;k++)
                    {
                        for(ll i=0;i<n;i++)
                        {
                            for(ll j=0;j<n;j++)
                                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
                        }
                    }
                    ll ans=0;
                    rep(i,0,n)
                    {
                        rep(j,0,n)
                        if(dp[i][j]!=maxi) ans=max(ans,dp[i][j]);
                        //cout<<dp[i][j]<<" ";
                    }
                    cout<<ans+1;




}

                int main()
                {
                    speed_up
                    ll t=1;
                    //call();
                    //cin>>t;
                    while(t--)
                        solve();
                }


Measured memory_bytes: 896