#include <bits/stdc++.h>

using namespace std;

#define sz(x) ((int) ((x).size()))
typedef long long ll;
typedef long double ld;

int n;
vector<int> e[400];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> n;
  char c;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      cin >> c;
      if (c == '1')
        e[i].push_back(j);
    }
  int k = -1;
  for (int i = 0; i < n; i++) {
    queue<int> q;
    vector<int> num(n, -1);
    num[i] = 0;
    q.push(i);
    int h = 0;
    bool ok = true;
    while (!q.empty() && ok) {
      int j = q.front();
      q.pop();
      for (int l : e[j]) {
        if (num[l] >= 0)
          ok = ok && abs(num[j] - num[l]) == 1;
        else {
          num[l] = num[j] + 1;
          h = num[l];
          q.push(l);
        }
      }
    }
    if (ok)
      k = max(k, h + 1);
  }
  cout << k << "\n";
}
