def b(u,x):

 c[u]=x

 return all((c[u]!=c[v])&(c[v]or b(v,-x))for v in X if d[u][v]==1)

N=int(eval(input()))

X=list(range(N))

S=[eval(input())for _ in X]

d=[[1 if S[i][j]=='1'else(N if i^j else 0)for j in X]for i in X]

for k in X:

 for i in X:

  for j in X:d[i][j]=min(d[i][j],d[i][k]+d[k][j])

c=[0]*N

print((max(max(e)for e in d)+1 if b(0,1)else-1))
import scipy.sparse.csgraph as s

def b(u,x):c[u]=x;return all(d[u][v]^1or(c[u]!=c[v])&(c[v]or b(v,-x))for v in X)

N,*S=open(0)

N=int(N)

X=list(range(N))

d=[[i^j and(S[i][j]=='1'or N)for j in X]for i in X]

c=[0]*N

print((int(b(0,1)and s.floyd_warshall(d).max()+2)-1))

def b(u, x):
    c[u] = x
    return all((c[u] != c[v]) & (c[v] or b(v, -x)) for v in X if d[u][v] == 1)


N = int(eval(input()))
X = list(range(N))
S = [eval(input()) for _ in X]
d = [[1 if S[i][j] == "1" else (N if i ^ j else 0) for j in X] for i in X]
for k in X:
    for i in X:
        for j in X:
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])
c = [0] * N
print((max(max(e) for e in d) + 1 if b(0, 1) else -1))

import scipy.sparse.csgraph as s


def b(u, x):
    c[u] = x
    return all(d[u][v] ^ 1 or (c[u] != c[v]) & (c[v] or b(v, -x)) for v in X)


N, *S = open(0)
N = int(N)
X = list(range(N))
d = [[i ^ j and (S[i][j] == "1" or N) for j in X] for i in X]
c = [0] * N
print((int(b(0, 1) and s.floyd_warshall(d).max() + 2) - 1))

+import scipy.sparse.csgraph as s + + -    return all((c[u] != c[v]) & (c[v] or b(v, -x)) for v in X if d[u][v] == 1) +    return all(d[u][v] ^ 1 or (c[u] != c[v]) & (c[v] or b(v, -x)) for v in X) -N = int(eval(input())) +N, *S = open(0) +N = int(N) -S = [eval(input()) for _ in X] -d = [[1 if S[i][j] == "1" else (N if i ^ j else 0) for j in X] for i in X] -for k in X: -    for i in X: -        for j in X: -            d[i][j] = min(d[i][j], d[i][k] + d[k][j]) +d = [[i ^ j and (S[i][j] == "1" or N) for j in X] for i in X] -print((max(max(e) for e in d) + 1 if b(0, 1) else -1)) +print((int(b(0, 1) and s.floyd_warshall(d).max() + 2) - 1))