#直径が答えになるわけではない



N = int(eval(input()))

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



# for s in S:

#     print (s)



lst = [0] * N

lst[0] = 1



stack = [0]

flag = True

while len(stack) != 0:

    tmp = stack.pop()

    for index, x in enumerate(S[tmp]):

        if x == 0:

            continue

        if lst[index] == 0:

            lst[index] = -lst[tmp]

            stack.append(index)

        else:

            if lst[tmp] == -lst[index]:

                pass

            else:

                flag = False

                break

    if not flag:

        print((-1))

        exit()



INF = 10 ** 9

for i in range(N):

    for j in range(N):

        if S[i][j] == 0 and i != j:

            S[i][j] = INF



for k in range(N):

    for i in range(N):

        for j in range(N):

            S[i][j] = min(S[i][j], S[i][k] + S[k][j])

            

ans = 0

for i in range(N):

    for j in range(N):

        ans = max(S[i][j], ans)



print((ans + 1))
N = int(eval(input()))

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



lst = [0] * N

lst[0] = 1



stack = [0]

flag = True

while len(stack) != 0:

    tmp = stack.pop()

    for index, x in enumerate(S[tmp]):

        if x == 0:

            continue

        if lst[index] == 0:

            lst[index] = -lst[tmp]

            stack.append(index)

        else:

            if lst[tmp] == -lst[index]:

                pass

            else:

                flag = False

                break

    if not flag:

        print((-1))

        exit()



import numpy as np

from scipy.sparse.csgraph import floyd_warshall



g = np.array(S)

anslst = floyd_warshall(g)

ans = int(np.max(anslst))



print((ans + 1))
# 直径が答えになるわけではない
N = int(eval(input()))
S = [list(map(int, eval(input()))) for _ in range(N)]
# for s in S:
#     print (s)
lst = [0] * N
lst[0] = 1
stack = [0]
flag = True
while len(stack) != 0:
    tmp = stack.pop()
    for index, x in enumerate(S[tmp]):
        if x == 0:
            continue
        if lst[index] == 0:
            lst[index] = -lst[tmp]
            stack.append(index)
        else:
            if lst[tmp] == -lst[index]:
                pass
            else:
                flag = False
                break
    if not flag:
        print((-1))
        exit()
INF = 10**9
for i in range(N):
    for j in range(N):
        if S[i][j] == 0 and i != j:
            S[i][j] = INF
for k in range(N):
    for i in range(N):
        for j in range(N):
            S[i][j] = min(S[i][j], S[i][k] + S[k][j])
ans = 0
for i in range(N):
    for j in range(N):
        ans = max(S[i][j], ans)
print((ans + 1))

N = int(eval(input()))
S = [list(map(int, eval(input()))) for _ in range(N)]
lst = [0] * N
lst[0] = 1
stack = [0]
flag = True
while len(stack) != 0:
    tmp = stack.pop()
    for index, x in enumerate(S[tmp]):
        if x == 0:
            continue
        if lst[index] == 0:
            lst[index] = -lst[tmp]
            stack.append(index)
        else:
            if lst[tmp] == -lst[index]:
                pass
            else:
                flag = False
                break
    if not flag:
        print((-1))
        exit()
import numpy as np
from scipy.sparse.csgraph import floyd_warshall

g = np.array(S)
anslst = floyd_warshall(g)
ans = int(np.max(anslst))
print((ans + 1))

-# 直径が答えになるわけではない -# for s in S: -#     print (s) -INF = 10**9 -for i in range(N): -    for j in range(N): -        if S[i][j] == 0 and i != j: -            S[i][j] = INF -for k in range(N): -    for i in range(N): -        for j in range(N): -            S[i][j] = min(S[i][j], S[i][k] + S[k][j]) -ans = 0 -for i in range(N): -    for j in range(N): -        ans = max(S[i][j], ans) +import numpy as np +from scipy.sparse.csgraph import floyd_warshall + +g = np.array(S) +anslst = floyd_warshall(g) +ans = int(np.max(anslst))