import sys

sys.setrecursionlimit(10**8)

input = sys.stdin.readline



N = int(input())

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



edge = [[] for _ in range(N)]

for i in range(N):

    for j in range(N):

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

            edge[i].append(j)



ans = 0

import queue

for i in range(N):

    q = queue.Queue()

    q.put(i)

    nums = [-1]*N

    nums[i] = 1

    while not q.empty():

        v = q.get()

        flag = True

        for nv in edge[v]:

            if nums[nv] == -1:

                nums[nv] = nums[v] + 1

                q.put(nv)

            elif nums[nv]%2 != (nums[v]+1)%2:

                flag = False

                break

        if not flag:

            break

    if flag:

        ans = max(ans, max(nums))

print(ans) if ans != 0 else print(-1)



import sys

sys.setrecursionlimit(10**8)

input = sys.stdin.readline



N = int(eval(input()))

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

INF = 10**12

dist = [[INF]*N for _ in range(N)]

edge = [[] for _ in range(N)]

for i in range(N):

    for j in range(N):

        if i==j:

            dist[i][j] = 0

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

            edge[i].append(j)

            dist[i][j] = 1



def WarshallFloyd(d, N):

    #d[i][j]: iからjへの最短距離（隣接行列）

    for k in range(N):

        for i in range(N):

            for j in range(N):

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

    return d



d = WarshallFloyd(dist, N)

def dfs(v, c):

    colors[v] = c

    for nv in edge[v]:

        if colors[nv] == c:

            return False

        if colors[nv] == -1:

            if not dfs(nv, c^1):

                return False

    return True



colors = [-1]*N

if not dfs(0,0):

    print((-1))

    exit()



ans = 0

for e in d:

    ans = max(ans, max(e))

print((ans+1))





import sys

sys.setrecursionlimit(10**8)
input = sys.stdin.readline
N = int(input())
S = [list(input()) for _ in range(N)]
edge = [[] for _ in range(N)]
for i in range(N):
    for j in range(N):
        if S[i][j] == "1":
            edge[i].append(j)
ans = 0
import queue

for i in range(N):
    q = queue.Queue()
    q.put(i)
    nums = [-1] * N
    nums[i] = 1
    while not q.empty():
        v = q.get()
        flag = True
        for nv in edge[v]:
            if nums[nv] == -1:
                nums[nv] = nums[v] + 1
                q.put(nv)
            elif nums[nv] % 2 != (nums[v] + 1) % 2:
                flag = False
                break
        if not flag:
            break
    if flag:
        ans = max(ans, max(nums))
print(ans) if ans != 0 else print(-1)

import sys

sys.setrecursionlimit(10**8)
input = sys.stdin.readline
N = int(eval(input()))
S = [list(eval(input())) for _ in range(N)]
INF = 10**12
dist = [[INF] * N for _ in range(N)]
edge = [[] for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            dist[i][j] = 0
        if S[i][j] == "1":
            edge[i].append(j)
            dist[i][j] = 1


def WarshallFloyd(d, N):
    # d[i][j]: iからjへの最短距離（隣接行列）
    for k in range(N):
        for i in range(N):
            for j in range(N):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


d = WarshallFloyd(dist, N)


def dfs(v, c):
    colors[v] = c
    for nv in edge[v]:
        if colors[nv] == c:
            return False
        if colors[nv] == -1:
            if not dfs(nv, c ^ 1):
                return False
    return True


colors = [-1] * N
if not dfs(0, 0):
    print((-1))
    exit()
ans = 0
for e in d:
    ans = max(ans, max(e))
print((ans + 1))

-N = int(input()) -S = [list(input()) for _ in range(N)] +N = int(eval(input())) +S = [list(eval(input())) for _ in range(N)] +INF = 10**12 +dist = [[INF] * N for _ in range(N)] +        if i == j: +            dist[i][j] = 0 +            dist[i][j] = 1 + + +def WarshallFloyd(d, N): +    # d[i][j]: iからjへの最短距離（隣接行列） +    for k in range(N): +        for i in range(N): +            for j in range(N): +                d[i][j] = min(d[i][j], d[i][k] + d[k][j]) +    return d + + +d = WarshallFloyd(dist, N) + + +def dfs(v, c): +    colors[v] = c +    for nv in edge[v]: +        if colors[nv] == c: +            return False +        if colors[nv] == -1: +            if not dfs(nv, c ^ 1): +                return False +    return True + + +colors = [-1] * N +if not dfs(0, 0): +    print((-1)) +    exit() -import queue - -for i in range(N): -    q = queue.Queue() -    q.put(i) -    nums = [-1] * N -    nums[i] = 1 -    while not q.empty(): -        v = q.get() -        flag = True -        for nv in edge[v]: -            if nums[nv] == -1: -                nums[nv] = nums[v] + 1 -                q.put(nv) -            elif nums[nv] % 2 != (nums[v] + 1) % 2: -                flag = False -                break -        if not flag: -            break -    if flag: -        ans = max(ans, max(nums)) -print(ans) if ans != 0 else print(-1) +for e in d: +    ans = max(ans, max(e)) +print((ans + 1))