#include<bits/stdc++.h>
using namespace std;
const int N=205;
char s[N];
int a[N][N],flag[N],n;
void dfs(int x){
	for (int i=1;i<=n;i++)
		if (a[x][i]){
			if (!flag[i]){
				flag[i]=3-flag[x];
				dfs(i);
			}
			else if (flag[i]+flag[x]!=3){
				puts("-1");
				exit(0);
			}
		}
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%s",s+1);
		for (int j=1;j<=n;j++)a[i][j]=s[j]-'0';
	}
	flag[1]=1;dfs(1);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			if (i!=j&&!a[i][j])a[i][j]=1e9;	
	for (int k=1;k<=n;k++)
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++)a[i][j]=min(a[i][k]+a[k][j],a[i][j]);
	int ans=0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			ans=max(ans,a[i][j]);
	printf("%d\n",ans+1);				
}