#include<bits/stdc++.h>
using namespace std;
int n, ans, deep[220], uu;
char g[210][210];
int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%s", g[i] + 1);
	}
	for(int u = 1; u <= n; u++){
		memset(deep, -1, sizeof(deep));
		deep[u] = 1;
		bool flag = 0;
		int res = 0;
		queue<int> q;
		q.push(u);
		while(!q.empty()){
			int x = q.front();
			q.pop();
			for(int j = 1; j <= n; j++){
				if(g[x][j] == '1'){
					if(deep[j] == deep[x] - 1 || deep[j] == deep[x] + 1) continue;
					if(deep[j] == -1){
						deep[j] = deep[x] + 1;
						res = max(res, deep[j]);
						q.push(j);
					}
					else{
						flag = 1;
						break;
					}
				}	
			}
		}
		if(flag == 0){
			if(ans < res) {
				ans = res, uu = u;
			}
		}
	}
	if(ans){
		cout<<ans;
	}
	else printf("-1");
}