#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#ifdef _debug
#define dout(i) cout << #i << ' ' << i << ' '
#else 
#define dout(i) //
#endif
using ll = long long;
using ull = unsigned long long;
using ul = unsigned;
using db = double;
int n;
const int maxn = 300;
int con[maxn][maxn];
int res = -1;
int pick(int root){
	set<int> last, now, put, next;
	int res = 0;
	now.insert(root);
	for(;now.size();++res){
		for(auto &i : now){
			for(int j = 1;j <= n;++j){
				if(con[i][j]){
					if(!last.count(j)){
						if(put.count(j))return -1;
						next.insert(j);
					}
				}
			}
		}
		for(auto &i : next)put.insert(i);
		last = now;
		now = next;
		next.clear();
	}
	return res;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for(int i = 1;i <= n;++i)for(int j = 1;j <= n;++j){
		char c;
		cin >> c;
		con[i][j] = c == '1';
	}
	for(int i = 1;i <= n;++i)res = max(res, pick(i));
	cout << res << '\n';


}