#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF=1e9,MOD=1e9+7,ohara=1e6+10;
const ll LINF=1e18;
using namespace std;
    
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define rrep(i,a,b) for(int i=(a);i<(b);i++)
#define rrrep(i,a,b) for(int i=(a);i>=(b);i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout<<(x)<<endl
#define doublecout(a) cout<<fixed<<setprecision(15)<<a<<endl;
#define Cerr(x) cerr<<(x)<<endl
#define fi first
#define se second
#define P pair<ll,ll> 
#define m_p make_pair
#define V vector<ll> 
#define U_MAP unordered_map<ll,ll>
#define pq priority_queue<ll>
#define rpq priority_queue<ll,vector<ll>,greater<ll>>
#define p_b push_back
    
ll n,a,b,c,d,tmp,tmpp,m,h,w,x,y,sum,pos,k,q;
ld doua;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
//int dy[]={-1,0,1,-1,1,-1,0,1};
//int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz");
char s;
bool fl;
struct edge{int to,cost;};
V g[ohara];
    
//------ Believe yourself as a genius!!!!!! ------
    
int main(void){
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>n;
    rep(i,n){
        rep(j,n){
            cin>>s;
            if(s=='0')continue;
            g[i].p_b(j);
        }
    }
    int ans=0;
    rep(i,n){
        //開始地点固定
        int dat[n+10]={};
        queue<int> que;
        que.push(i);
        dat[i]=1;
        int ok=1;
        while(1){
            if(que.empty())break;
            int moto=que.front();
            que.pop();
            if(moto!=i&&dat[moto]!=0)continue;
            ll mi=LINF,ma=-LINF;
            int vi[300]={};
            rep(j,Size(g[moto])){
                ll to=g[moto][j];
                if(dat[to]==0){
                    que.push(to);
                    continue;
                }
                mi=min(mi,(ll)dat[to]);
                ma=max(ma,(ll)dat[to]);
                vi[dat[to]]=1;
            }
            if(ma-mi>=3||ma-mi==1){
                ok=0;
                break;
            }
            if(ma-mi==0){
                dat[moto]=ma+1;
            }
            else if(ma-mi==2){
                if(vi[(ma+mi)/2]==1){
                    ok=0;
                    break;
                }
                dat[moto]=(ma+mi)/2;
            }
        }
        int cnt=0;
        rep(j,n){
            cnt=max(cnt,dat[j]);
            if(dat[j]==0){ok=0;break;}
        }
        if(!ok)continue;
        ans=max(ans,cnt);
    }
    if(ans==0)Cout(-1);
    else Cout(ans);
    return 0;
}
