#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cout << #x << " = " << (x) << endl;
using Graph = vector<vector<int> >;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//#define int long long

// 二部グラフ判定
vector<int> color;
bool dfs(const Graph &G, int v, int cur = 0) {
    color[v] = cur;
    for (auto next_v : G[v]) {
        // 隣接頂点がすでに色確定していた場合
        if (color[next_v] != -1) {
            if (color[next_v] == cur) return false; // 同じ色が隣接したらダメ
            continue;
        }

        // 隣接頂点の色を変えて、再帰的に探索 (一回でも false が返ってきたら false)
        if (!dfs(G, next_v , 1 - cur)) return false;
    }
    return true;
}

void solve() {
	int n; cin >> n;
    char s[220][220];
    Graph G(n);
    rep(i, n) {
        rep(j, n) {
            cin >> s[i][j];
            if(s[i][j] == '1') G[i].pb(j);
        }
    }

    color.assign(n, -1);
    bool is_bipartite = true;
    for (int v = 0; v < n; ++v) {
        if (color[v] != -1) continue; // v が探索済みだったらスルー
        if (!dfs(G, v)) is_bipartite = false;
    }

    if(!is_bipartite) {
        cout << -1 << endl;
    } else {
        vector<bool> in_loop(n, false);
        rep(s, n) {
            vector<int> dist(n, -1);
            queue<P> que;
            dist[s] = 0; que.push(P(s, -1));
            while(que.size()) {
                P p = que.front(); int v = p.fr; int par = p.sc;
                que.pop();
                for(auto nv : G[v]) {
                    if(nv == par) continue;
                    if(dist[nv] == 0) {
                        in_loop[s] = true;
                    }
                    if (dist[nv] == -1) {
                        dist[nv] = dist[v] + 1;
                        que.push(P(nv, v));
                    }
                }
            }

        }

        int ans = 1;
        rep(s, n) {
            vector<int> dist(n, -1);
            queue<pair<int,pair<int, int>>> que;
            dist[s] = 0; que.push(make_pair(s,make_pair(-1, 0)));
            while(que.size()) {
                pair<int, pair<int, int>> p = que.front(); int v = p.fr; int par = p.sc.fr; int loop_cnt = p.sc.sc;
                que.pop();
                for(auto nv : G[v]) {
                    if(nv == par) continue;
                    if (dist[nv] == -1) {
                        int lcnt;
                        if(in_loop[nv]) {
                            lcnt = loop_cnt + 1;
                            if(lcnt == 1) {
                                dist[nv] = dist[v] + 1;
                            }
                            else if(lcnt % 2 == 0) {
                                dist[nv] = dist[v] + 1;
                            }
                            else if(lcnt % 2 == 1) {
                                 dist[nv] = dist[v] - 1;
                            }
                        } else {
                            lcnt = 0;
                            dist[nv] = dist[v] + 1;
                        }
                        que.push(make_pair(nv, make_pair(v, lcnt)));
                    }
                }
            }

            int res = 0;
            rep(i, n) {
                res = max(res, dist[i]);
            }
            ans = max(ans, res + 1);
        }
        cout << ans << endl;
    }

}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init();
	solve();
	//cout << "finish" << endl;
    return 0;
}

Measured memory_bytes: 512