#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;
#else
#define debug 0;
#endif

template <typename T>
auto make_vector_impl(size_t sz, T t) {return std::vector<T>(sz, t);}

template <size_t N, typename T, typename U, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));}

template <size_t N, typename T, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {return std::vector<T>(sz);}

template <size_t N, typename T, typename... Args, std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector<N - 1, T>(args...));}

template <typename T, typename Size_t>
auto& at(T& t, Size_t i) {return t.at(i);}

template <typename T, typename Size_t, typename... Args>
auto& at(T& t, Size_t i, Args... args) {return at(t.at(i), args...);}

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

class union_find {
    int n;
    std::vector<int> prt;

  public:
    union_find (int n) : n(n), prt(n, -1){}

    bool is_root (int x)        const {return prt.at(x) < 0;}
    int  size    (int x)        const {return -prt.at(find(x));}
    bool same(int x, int y)     const {return find(x) == find(y);}
    int  find    (int x)        const {
      while (!is_root(x)) x = prt.at(x);
      return x;
    }
    // Returns `true` if x and y are newly connected.
    // The smaller one x becomes a child of the larger one y.
    bool unite   (int x, int y) {
      if ((x = find(x)) == (y = find(y))) return false;
      if (size(x) > size(y)) std::swap(x, y);
      prt.at(y) += prt.at(x);
      prt.at(x) = y;
      return true;
    }
};

template < class Value >
auto diameter_floyd_warshall(std::vector< std::vector< Value > > a) {
  int n = a.size();
  auto inf = std::numeric_limits< Value >::max() / 2;
  for (auto k = 0; k < n; k++) {
    for (auto i = 0; i < n; i++) {
      for (auto j = 0; j < n; j++) {
        auto & z = a.at(i).at(j);
        auto   x = a.at(i).at(k);
        auto   y = a.at(k).at(j);
        z = std::min(z, inf - x <= y
          ? inf : x + y);
      }
    }
  }
  auto ret = 0;
  for (auto i = 0; i < n; i++) {
    for (auto j = 0; j < n; j++) {
      ret = std::max(ret, a.at(i).at(j));
    }
  }
  return ret;
}

void err() { std::cout << -1 << std::endl; exit(0); }
#define require(exp) do { if (!(exp)) { err(); } } while(false)

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n; std::cin >> n;
  auto a = make_vector< 2, int > (n, n);
  rep(i, 0, n) rep(j, 0, n) {
    char c; std::cin >> c;
    at(a, i, j) = c == '1';
  }

  auto uf = union_find(2 * n);
  rep(i, 0, n) rep(j, i + 1, n) {
    if (at(a, i, j)) {
      uf.unite(i, j + n);
      uf.unite(j, i + n);
    }
  }
  if (uf.size(0) == 2 * n) err();
  assert(uf.size(0) == n);

  constexpr int inf = 1 << 30;
  rep(i, 0, n) rep(j, 0, n) {
    auto & crr = at(a, i, j);
    if (i != j && crr == 0) {
      crr = inf;
    }
  }
  auto diam = diameter_floyd_warshall(a);
  std::cout << diam + 1 << std::endl;
  return 0;
}


Measured memory_bytes: 640