#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9+7)
#define inf (int)(3e18)
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define P std::pair<int,int>
#define PiP std::pair<int,std::pair<int,int>>
#define all(v) v.begin(),v.end()
#define mkp std::make_pair
#define prique(T) std::priority_queue<T,vector<T>,greater<T>>
using namespace std;
template<class T> inline void chmax(T& a, T b) { a = std::max(a, b); }
template<class T> inline void chmin(T& a, T b) { a = std::min(a, b); }

bool prime(int x) {
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0)return false;
	}
	return x != 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= mod;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int get_rand(int MIN, int MAX) {
	std::random_device rnd;
	std::mt19937_64 mt64(rnd());
	std::uniform_int_distribution<int>engine(MIN, MAX);
	return engine(mt64);
}
/*--------Library Zone!--------*/

int n;
bool con[205][205];
signed main() {
	cin >> n;
	rep(i, n)rep(j, n) {
		char c; cin >> c; if (c == '1')con[i][j] = true;
	}
	int ans = -1;
	rep(i, n) {
		int far[205];
		rep(j, n)far[j] = inf;
		far[i] = 1;
		queue<int>que; que.push(i);
		while (!que.empty()) {
			int k = que.front(); que.pop();
			rep(j, n) {
				if (!con[j][k])continue;
				if (far[j] == inf) {
					que.push(j); far[j] = far[k] + 1;
				}
				else {
					if (abs(far[k] - far[j]) != 1) {
						goto ng;
					}
				}
			}
		}
		rep(j, n)chmax(ans, far[j]);
	ng:;
	}
	cout << ans << endl;
}