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

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	Int n; cin >> n;
	vector<string> s(n);
	for(auto &i:s) cin >> i;
	bool bpg = true;
	vector<Int> c(n, -1);
	queue<Int> q;
	c[0] = 0;
	q.push(0);
	while(!q.empty()){
		Int p = q.front(); q.pop();
		for(Int i=0; i<n; ++i)
			if(s[p][i] == '1'){
				if(c[i] != -1 && c[i] != 1-c[p]) bpg = false;
				if(c[i] == -1) q.push(i);
				c[i] = 1-c[p];
			}
	}
	if(!bpg){
		cout << -1 << "\n";
		return 0;
	}
	vector<vector<Int>> d(n, vector<Int>(n, 201));
	for(Int i=0; i<n; ++i)
		for(Int j=0; j<n; ++j){
			if(i == j) d[i][j] = 0;
			else if(s[i][j] == '1') d[i][j] = 1;
		}
	for(Int k=0; k<n; ++k)
		for(Int i=0; i<n; ++i)
			for(Int j=0; j<n; ++j)
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	Int ans = 2;
	for(Int i=0; i<n; ++i){
		Int tmp = 0;
		for(Int j=0; j<n; ++j) tmp = max(tmp, d[i][j]);
		ans = max(ans, tmp+1);
	}
	cout << ans << "\n";
}