#include<bits/stdc++.h>
#define ll long long
#define uint unsigned
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define y1 djsfah
using namespace std;
const int N=205;
char s[N][N];
int dis[N],q[N],n,ans;
void check(int S){
	For(i,1,n) dis[i]=-1;
	int h=0,t=1; q[1]=S; dis[S]=1;
	while (h!=t){
		int x=q[++h];
		For(i,1,n)
			if (s[x][i]=='1'&&dis[i]==-1){
				dis[i]=dis[x]+1;
				q[++t]=i;
			}
	}
	For(i,1,n)
		For(j,i+1,n)
			if (s[i][j]=='1')
				if (abs(dis[i]-dis[j])!=1)
					return;
	ans=max(ans,dis[q[n]]);
}
int main(){
	ans=-1;
	scanf("%d",&n);
	For(i,1,n) scanf("%s",s[i]+1);
	For(i,1,n) check(i);
	printf("%d\n",ans);
}
/*
6
2
1 3
4 5
*/