#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e6+10;

vector<int> G[201];
int pos[201], n;

int bfs(int v){
	queue<int> q;
	q.push(v);
	pos[v] = 1;
	int d = 0;
	while(!q.empty()){
		++d;
		int k = q.size();
		while(k--){
			int t = q.front(); q.pop();
			for(int i:G[t]){
				if(pos[i]){
					if(pos[i] != d+1 && pos[i] != d-1) return -1;
				}
				else{
					pos[i] = d+1;
					q.push(i);
				}
			}			
		}
	}
	return d;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> n;
	char buf[201];
	for(int i=0;i<n;++i){
		cin >> buf;
		for(int j=0;j<n;++j){
			if(buf[j] == '1'){
				G[i].push_back(j);
			}
		}
	}
	int ans = -1;
	for(int i=0;i<n;++i){
		memset(pos, 0, sizeof(pos));
		ans = max(ans, bfs(i));
	}
	cout << ans << endl;
	return 0;
}
