

N = int(eval(input()))

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



import sys

import copy



ans = 0

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

for i in range(N):

    for j in range(i+1,N):

        if int(S[i][j]):

            adj_list[i].append(j)

            adj_list[j].append(i)





for kk in range(N):

    q = []

    visited = [False] * N

    cnt = 1

    visited[kk] = True

    q.append(kk)

    VC = 1

    while VC < N:

        nextv = copy.copy(q)

        q = []

        nextv = list(set(nextv))

        #子の要素同士の連結チェック

        L = len(nextv)

        for i in range(L):

            if not(visited[nextv[i]]):

                visited[nextv[i]] = True

                VC += 1

            for j in range(i+1,L):

                if (int(S[nextv[i]][nextv[j]])):

                    print((-1))

                    sys.exit()

        

        for nv in nextv:

            for v in adj_list[nv]:

                if not(visited[v]):

                    q.append(v)

    

        cnt += 1

    

    ans = max(ans,cnt-1)

        

#

print(ans)

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)

N = int(eval(input()))
S = [eval(input()) for _ in range(N)]
import sys
import copy

ans = 0
adj_list = [[] for _ in range(N)]
for i in range(N):
    for j in range(i + 1, N):
        if int(S[i][j]):
            adj_list[i].append(j)
            adj_list[j].append(i)
for kk in range(N):
    q = []
    visited = [False] * N
    cnt = 1
    visited[kk] = True
    q.append(kk)
    VC = 1
    while VC < N:
        nextv = copy.copy(q)
        q = []
        nextv = list(set(nextv))
        # 子の要素同士の連結チェック
        L = len(nextv)
        for i in range(L):
            if not (visited[nextv[i]]):
                visited[nextv[i]] = True
                VC += 1
            for j in range(i + 1, L):
                if int(S[nextv[i]][nextv[j]]):
                    print((-1))
                    sys.exit()
        for nv in nextv:
            for v in adj_list[nv]:
                if not (visited[v]):
                    q.append(v)
        cnt += 1
    ans = max(ans, cnt - 1)
#
print(ans)

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)

-N = int(eval(input())) -S = [eval(input()) for _ in range(N)] -import copy +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) -adj_list = [[] for _ in range(N)] +import queue + -    for j in range(i + 1, N): -        if int(S[i][j]): -            adj_list[i].append(j) -            adj_list[j].append(i) -for kk in range(N): -    q = [] -    visited = [False] * N -    cnt = 1 -    visited[kk] = True -    q.append(kk) -    VC = 1 -    while VC < N: -        nextv = copy.copy(q) -        q = [] -        nextv = list(set(nextv)) -        # 子の要素同士の連結チェック -        L = len(nextv) -        for i in range(L): -            if not (visited[nextv[i]]): -                visited[nextv[i]] = True -                VC += 1 -            for j in range(i + 1, L): -                if int(S[nextv[i]][nextv[j]]): -                    print((-1)) -                    sys.exit() -        for nv in nextv: -            for v in adj_list[nv]: -                if not (visited[v]): -                    q.append(v) -        cnt += 1 -    ans = max(ans, cnt - 1) -# -print(ans) +    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)