#include<algorithm>
#include<cassert>
#include<cfloat>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << endl

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-8;

constexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
constexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

class UnionFind{
    vector<int> par;
    int groupCount;

public:
    UnionFind(int n = 0){
        init(n);
    }

    void init(int n = 0){
        par.resize(n);
        fill(par.begin(), par.end(), -1);

        groupCount = n;

        return;
    }

    int root(int x){
        if(par[x] < 0){
            return x;
        }
        return par[x] = root(par[x]);
    }

    bool same(int x, int y){
        return root(x) == root(y);
    }

    int size(int x){
        return -par[root(x)];
    }

    bool unite(int x, int y){
        x = root(x);
        y = root(y);

        if(x == y){
            return false;
        }

        if(par[y] < par[x]){
            swap(x, y);
        }

        par[x] += par[y];
        par[y] = x;
        --groupCount;

        return true;
    }

    int size(void){
        return groupCount;
    }
};

template<typename T>
bool chmax(T& a, T b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmin(T& a, T b){
    if(b < a){
        a = b;
        return true;
    }
    return false;
}

bool warshall_floyd(vector< vector<long long> >& cost){
    const int v = (int)cost.size();

    constexpr long long INF = 1LL << 61;

    for(int k = 0 ; k < v ; ++k){
        for(int i = 0 ; i < v ; ++i){
            for(int j = 0 ; j < v ; ++j){
                if(cost[i][k] < INF && cost[k][j] < INF){
                    cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
                }
            }
        }
    }

    for(int i = 0 ; i < v ; ++i){
        if(cost[i][i] < 0LL){
            return false;
        }
    }

    return true;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vector< vector<lint> > cost(n, vector<lint>(n, LLINF));

    vector<string> s(n);
    rep(i, n){
        cin >> s[i];
        rep(j, n){
            if(s[i][j] == '1'){
                chmin(cost[i][j], 1LL);
            }
        }
        chmin(cost[i][i], 0LL);
    }

    warshall_floyd(cost);

    rep(i, n){
        rep(j, n){
            rep(k, n){
                if((cost[i][j] + cost[j][k] + cost[k][i]) & 1LL){
                    cout << -1 << endl;
                    return 0;
                }
            }
        }
    }

    lint ans = 0;
    rep(i, n){
        rep(j, n){
            chmax(ans, cost[i][j]);
        }
    }
    ++ans;

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 640