from queue import Queue





def main():

    n = int(eval(input()))

    S = [list(map(int, eval(input()))) for _ in range(n)]





    def bfs(s):

        WHITE = 0

        GRAY = 1

        BLACK = 2



        color = [WHITE] * n

        d = [-1] * n

        q = Queue()

        q.put(s)

        color[s] = GRAY

        d[s] = 0

        while not q.empty():

            u = q.get()

            color[u] = BLACK

            for v, edge in enumerate(S[u]):

                if edge and color[v] == BLACK and d[v] + 1 != d[u]:

                    return -1

                if edge and color[v] == WHITE:

                    q.put(v)

                    color[v] = GRAY

                    d[v] = d[u] + 1



        return max(d) + 1



    cnt = -1

    for i in range(n):

        cnt = max(cnt, bfs(i))



    print(cnt)





if __name__ == "__main__":

    main()

import numpy as np

from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall





def main():

    n = int(eval(input()))

    s = [list(map(int, eval(input()))) for _ in range(n)]



    s = np.array(s)



    g = csgraph_from_dense(s)



    dist_mat = floyd_warshall(g).astype(np.int32)



    dist_max = dist_mat.max()



    a = np.where(dist_mat[0] % 2 == 0)

    b = np.where(dist_mat[0] % 2 == 1)

    bl = (s[a][:,a] == 0).all() and (s[b][:,b] == 0).all()



    ans = dist_max + 1 if bl else -1



    print(ans)





if __name__ == "__main__":

    main()

from queue import Queue


def main():
    n = int(eval(input()))
    S = [list(map(int, eval(input()))) for _ in range(n)]

    def bfs(s):
        WHITE = 0
        GRAY = 1
        BLACK = 2
        color = [WHITE] * n
        d = [-1] * n
        q = Queue()
        q.put(s)
        color[s] = GRAY
        d[s] = 0
        while not q.empty():
            u = q.get()
            color[u] = BLACK
            for v, edge in enumerate(S[u]):
                if edge and color[v] == BLACK and d[v] + 1 != d[u]:
                    return -1
                if edge and color[v] == WHITE:
                    q.put(v)
                    color[v] = GRAY
                    d[v] = d[u] + 1
        return max(d) + 1

    cnt = -1
    for i in range(n):
        cnt = max(cnt, bfs(i))
    print(cnt)


if __name__ == "__main__":
    main()

import numpy as np
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall


def main():
    n = int(eval(input()))
    s = [list(map(int, eval(input()))) for _ in range(n)]
    s = np.array(s)
    g = csgraph_from_dense(s)
    dist_mat = floyd_warshall(g).astype(np.int32)
    dist_max = dist_mat.max()
    a = np.where(dist_mat[0] % 2 == 0)
    b = np.where(dist_mat[0] % 2 == 1)
    bl = (s[a][:, a] == 0).all() and (s[b][:, b] == 0).all()
    ans = dist_max + 1 if bl else -1
    print(ans)


if __name__ == "__main__":
    main()

-from queue import Queue +import numpy as np +from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall -    S = [list(map(int, eval(input()))) for _ in range(n)] - -    def bfs(s): -        WHITE = 0 -        GRAY = 1 -        BLACK = 2 -        color = [WHITE] * n -        d = [-1] * n -        q = Queue() -        q.put(s) -        color[s] = GRAY -        d[s] = 0 -        while not q.empty(): -            u = q.get() -            color[u] = BLACK -            for v, edge in enumerate(S[u]): -                if edge and color[v] == BLACK and d[v] + 1 != d[u]: -                    return -1 -                if edge and color[v] == WHITE: -                    q.put(v) -                    color[v] = GRAY -                    d[v] = d[u] + 1 -        return max(d) + 1 - -    cnt = -1 -    for i in range(n): -        cnt = max(cnt, bfs(i)) -    print(cnt) +    s = [list(map(int, eval(input()))) for _ in range(n)] +    s = np.array(s) +    g = csgraph_from_dense(s) +    dist_mat = floyd_warshall(g).astype(np.int32) +    dist_max = dist_mat.max() +    a = np.where(dist_mat[0] % 2 == 0) +    b = np.where(dist_mat[0] % 2 == 1) +    bl = (s[a][:, a] == 0).all() and (s[b][:, b] == 0).all() +    ans = dist_max + 1 if bl else -1 +    print(ans)