from collections import deque

N = int(eval(input()))

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





def bfs(i):



    d = [-1 for _ in range(N)]

    d[i] = 0



    que = deque([(i, 0, -1)])



    finished = set()

    while que:

        ci, dist, p = que.popleft()

        # print(ci, dist, p)



        for j in range(N):

            if S[ci][j] == '1':

                if j != p and d[j] >= 0:

                    if d[j] == dist or (d[j] != dist+1 and j not in finished):

                        # print(ci, j)

                        return -1

                elif d[j] == -1:

                    d[j] = dist+1

                    que.append((j, dist+1, ci))

        finished.add(ci)



    return dist+1





ans = -1

for i in range(N):

    b = bfs(i)

    ans = max(ans, b)



print(ans)

from collections import deque

N = int(eval(input()))



G = {k: [] for k in range(N)}

for i in range(N):

    S = eval(input())

    for j in range(N):

        if S[j] == '1':

            G[i].append(j)

            G[j].append(i)





def bfs(i):

    visited = [-1 for _ in range(N)]

    visited[i] = 1

    M = 1

    que = deque([(i, -1)])



    while que:

        ci, p = que.popleft()



        for ni in G[ci]:

            if ni != p:

                if visited[ni] != -1:

                    if (visited[ci]+1-visited[ni]) % 2 == 1:

                        print((-1))

                        exit()

                else:

                    que.append((ni, ci))

                    visited[ni] = visited[ci]+1

                    M = max(M, visited[ni])



    return M





ans = 0

for i in range(N):   # start node

    ans = max(ans, bfs(i))

print(ans)

from collections import deque

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


def bfs(i):
    d = [-1 for _ in range(N)]
    d[i] = 0
    que = deque([(i, 0, -1)])
    finished = set()
    while que:
        ci, dist, p = que.popleft()
        # print(ci, dist, p)
        for j in range(N):
            if S[ci][j] == "1":
                if j != p and d[j] >= 0:
                    if d[j] == dist or (d[j] != dist + 1 and j not in finished):
                        # print(ci, j)
                        return -1
                elif d[j] == -1:
                    d[j] = dist + 1
                    que.append((j, dist + 1, ci))
        finished.add(ci)
    return dist + 1


ans = -1
for i in range(N):
    b = bfs(i)
    ans = max(ans, b)
print(ans)

from collections import deque

N = int(eval(input()))
G = {k: [] for k in range(N)}
for i in range(N):
    S = eval(input())
    for j in range(N):
        if S[j] == "1":
            G[i].append(j)
            G[j].append(i)


def bfs(i):
    visited = [-1 for _ in range(N)]
    visited[i] = 1
    M = 1
    que = deque([(i, -1)])
    while que:
        ci, p = que.popleft()
        for ni in G[ci]:
            if ni != p:
                if visited[ni] != -1:
                    if (visited[ci] + 1 - visited[ni]) % 2 == 1:
                        print((-1))
                        exit()
                else:
                    que.append((ni, ci))
                    visited[ni] = visited[ci] + 1
                    M = max(M, visited[ni])
    return M


ans = 0
for i in range(N):  # start node
    ans = max(ans, bfs(i))
print(ans)

-S = [list(eval(input())) for _ in range(N)] +G = {k: [] for k in range(N)} +for i in range(N): +    S = eval(input()) +    for j in range(N): +        if S[j] == "1": +            G[i].append(j) +            G[j].append(i) -    d = [-1 for _ in range(N)] -    d[i] = 0 -    que = deque([(i, 0, -1)]) -    finished = set() +    visited = [-1 for _ in range(N)] +    visited[i] = 1 +    M = 1 +    que = deque([(i, -1)]) -        ci, dist, p = que.popleft() -        # print(ci, dist, p) -        for j in range(N): -            if S[ci][j] == "1": -                if j != p and d[j] >= 0: -                    if d[j] == dist or (d[j] != dist + 1 and j not in finished): -                        # print(ci, j) -                        return -1 -                elif d[j] == -1: -                    d[j] = dist + 1 -                    que.append((j, dist + 1, ci)) -        finished.add(ci) -    return dist + 1 +        ci, p = que.popleft() +        for ni in G[ci]: +            if ni != p: +                if visited[ni] != -1: +                    if (visited[ci] + 1 - visited[ni]) % 2 == 1: +                        print((-1)) +                        exit() +                else: +                    que.append((ni, ci)) +                    visited[ni] = visited[ci] + 1 +                    M = max(M, visited[ni]) +    return M -ans = -1 -for i in range(N): -    b = bfs(i) -    ans = max(ans, b) +ans = 0 +for i in range(N):  # start node +    ans = max(ans, bfs(i))