#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_dijkstra(std::vector< std::vector< std::pair< int, Value > > > graph) {
  int n = graph.size();
  auto inf = std::numeric_limits< Value >::max();
  Value ret = 0;
  std::vector< Value > dist(n, inf);
  for (auto i = 0; i < n; i++) {
    std::priority_queue<
      std::pair< Value, int >,
      std::vector < std::pair< Value, int > >,
      std::greater< std::pair< Value, int > > > que;
    auto root = i;
    dist.assign(n, inf);
    que.emplace(0, root);
    dist.at(root) = 0;
    while (!que.empty()) {
      Value d; int crr; std::tie(d, crr) = que.top(); que.pop();
      for (auto pair : graph.at(crr)) {
        int nxt; Value w; std::tie(nxt, w) = pair;
        auto e = d + w;
        if (e < dist.at(nxt)) {
          dist.at(nxt) = e;
          que.emplace(e, nxt);
        }
      }
    }
    for (auto d : dist)
      { ret = std::max(ret, d); }
  }
  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;
    }
  }

  // adjacency list
  auto graph = make_vector< 2, std::pair< int, int > > (n, 0);
  rep(i, 0, n) rep(j, i + 1, n) {
    auto x = at(a, i, j);
    if (x == 1) {
      graph.at(i).emplace_back(j, 1);
      graph.at(j).emplace_back(i, 1);
    }
  }

  auto diam = diameter_dijkstra(graph);
  std::cout << diam + 1 << std::endl;
  return 0;
}


Measured memory_bytes: 768