#!/usr/bin/env python3

from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

from collections import deque

import sys

input = lambda: sys.stdin.readline()[:-1]

sys.setrecursionlimit(10**8)

def solve():

    n=int(eval(input()))

    data=[list(map(int,list(eval(input())))) for i in range(n)]

    fordfs=[[] for i in range(n)]

    for i in range(n):

        for j in range(n):

            if data[i][j]==1:

                fordfs[i].append(j)



    def dfs():

        V=[-1]*n

        V[0]=0

        que=deque()

        que.append(0)

        while que:

            now=que.popleft()

            nowv=V[now]

            for ne in fordfs[now]:

                if nowv==V[ne]:

                    print((-1))

                    exit()

                elif V[ne]==-1:

                    V[ne]=(nowv+1)%2

                    que.append(ne)

    

    dfs()

    # print(data)

    G=csgraph_from_dense(data)

    d = floyd_warshall(G)

    d.tolist()

    M=0

    for i in d:

        M=max(M,max(i))

    M=int(M)

    print((M+1))

    return







def main():



    solve()

    return

if __name__ == '__main__':

    main()

from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

from collections import deque

import sys





def input(): return sys.stdin.readline()[:-1]



n = int(eval(input()))

data = [list(map(int, list(eval(input())))) for i in range(n)]

fordfs = [[] for i in range(n)]

for i in range(n):

    for j in range(n):

        if data[i][j] == 1:

            fordfs[i].append(j)



def bfs():

    V = [-1]*n

    V[0] = 0

    que = deque()

    que.append(0)

    while que:

        now = que.popleft()

        nowv = V[now]

        for ne in fordfs[now]:

            if nowv == V[ne]:

                print((-1))

                exit()

            elif V[ne] == -1:

                V[ne] = (nowv+1) % 2

                que.append(ne)



bfs()

# print(data)

G = csgraph_from_dense(data)

d = floyd_warshall(G)

d.tolist()

M = 0

for i in d:

    M = max(M, max(i))

M = int(M)

print((M+1))

#!/usr/bin/env python3
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from collections import deque
import sys

input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)


def solve():
    n = int(eval(input()))
    data = [list(map(int, list(eval(input())))) for i in range(n)]
    fordfs = [[] for i in range(n)]
    for i in range(n):
        for j in range(n):
            if data[i][j] == 1:
                fordfs[i].append(j)

    def dfs():
        V = [-1] * n
        V[0] = 0
        que = deque()
        que.append(0)
        while que:
            now = que.popleft()
            nowv = V[now]
            for ne in fordfs[now]:
                if nowv == V[ne]:
                    print((-1))
                    exit()
                elif V[ne] == -1:
                    V[ne] = (nowv + 1) % 2
                    que.append(ne)

    dfs()
    # print(data)
    G = csgraph_from_dense(data)
    d = floyd_warshall(G)
    d.tolist()
    M = 0
    for i in d:
        M = max(M, max(i))
    M = int(M)
    print((M + 1))
    return


def main():
    solve()
    return


if __name__ == "__main__":
    main()

from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from collections import deque
import sys


def input():
    return sys.stdin.readline()[:-1]


n = int(eval(input()))
data = [list(map(int, list(eval(input())))) for i in range(n)]
fordfs = [[] for i in range(n)]
for i in range(n):
    for j in range(n):
        if data[i][j] == 1:
            fordfs[i].append(j)


def bfs():
    V = [-1] * n
    V[0] = 0
    que = deque()
    que.append(0)
    while que:
        now = que.popleft()
        nowv = V[now]
        for ne in fordfs[now]:
            if nowv == V[ne]:
                print((-1))
                exit()
            elif V[ne] == -1:
                V[ne] = (nowv + 1) % 2
                que.append(ne)


bfs()
# print(data)
G = csgraph_from_dense(data)
d = floyd_warshall(G)
d.tolist()
M = 0
for i in d:
    M = max(M, max(i))
M = int(M)
print((M + 1))

-#!/usr/bin/env python3 -input = lambda: sys.stdin.readline()[:-1] -sys.setrecursionlimit(10**8) + +def input(): +    return sys.stdin.readline()[:-1] -def solve(): -    n = int(eval(input())) -    data = [list(map(int, list(eval(input())))) for i in range(n)] -    fordfs = [[] for i in range(n)] -    for i in range(n): -        for j in range(n): -            if data[i][j] == 1: -                fordfs[i].append(j) - -    def dfs(): -        V = [-1] * n -        V[0] = 0 -        que = deque() -        que.append(0) -        while que: -            now = que.popleft() -            nowv = V[now] -            for ne in fordfs[now]: -                if nowv == V[ne]: -                    print((-1)) -                    exit() -                elif V[ne] == -1: -                    V[ne] = (nowv + 1) % 2 -                    que.append(ne) - -    dfs() -    # print(data) -    G = csgraph_from_dense(data) -    d = floyd_warshall(G) -    d.tolist() -    M = 0 -    for i in d: -        M = max(M, max(i)) -    M = int(M) -    print((M + 1)) -    return +n = int(eval(input())) +data = [list(map(int, list(eval(input())))) for i in range(n)] +fordfs = [[] for i in range(n)] +for i in range(n): +    for j in range(n): +        if data[i][j] == 1: +            fordfs[i].append(j) -def main(): -    solve() -    return +def bfs(): +    V = [-1] * n +    V[0] = 0 +    que = deque() +    que.append(0) +    while que: +        now = que.popleft() +        nowv = V[now] +        for ne in fordfs[now]: +            if nowv == V[ne]: +                print((-1)) +                exit() +            elif V[ne] == -1: +                V[ne] = (nowv + 1) % 2 +                que.append(ne) -if __name__ == "__main__": -    main() +bfs() +# print(data) +G = csgraph_from_dense(data) +d = floyd_warshall(G) +d.tolist() +M = 0 +for i in d: +    M = max(M, max(i)) +M = int(M) +print((M + 1))