#include<iostream>
#include<vector>
#include<queue>
using namespace std;
long long inf=1e18+7;
struct Node{
  int no;
  int par;
  int col;
  long long dist;
  vector<pair<Node*,long long>> child;
  void build(int i){
    no=i;par=i;col=0;dist=inf;
  }
};
bool operator >(const Node &a,const Node &r){
        return a.dist > r.dist;
    }
struct Graph{
    int n;
    bool direct;
    vector<Node> node;
    Graph(int N){
        n=N;
        node.resize(n+1);
        for(int i=1;i<=n;++i){
            node[i].build(i);
        }
    }
    void input(int m,bool flag){
        direct=flag;
        for(int i=0;i<m;++i){
            int l=1;
            int a,b;cin>>a>>b>>l;
            node[a].child.push_back({&node[b],l});
            if(!direct){
                node[b].child.push_back({&node[a],l});
            }
        }
    }
    void dijkstra(int s){
        for(int i=1;i<=n;++i){
            node[i].dist=inf;
        }
        node[s].dist=0;
        priority_queue<Node,vector<Node>,greater<Node>> que;
        que.push(node[s]);
        while(!que.empty()){
            Node buf=que.top();que.pop();
            int num=buf.child.size();
            for(int i=0;i<num;++i){
                if(buf.child[i].first->dist>buf.child[i].second+buf.dist){
                    buf.child[i].first->dist=buf.child[i].second+buf.dist;
                    que.push(*buf.child[i].first);
                }
            }
        }
    }
    bool binary_graph(){
        node[1].col=1;
        queue<Node> que;
        que.push(node[1]);
        bool res=true;
        while(!que.empty()){
            Node buf=que.front();que.pop();
            int num=buf.child.size();
            for(int i=0;i<num;++i){
                if(buf.child[i].first->col==0){
                    buf.child[i].first->col=buf.col*2%3;
                    que.push(*buf.child[i].first);
                }
                else{
                    if(buf.child[i].first->col==buf.col){
                        res=false;
                    }
                }
            }
        }
        return res;
    }
    vector<vector<long long>> warshall_froyd(){
        vector<vector<long long>> dp(n+1,vector<long long>(n+1,inf));
        for(int i=1;i<=n;++i){
            int num=node[i].child.size();
            for(int j=0;j<num;++j){
                dp[i][node[i].child[j].first->no]=node[i].child[j].second;
            }
            dp[i][i]=0;
        }
        for(int k=1;k<=n;++k){
            for(int i=1;i<=n;++i){
                for(int j=1;j<=n;++j){
                    dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
                }
            }
        }
        return dp;
    }
};

int main(){
    int n;cin>>n;
    Graph g(n);
    for(int i=1;i<=n;++i){
        string buf;cin>>buf;
        for(int j=1;j<=n;++j){
            if(buf[j-1]=='1'){
                g.node[i].child.push_back({&g.node[j],1});
            }
        }
    }
    if(!g.binary_graph()){
        cout<<-1<<"\n";
        return 0;
    }
    long long res=0;
    vector<vector<long long>> dp;
    dp=g.warshall_froyd();
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            if(dp[i][j]!=inf){
                res=max(res,dp[i][j]);
            }
        }
    }
    cout<<res+1<<"\n";
    return 0;
}

Measured memory_bytes: 1664