//Created Time:2019年10月05日 星期六 20时41分32秒
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define N 202

using namespace std;

int n, ans;
int a[N];
char mp[N][N];

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) scanf("%s", mp[i] + 1);
	for(int i = 1; i <= n; ++i){
		memset(a, 0, sizeof a);
		a[i] = 1; queue < int > Q; Q.push(i);
		while(!Q.empty()){
			int u = Q.front(); Q.pop();
			for(int v = 1; v <= n; ++v){
				if(mp[u][v] == '1'){
					if(a[v] && abs(a[v] - a[u]) != 1){
						puts("-1"); return 0;
					}
					if(!a[v]) a[v] = a[u] + 1, Q.push(v);
				}
			}
		}
		for(int j = 1; j <= n; ++j) ans = max(ans, a[j]);
	}
	cout << ans << endl;
	return 0;
}
