#include "bits/stdc++.h"
using namespace std;

int main() {
	long long N, INF = 1000000000;
	vector<string> S;
	vector<vector<long long> > Dist;
	cin >> N;
	S.resize(N);
	Dist.resize(N);
	for (int i = 0; i < N; i++) {
		cin >> S[i];
		Dist[i].assign(N, INF);
		for (int j = 0; j < N; j++) {
			if (S[i][j] == '1') Dist[i][j] = 1;
		}
		Dist[i][i] = 0;
	}
	for (int k = 0; k < N; k++) {
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
 				Dist[i][j] = min(Dist[i][j], Dist[i][k] + Dist[k][j]);
			}
		}
	}
	for (int k = 0; k < N; k++) {
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (Dist[i][j] % 2 != (Dist[i][k] + Dist[k][j]) % 2) {
					cout << -1 << endl;
					return 0;
				}
			}
		}
	}
	long long ANS = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			ANS = max(ANS, Dist[i][j]);
		}
	}
	cout << ANS + 1 << endl;
}