#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>

using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
 
template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

int N;

void dfs(int from, vector<vector<int>>& edges, vector<int>& connected_group, int group_idx) {
    connected_group[from] = group_idx;
    for(int to : edges[from]) {
        if(connected_group[to] == 0)
            dfs(to, edges, connected_group, group_idx);
    }
}

int main() {
    cin >> N;
    vector< vector<int> > edges(N, vector<int>(0));

    REP(i, N) {
        string tmp;
        cin >> tmp;
        REP(j, N) {
            if(tmp[j]=='1') {
                edges[i].push_back(j);
                edges[j].push_back(i);
            }
        }
    }

    // cerr << "dfs" << endl;
    vector<int> connected_group(N);
    int group_idx = 1;
    REP(i, N) {
        if(connected_group[i] == 0) {
            dfs(i, edges, connected_group, group_idx);
            group_idx++;
        }
    }

    int group_num = group_idx - 1;
    vector<int> group_largest_size(group_num);

    // cerr << "bfs" << endl;
    FOR(group_idx, 1, group_num) {
        int largest_group_num = 0;
        vector<int> vertex_in_group;
        REP(i, N) if(connected_group[i]==group_idx) vertex_in_group.push_back(i);

        for(int v : vertex_in_group) {
            bool flag = true;
            queue<int> que;
            que.push(v);
            vector<int> group(N, 0);
            group[v] = 1;

            while(!que.empty()) {
                int v = que.front();
                que.pop();

                for (int nv : edges[v]) {
                    if (group[nv] > 0 && group[nv]%2 == group[v]%2) {
                        flag = false;
                        continue;
                    } else if (group[nv] > 0) {
                        continue;
                    } 

                    group[nv] = group[v] + 1;
                    que.push(nv);
                }

                if(!flag) break;
            }

            if(!flag) {
                cout << -1 << endl;
                return 0;
            }

            int ans_group = 0;
            REP(i, N) {
                if(ans_group < group[i]) ans_group = group[i];
            }
            if(ans_group > largest_group_num) largest_group_num = ans_group;
        }
        // cerr << "group: " << group_idx << " larget_size: " << largest_group_num << endl;
        group_largest_size[group_idx-1] = largest_group_num;
    }

    // cerr << group_num << endl;
    int ans = 0;
    REP(i, group_num) {
        // cerr << "group_largest_size[" << i << "] = " << group_largest_size[i] << endl;
        ans += group_largest_size[i];
    }

    cout << ans << endl;
    
    return 0;
}

Measured memory_bytes: 640