#include<bits/stdc++.h>
using namespace std;

#define MAXN 205
int n,m;
bool a[MAXN][MAXN];
char s[MAXN][MAXN];

void rd()
{
	scanf("%d",&n);
	for(int i = 1; i <= n; i ++) {
		scanf("%s",s[i]+1);
		for(int j = 1; j <= n; j ++)
			a[i][j] = s[i][j]-'0';
	}
}

queue<int>q;
int dis[MAXN];
int ans = -1;

bool bfs(int s)
{
	while(!q.empty()) q.pop();
	memset(dis,-1,sizeof(dis));
	q.push(s);
	dis[s] = 0;
	while(!q.empty())
	{
		int x = q.front();
		q.pop();
		for(int i = 1; i <= n; i ++)
		if(a[x][i]) {
			if(dis[i] == -1) {
				dis[i] = dis[x]+1;
				q.push(i);
			} else {
				if(dis[x] == dis[i]+1 || dis[i] == dis[x]+1) ;
				else return 0;
			}
		}
	}
//	for(int i = 1; i <= n; i ++)
//		cout<<dis[i]<<" ";
//	cout<<"\n";
	for(int i = 1; i <= n; i ++)
		ans = max(ans,dis[i]+1);
	return 1;
}

signed main()
{
	rd();
	for(int i = 1; i <= n; i ++)
		bfs(i);
	cout<<ans;
	return 0;
}