#include <bits/stdc++.h>  // ver2.1.9
#define int long long
#define endl "\n"
#define ALL(v) (v).begin(),(v).end()
#define Vi vector<int>
#define Pii pair<int,int>
#define Tiii tuple<int,int,int>
#define PQi priority_queue<int>
#define PQir priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define cYES cout<<"YES"<<endl
#define cNO cout<<"NO"<<endl
#define cYes cout<<"Yes"<<endl
#define cNo cout<<"No"<<endl
#define sortr(v) sort(v,greater<>())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repeq(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>b;i--)
#define repreq(i,a,b) for(int i=a;i>=b;i--)
#define leng(n) (int)(log10(n)+1)
#define dem(a,b) ((a+b-1)/(b))
#define Vout(a) rep(lZ,0,a.size()-1)cout<<a[lZ]<<' ';cout<<a.back()<<endl
#define VVout(a) rep(lW,0,a.size()){Vout(a[lW]);}
#define INF 9000000000000000000  //  9.0 * 10^18
#define MOD 1000000007  //  10^9 + 7
using namespace std;
 
int gcd(int a,int b){return b?gcd(b,a%b):a;}  //  最大公約数gcd
int lcm(int a,int b){return a/gcd(a,b)*b;}  //  最小公倍数lcm
void uniq(Vi &v){sort(ALL(v)); v.erase(unique(ALL(v)),v.end());}  //  uniqのやつ
 
int mypow(int x, int n, int m){  //  累乗x^n(mod m)  O(log(n))
if(n==0)return 1;if(n%2==0)return mypow(x*x%m,n/2,m);else return x*mypow(x,n-1,m)%m;}
 
signed main() {
  cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(12);
  int n;
  cin >> n;
  vector<Pii> gr(0);
  vector<string> s(n);
  
  rep(i,0,n){
    cin >> s[i];
    rep(j,i,n){
      if(s[i][j]=='1') gr.pb(mp(i,j));
    }
  }
  //cout << gr.size();
  int qw,now=0,last=0;
  queue<int> next;
  set<int> pas;
  
  next.push(0);
  pas.insert(0);
  
  while(next.size()!=0){
    
    qw = next.size();
    rep(i,0,qw){
      now = next.front();
      next.pop();
      rep(j,0,n){
        if(s[now][j]=='1' && pas.count(j)==0){
          pas.insert(j);
          next.push(j);
        }
      }
    }
    
  }
// 			 cout<<'#' << now << endl;
  last = now;
  next.push(last);
  pas = {last};
  
  while(next.size()!=0){
    
    qw = next.size();
    rep(i,0,qw){
      now = next.front();
      next.pop();
      rep(j,0,n){
        if(s[now][j]=='1' && pas.count(j)==0){
          pas.insert(j);
          next.push(j);
        }
      }
    }
    
  }
//       cout<<"##" << now << endl;
  //&&&&&&&&&		1回目
  
  last = now;
  next.push(last);
  pas = {last};
  Vi memo(n,-5);
  int ct = 1;
  memo[last] = 1;
//  cout << last <<endl;
  while(next.size()!=0){
    ct++;
    qw = next.size();
    rep(i,0,qw){
      now = next.front();
      next.pop();
      rep(j,0,n){
        if(s[now][j]=='1' && pas.count(j)==0){
          pas.insert(j);
          memo[j] = ct;
          next.push(j);
        }
        if(s[now][j]=='1' && pas.count(j)==1){
          if(memo[j]==ct || memo[j]==ct-2) continue;
          else {cout << -1 << endl;return 0;}
        }
      }
    }
    
  }
  //Vout(memo);
  int ans = *max_element(ALL(memo));
  
  //cout << ans << endl;
  
  		//	&&&&&&&&&&&	2回目
 rep(lQW,0,n) {
   last = lQW;
  next.push(last);
  pas = {last};
   
  rep(asd,0,memo.size()){
    memo[asd] = -5;
  }
    
    
  ct = 1;
  memo[last] = 1;
//		  cout << last <<endl;
  while(next.size()!=0){
    ct++;
    qw = next.size();
    rep(i,0,qw){
      now = next.front();
      next.pop();
      rep(j,0,n){
        if(s[now][j]=='1' && pas.count(j)==0){
          pas.insert(j);
          memo[j] = ct;
          next.push(j);
        }
        if(s[now][j]=='1' && pas.count(j)==1){
          if(memo[j]==ct || memo[j]==ct-2) continue;
          else {cout << -1 << endl;return 0;}
        }
      }
    }
    
  }
  
  
  //Vout(memo2);
  
  ans = max(ans,*max_element(ALL(memo)));
 }
    cout << ans << endl;
  return 0;
}

Measured memory_bytes: 892