#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
int n;
vector<int>v[210];
char c[210][210];
int color[210];
bool dfs(int x,int c){
	color[x]=c;
	for(int g:v[x]){
		if(color[g]==c)return false;
		if(color[g]==0&&!dfs(g,-c))return false;
	}
	return true;
}
bool solve(){
	rep(i,n){
		if(color[i]==0){
			if(dfs(i,1)==false)return false;
		}
	}
	return true;
}
int d[210][210];	
int main(){
	cin>>n;
	rep(i,n){
		rep(j,n){
			cin>>c[i][j];
			if(i!=j)d[i][j]=1000000000;
			if(c[i][j]=='1'){
				v[i].pb(j);
				d[i][j]=1;
			}
		}
	}
	if(!solve()){
		puts("-1");
		return 0;
	}
	rep(k,n){
		rep(i,n){
			rep(j,n){
				d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
			}
		}
	}
	int ma=0;
	rep(i,n){
		rep(j,n){
			ma=max(ma,d[i][j]);
		}
	}
	cout<<ma+1<<endl;
}
