#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int N=205;

int n;
int dep[N];
char s[N][N];
queue<int>Q;

int Bfs(int st) {
	int ret=1;
	memset(dep,0,sizeof(dep));
	dep[st]=1;Q.push(st);
	while(!Q.empty()) {
		int u=Q.front(); Q.pop();
		ret=max(ret,dep[u]);
		for(int v=1;v<=n;v++)
			if(s[u][v]=='1') {
				if(!dep[v]) {
					dep[v]=dep[u]+1;
					Q.push(v);
				}
				else if(dep[v]!=dep[u]+1&&dep[v]!=dep[u]-1)
					return -1;
			}
	}
	return ret;
}
int main() {
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%s",s[i]+1);
	int ans=-1;
	for(int i=1;i<=n;i++)
		ans=max(ans,Bfs(i));
	printf("%d\n",ans);
	return 0;
}