#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<string> g(n);
  for (int i = 0; i < n; i++) {
    cin >> g[i];
  }
  int ans = 0;
  for (int st = 0; st < n; st++) {
    vector<int> dist(n, -1);
    vector<int> que(1, st);
    dist[st] = 0;
    for (int b = 0; b < (int) que.size(); b++) {
      for (int i = 0; i < n; i++) {
        if (g[que[b]][i] == '1' && dist[i] == -1) {
          que.push_back(i);
          dist[i] = dist[que[b]] + 1;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (g[i][j] == '1' && dist[i] == dist[j]) {
          cout << -1 << '\n';
          return 0;
        }
      }
    }
    int mx = *max_element(dist.begin(), dist.end());
    ans = max(ans, mx);
  }
  cout << ans + 1 << endl;
  return 0;
}