#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N = 210;
int n;
char grid[N][N];

bool done[N];

int group[N];

int ans = -1;

void start(int node){
	for(int i = 0 ;i < n;i++){
		done[i] = false;
	}
	group[node] = 1;
	queue < int > q;
	done[node] = true;
	q.push(node);
	int mx = 0;
	while(!q.empty()){
		node = q.front();
		q.pop();
		mx = max(mx,group[node]);
		for(int i = 0 ;i < n;i++){
			if(grid[node][i] == '0') continue;
			if(!done[i]){
				done[i] = true;
				group[i] = group[node] + 1;
				q.push(i);
				continue;
			}
			if(abs(group[i] - group[node]) != 1)
				return;
		}
	}
	ans = max(ans , mx);
}

int main(){
	scanf("%d",&n);
	for(int i = 0 ;i < n;i++){
		scanf("%s",grid[i]);
		
	}
	for(int i = 0 ;i < n;i++){
		start(i);
	}
	cout << ans << endl;
	return 0;
}