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

const int INF = 1e8;

const int MAXN = 210;
int N;
char adj[MAXN][MAXN];

int dist[MAXN][MAXN];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> adj[i];
	}

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (i == j) dist[i][j] = 0;
			else if (adj[i][j] == '1') dist[i][j] = 1;
			else dist[i][j] = INF;
		}
	}
	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 st = 0; st < N; st++) {
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (adj[i][j] == '1' && dist[st][i] == dist[st][j]) {
					cout << -1 << '\n';
					exit(0);
				}
			}
		}
	}

	int diam = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			diam = max(diam, dist[i][j]);
		}
	}
	cout << diam + 1 << '\n';

	return 0;
}
