ma = lambda :map(int,input().split())

lma = lambda :list(map(int,input().split()))

tma = lambda :tuple(map(int,input().split()))

ni = lambda:int(input())

yn = lambda fl:print("Yes") if fl else print("No")

import collections

import math

import itertools

import heapq as hq



n = ni()

g = [[] for i in range(n)]

for i in range(n):

    S =input()

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

        if S[j]=="1":

            g[i].append(j)

            g[j].append(i)

visited=[False]*n

def bfs(s):

    que = collections.deque()

    que.append(s)

    visited[s]=True

    ls=[s]

    while que:

        s = que.popleft()

        for node in g[s]:

            if not visited[node]:

                visited[node]=True

                que.append(node)

                ls.append(node)

    return ls

def bfs2(s):

    vis = [False]*n

    cnts=[0]*n

    que = collections.deque()

    cnt=1

    que.append((s,cnt))

    vis[s]=True

    cnts[s]=1

    while que:

        s,cnt = que.popleft()

        for node in g[s]:

            if not vis[node]:

                vis[node]=True

                cnts[node]=cnt+1

                que.append((node,cnt+1))

            else:

                if (cnts[node]+cnt)%2==0:

                    print(-1)

                    exit()

    return cnt

#print(g)

ans = 0

for i in range(n):

    if not visited[i]:

        ls = bfs(i)

        #print(ans,ls)

        tmp = 0

        for node in ls:

            tmp = max(bfs2(node),tmp)

        ans+=tmp

print(ans)

import collections



n = int(eval(input()))

g = [[] for i in range(n)]

for i in range(n):

    S =eval(input())

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

        if S[j]=="1":

            g[i].append(j)

            g[j].append(i)

def bfs(s):

    vis = [False]*n

    cnts=[0]*n

    que = collections.deque()

    cnt=1

    que.append((s,cnt))

    vis[s]=True

    cnts[s]=1

    while que:

        s,cnt = que.popleft()

        for node in g[s]:

            if not vis[node]:

                vis[node]=True

                cnts[node]=cnt+1

                que.append((node,cnt+1))

            else:

                if (cnts[node]+cnt)%2==0:

                    print((-1))

                    exit()

    return cnt

ans = 0

for node in range(n):

    ans = max(bfs(node),ans)

print(ans)

ma = lambda: map(int, input().split())
lma = lambda: list(map(int, input().split()))
tma = lambda: tuple(map(int, input().split()))
ni = lambda: int(input())
yn = lambda fl: print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

n = ni()
g = [[] for i in range(n)]
for i in range(n):
    S = input()
    for j in range(i + 1, n):
        if S[j] == "1":
            g[i].append(j)
            g[j].append(i)
visited = [False] * n


def bfs(s):
    que = collections.deque()
    que.append(s)
    visited[s] = True
    ls = [s]
    while que:
        s = que.popleft()
        for node in g[s]:
            if not visited[node]:
                visited[node] = True
                que.append(node)
                ls.append(node)
    return ls


def bfs2(s):
    vis = [False] * n
    cnts = [0] * n
    que = collections.deque()
    cnt = 1
    que.append((s, cnt))
    vis[s] = True
    cnts[s] = 1
    while que:
        s, cnt = que.popleft()
        for node in g[s]:
            if not vis[node]:
                vis[node] = True
                cnts[node] = cnt + 1
                que.append((node, cnt + 1))
            else:
                if (cnts[node] + cnt) % 2 == 0:
                    print(-1)
                    exit()
    return cnt


# print(g)
ans = 0
for i in range(n):
    if not visited[i]:
        ls = bfs(i)
        # print(ans,ls)
        tmp = 0
        for node in ls:
            tmp = max(bfs2(node), tmp)
        ans += tmp
print(ans)

import collections

n = int(eval(input()))
g = [[] for i in range(n)]
for i in range(n):
    S = eval(input())
    for j in range(i + 1, n):
        if S[j] == "1":
            g[i].append(j)
            g[j].append(i)


def bfs(s):
    vis = [False] * n
    cnts = [0] * n
    que = collections.deque()
    cnt = 1
    que.append((s, cnt))
    vis[s] = True
    cnts[s] = 1
    while que:
        s, cnt = que.popleft()
        for node in g[s]:
            if not vis[node]:
                vis[node] = True
                cnts[node] = cnt + 1
                que.append((node, cnt + 1))
            else:
                if (cnts[node] + cnt) % 2 == 0:
                    print((-1))
                    exit()
    return cnt


ans = 0
for node in range(n):
    ans = max(bfs(node), ans)
print(ans)

-ma = lambda: map(int, input().split()) -lma = lambda: list(map(int, input().split())) -tma = lambda: tuple(map(int, input().split())) -ni = lambda: int(input()) -yn = lambda fl: print("Yes") if fl else print("No") -import math -import itertools -import heapq as hq -n = ni() +n = int(eval(input())) -    S = input() +    S = eval(input()) -visited = [False] * n -    que = collections.deque() -    que.append(s) -    visited[s] = True -    ls = [s] -    while que: -        s = que.popleft() -        for node in g[s]: -            if not visited[node]: -                visited[node] = True -                que.append(node) -                ls.append(node) -    return ls - - -def bfs2(s): -                    print(-1) +                    print((-1)) -# print(g) -for i in range(n): -    if not visited[i]: -        ls = bfs(i) -        # print(ans,ls) -        tmp = 0 -        for node in ls: -            tmp = max(bfs2(node), tmp) -        ans += tmp +for node in range(n): +    ans = max(bfs(node), ans)