#include <bits/stdc++.h>
#define debug(x) cerr<<#x<<" = "<<x<<'\n'

using namespace std;

int n,vis[400],tot,dis[400],ans;
vector<int> g[400];
void bfs(int i) {
    queue<int> q;
    q.push(i);
    vis[i] = ++tot, dis[i] = 0;
    while(!q.empty()) {
        i = q.front(), q.pop();
        ans = max(ans,dis[i]);
        for(int j:g[i]) {
            if(vis[j] < tot) vis[j]=tot, dis[j]=dis[i]+1, q.push(j);
            else if((dis[i]&1) == (dis[j]&1)) cout << -1 << '\n', exit(0);
        }
    }
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++) {
        string s;
        cin >> s;
        for(int j = 0; j < n; j++) if(s[j]=='1') g[i].push_back(j);
    }
    for(int i = 0; i < n; i++) bfs(i);
    cout << ans+1 << '\n';
}
