#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include<unordered_map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include<bitset>
#include<deque>
#include<string>
#include<list>
#include<iterator>
#include<sstream>
#include <algorithm>
# include <math.h>
#define lli long long int
#define lower(a) transform(a.begin(),a.end(),a.begin(),::tolower);

using namespace std;

const int N = 3e5 + 500;
const long long mod = 1e9 + 7;
const long long cmod = 998244353 ;
const long long inf = 1LL << 57;
const int M = 1e6 + 500;
const lli ths = 1LL << 40;

vector<lli> graph[400];
bool vis[400];
map<lli, lli > dp_lvl;
bool ok = 1;
lli adj[400][400];
lli n;

void dfs()
{
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= n; j ++)
        {
            if(adj[i][j] == 1)
            {
                if(abs(dp_lvl[i] - dp_lvl[j]) != 1)
                {
                    ok = 0;
                    cout<<-1;
                    return ;
                }
            }
        }
    }
}

void solve()
{
    cin>>n;
    for(int i = 1; i <= n; i ++)
    {
        string s;
        cin>>s;
        for(int j = 0; j < n; j ++)
        {
            if(s[j] == '1')
            {
                adj[i][j + 1] = 1;
                graph[i].push_back(j + 1);
            }
            else
                adj[i][j + 1] = 0;
        }
    }
    /*
    lli idx = -1, mi = 1e9;
    for(int i = 1; i <= n; i ++)
    {
        if(graph[i].size() < mi)
        {
            mi = graph[i].size();
            idx = i;
        }
    }
    */
    lli pr = 0;
    for(int idx = 1; idx <= n; idx ++){
    queue<lli> q;
    lli ans = 0;
    set<lli>  level[400];
    level[0].insert(idx);
    dp_lvl[idx] = 0;
    /*
    for(auto it : graph[idx]){
        q.push(it);
        level[1].insert(it);
        dp_lvl[it] = 0;
    }
    */
    memset(vis, 0, sizeof vis);
    q.push(idx);
    vis[idx] = 1;
    vector<lli> v;
    lli lvl = 1;
    while(!q.empty())
    {
        lli u = q.front();
        q.pop();
        for(auto it : graph[u])
        {
            if(vis[it])
                continue ;
            vis[it] = 1;
            v.push_back(it);
            level[lvl].insert(it);
            dp_lvl[it] = lvl;
        }
        if(q.empty())
        {
            ans ++;
            for(auto vert : v)
                q.push(vert);
            lvl ++;
            v.clear();
        }
    }
    memset(vis, 0, sizeof vis);
    ok = 1;
    dfs();
    if(ok == 0)
        return ;
    /*
    for(int i = 0; i < lvl; i ++)
    {
        for(auto it : level[i])
            cout<<it<<' ';
        cout<<endl;
    }
    */
    ok = 0;
    for(auto it : level[lvl - 1])
    {
        if(graph[it].size() == 1)
            ok = 1;
    }
    if(ok)
        ans = min(ans + 1, n);
    pr = max(pr, ans);
    }
    cout<<pr;
    return ;
}
/*
7
6 7 4 1 2 11 6
5
1 2 10 4 5
*/

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

    lli t;
    t = 1;
//    cin>>t;
    while(t --)
    {
        solve();
    }
    return 0;
}


Measured memory_bytes: 1408