#include <bits/stdc++.h>

#define TYPE(c)   remove_reference_t<decltype(c)>
#define REP(i, n) for(TYPE(n) i = 0; i < n; i++)
#define FOR(v, c) for(TYPE(c.begin()) v = c.begin(); v != c.end(); v++)
#define ALL(c)    c.begin(), c.end()
#define SORT(c)   sort(ALL(c))
#define RSORT(c)  sort(ALL(c), greater<TYPE(c)::value_type>())
#define UNIQUE(c) c.erase(unique(ALL(c)), c.end())

using namespace std;
using ll = long long;

constexpr int MOD  = (int)1e9 + 7;
constexpr int INF  = (int)1e9 + 1;
constexpr ll  LINF = (ll)1e18 + 1;
template<typename T> constexpr bool chmax(T& a, const T& b)
{ if(a < b) { a = b; return true; } else { return false; } }
template<typename T> constexpr bool chmin(T& a, const T& b)
{ if(b < a) { a = b; return true; } else { return false; } }
template<typename S, typename T> ostream& operator<<(ostream &os, const pair<S, T>& p)
{ return os << "(" << p.first << "," << p.second << ")"; }
ostream& operator<<(ostream &os, const string& s)
{ FOR(v, s) os << *v; return os; }
template<typename T> auto operator<<(ostream& os, const T& t)
    -> typename conditional<true, ostream&, decltype(*t.begin())>::type
{ os << "["; FOR(v, t) { if(v != t.begin()) os << ","; os << *v; } os << "]"; return os; }


class Graph {
    using ll = long long;
public:
    struct Edge {
        struct Info {
            ll   cost{1};
            bool validity{true};
            Info() = default;
            explicit Info(const ll& _cost) : cost(_cost) { }
            Info(const ll& _cost, const bool& _validity) : cost(_cost), validity(_validity) { }
            friend std::ostream& operator <<(std::ostream& os, const std::shared_ptr<Info>& i) {
                os << "cost : " << i->cost << ", " << "validity : " << i->validity;
                return os;
            }
        };

        enum class Type { Directed, Undirected };

        int src, dst;
        std::shared_ptr<Info> info;

        Edge(const int& _src, const int& _dst, const std::shared_ptr<Info>& _info) : src(_src), dst(_dst), info(_info) { }
        friend std::ostream& operator <<(std::ostream& os, const Edge& e) {
            os << "src : " << e.src << ", " << "dst : " << e.dst << ", " << "info : [" << e.info << "]";
            return os;
        }
    };

    struct Vertex {
        ll cost;
        explicit Vertex(const ll& _cost) : cost(_cost) { }
        friend std::ostream& operator <<(std::ostream& os, const Vertex& v) {
            os << "cost : " << v.cost;
            return os;
        }
    };

    const Edge::Type edge_type;

    std::vector<Vertex>            vertices;
    std::vector<std::vector<Edge>> edges;

    Graph(const int&        num_vertex,
          const Edge::Type& edge_type,
          const ll&         vertex_init_cost = (ll)1e9 + 1) :
        edge_type(edge_type),
        vertices(num_vertex, Vertex(vertex_init_cost)),
        edges(num_vertex) { }

    void setCostToAllVertex(const ll& val) {
        std::fill(vertices.begin(), vertices.end(), Vertex(val));
    };

    void addEdge(const int& src, const int& dst, const ll& cost = 1) {
        auto info = std::make_shared<Edge::Info>(cost);
        edges[src].emplace_back(src, dst, info);
        if(edge_type == Edge::Type::Undirected) edges[dst].emplace_back(dst, src, info);
    }

    void invalidateEdge(const int& src, const int& dst) {
        for(auto& edge : edges[src]) if(edge.dst == dst) { edge.info->validity = false; return; }
    }

    friend std::ostream& operator <<(std::ostream& os, const Graph& g) {
        os << std::endl
           << "--- Vertex Information ---" << std::endl;
        for(size_t i = 0; i < g.vertices.size(); i++) {  os << "[[" << i << "] " << g.vertices[i] << "]" << std::endl; }
        os << "---- Edge Information ----" << std::endl;
        for(const auto& v : g.edges) { for(const auto& e : v) { os << "[" << e << "]" << std::endl; } }
        os << "--------------------------";
        return os;
    }
};

/**
 *  BFSによる二部グラフ判定
 *  NOTE: Dependencies: comp-Graph, 0と1で塗り分ける
 */
// 使用例
// g.setCostToAllVertex(INF);
// judge_bipartite_graph(g, 0, INF);
bool judge_bipartite_graph(Graph&     g,
                           const int& start,
                           const ll&  inf_val = (ll)1e18 + 1) {
    std::queue<int> q;
    q.push(start);
    g.vertices[start].cost = 0;
    while(!q.empty()) {
        auto top = q.front(); q.pop();
        for(const auto& e : g.edges[top]) {
            if(g.vertices[e.dst].cost == inf_val) {
                g.vertices[e.dst].cost = (g.vertices[e.src].cost == 1) ? 0 : 1;
                q.push(e.dst);
            }
            else if(g.vertices[e.dst].cost == g.vertices[e.src].cost) {
                return false;
            }
        }
    }
    return true;
}

/**
 *  全頂点間の最短距離を求める(Warshall-Floyd法)
 *  NOTE: 計算量O(|V|^3)
 *  @Return: array[i][j] :: 頂点iから頂点jまでの最短距離
 */
// 使用例(グラフの直径)
// auto table    = warshall_floyd(g);
// auto diameter = 0;
// REP(i, g.vertices.size()) {
//     REP(j, g.vertices.size()) {
//         if(j <= i) continue;
//         chmax(diameter, table[j][i]);
//     }
// }
std::vector<std::vector<ll>> warshall_floyd(const Graph& g,
                                            const ll& inf_val = (ll)1e18 + 1) {
    std::vector<std::vector<ll>> dp(g.vertices.size(), std::vector<ll>(g.vertices.size(), inf_val));
    for(const auto& ee : g.edges) for(const auto& e : ee) dp[e.src][e.dst] = e.info->cost;
    for(size_t i = 0; i < g.vertices.size(); i++)
        for(size_t j = 0; j < g.vertices.size(); j++)
            for(size_t k = 0; k < g.vertices.size(); k++)
                dp[j][k] = std::min(dp[j][k], dp[j][i] + dp[i][k]);
    return dp;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    std::cin >> N;
    Graph g(N, Graph::Edge::Type::Undirected, INF);
    REP(i, N) {
        string s;
        std::cin >> s;
        REP(j, N) {
            if(j <= i) continue;
            if(s[j] == '1') g.addEdge(i, j, 1);
        }
    }

    if(judge_bipartite_graph(g, 0, INF)) {
        auto table    = warshall_floyd(g);
        ll   diameter = 0;
        REP(i, g.vertices.size()) {
            REP(j, g.vertices.size()) {
                if(j <= i) continue;
                chmax(diameter, table[j][i] + 1);
            }
        }
        std::cout << diameter << std::endl;
    }
    else {
        std::cout << -1 << std::endl;
    }

    return 0;
}


Measured memory_bytes: 2432