#include<bits/stdc++.h>
using namespace std;
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353 
#define MEM_SIZE 100010
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

 
 bool Bipartite_graph_DFS(int v,int c, vector<vector<int> >& GRAPH,vector<int> & color)
{
  color[v] = c;
  for (int i = 0; i < GRAPH[v].size(); i++)
  {
    if(color[GRAPH[v][i]] == c) return false;
    if(color[GRAPH[v][i]] == 0 && !Bipartite_graph_DFS(GRAPH[v][i],~c,GRAPH,color)) return false;
  }
  
  return true;
}

vector<int> GRAPH_BFS(vector<vector<int> > GRAPH,int S)
{

  int N = GRAPH.size();
  vector<int> DIST (N,INF);
  vector<int> CNT (N,0);
  DIST[S] = 0;
  CNT[S] = 1;
  deque<int> q;
  q.push_back(S);
  while(!q.empty())
  {
    int V = q.front();
    q.pop_front();

    // DEBUG(V);
    for (auto &&nV : GRAPH[V])
    {
      if(DIST[nV] == DIST[V]+1)
      {
        CNT[nV] += CNT[V];
        CNT[nV] %= MOD;
      }
      else if(DIST[nV] > DIST[V] + 1)
      {
        CNT[nV] = CNT[V];
        if(DIST[nV] == INF)
        q.push_back(nV);
        DIST[nV] = DIST[V] + 1;
      }
    }
  }
  return DIST;
}

void solve(void)
{
  int N;cin>>N;
  vector<vector<int> > mat (N,vector<int>(N,0));
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < N; j++)
    {
      char X;cin>>X;
      int Z = X - '0';
      mat[i][j] = Z;
    }
  }
  
  vector<vector<int> > GRAPH(N);
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < N; j++)
    {
      if(mat[i][j] == 1)
      {
        GRAPH[i].push_back(j);
      }
    }
  }

  vector<int> color(N,0);
  int flg = 1;
  for (int i = 0; i < N; i++)
  {
    if(color[i] == 0)
    {
      if(!Bipartite_graph_DFS(i,1,GRAPH,color))
      {
        flg = 0;
        cout<<-1<<endl;
        return;
      }
    }
  }
  if(flg == 0)
  {
    cout<<-1<<endl;
    return;
  }
  int res = 0;
  for (int i = 0; i < N; i++)
  {
    auto D = GRAPH_BFS(GRAPH,i);
    for (int j = 0; j < N; j++)
    {
      chmax(res,D[j]);
    }
    
  }
  cout<<res + 1<<endl;
  


  
  
  return;
}


int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


Measured memory_bytes: 4204