#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;
    }
};

auto diameter_bfs(std::vector< std::vector< int > > graph) {
  int n = graph.size();
  auto ret = 0;
  std::vector< int > dist(n, -1);
  for (auto root = 0; root < n; root++) {
    dist.assign(n, -1);
    std::queue< int > que;
    que.emplace(root);
    dist.at(root) = 0;
    while (!que.empty()) {
      auto crr = que.front(); que.pop();
      for (auto nxt : graph.at(crr)) {
        if (dist.at(nxt) == -1) {
          dist.at(nxt) = dist.at(crr) + 1;
          que.emplace(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, 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);
      graph.at(j).emplace_back(i);
    }
  }

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


Measured memory_bytes: 640