import sys

input = sys.stdin.readline



def floyd_warshall(dist):

    for k in range(N):

        for i in range(N):

            for j in range(N):

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



def is_bipartite(u):

    for v in range(N):

        if u == v or dist[u][v] != 1:

            continue

        if color[v] == -1:

            color[v] = 1 - color[u]

            if not is_bipartite(v):

                return False

        elif color[u] == color[v]:

            return False

    return True



N = int(eval(input()))

S = [input().rstrip() for _ in range(N)]

dist = [[1 if S[i][j] == '1' else (0 if i == j else float('inf')) for j in range(N)] for i in range(N)]

color = [-1] * N

color[0] = 0

ans = 0

if is_bipartite(0):

    floyd_warshall(dist)

    ans = max(max(d) for d in dist) + 1

else:

    ans = -1

print(ans)

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 sys

input = sys.stdin.readline


def floyd_warshall(dist):
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])


def is_bipartite(u):
    for v in range(N):
        if u == v or dist[u][v] != 1:
            continue
        if color[v] == -1:
            color[v] = 1 - color[u]
            if not is_bipartite(v):
                return False
        elif color[u] == color[v]:
            return False
    return True


N = int(eval(input()))
S = [input().rstrip() for _ in range(N)]
dist = [
    [1 if S[i][j] == "1" else (0 if i == j else float("inf")) for j in range(N)]
    for i in range(N)
]
color = [-1] * N
color[0] = 0
ans = 0
if is_bipartite(0):
    floyd_warshall(dist)
    ans = max(max(d) for d in dist) + 1
else:
    ans = -1
print(ans)

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 sys - -input = sys.stdin.readline - - -def floyd_warshall(dist): -    for k in range(N): -        for i in range(N): -            for j in range(N): -                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) - - -def is_bipartite(u): -    for v in range(N): -        if u == v or dist[u][v] != 1: -            continue -        if color[v] == -1: -            color[v] = 1 - color[u] -            if not is_bipartite(v): -                return False -        elif color[u] == color[v]: -            return False -    return True +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) -S = [input().rstrip() for _ in range(N)] -dist = [ -    [1 if S[i][j] == "1" else (0 if i == j else float("inf")) for j in range(N)] -    for i in range(N) -] -color = [-1] * N -color[0] = 0 -ans = 0 -if is_bipartite(0): -    floyd_warshall(dist) -    ans = max(max(d) for d in dist) + 1 -else: -    ans = -1 -print(ans) +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))