from collections import deque

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**7)

n = int(eval(input()))

edges = [[]for _ in range(n)]

for i in range(n):

    s = list(map(int, list(input().rstrip())))

    for j in range(n):

        if s[j]:

            edges[i].append(j)



            

def dfs(v):

    for v2 in edges[v]:

        if color[v2] != -1:

            if color[v] == color[v2]:

                global is_ok

                is_ok = 0

            continue

        color[v2] = color[v] ^ 1

        dfs(v2)





color = [-1]*n

color[0] = 0

is_ok = 1

dfs(0)





def bfs(init_v):

    next_v = deque([init_v])

    INF = 10**18

    dist = [INF]*n

    dist[init_v] = 0

    while next_v:

        v = next_v.popleft()

        for v2 in edges[v]:

            if dist[v2] <= dist[v]+1:

                continue

            dist[v2] = dist[v]+1

            next_v.append(v2)

    return dist





if is_ok:

    ans = 0

    for i in range(n):

        dist = bfs(i)

        x = max(dist)

        if ans < x:

            ans = x

    print((ans+1))

else:

    print((-1))
from collections import deque

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**7)

n = int(eval(input()))

edges = [[]for _ in range(n)]

for i in range(n):

    s = list(map(int, list(input().rstrip())))

    for j in range(n):

        if s[j]:

            edges[i].append(j)





def dfs(v, c=0):

    color[v] = c

    for v2 in edges[v]:

        if color[v2] != -1:

            if color[v] == color[v2]:

                global is_ok

                is_ok = 0

            continue

        dfs(v2, color[v] ^ 1)





color = [-1]*n

is_ok = 1

dfs(0)





def bfs(init_v):

    next_v = deque([init_v])

    INF = 10**18

    dist = [INF]*n

    dist[init_v] = 0

    while next_v:

        v = next_v.popleft()

        for v2 in edges[v]:

            if dist[v2] <= dist[v]+1:

                continue

            dist[v2] = dist[v]+1

            next_v.append(v2)

    return dist



if is_ok:

    ans = 0

    for i in range(n):

        dist = bfs(i)

        x = max(dist)

        if ans < x:

            ans = x

    print((ans+1))

else:

    print((-1))
from collections import deque
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n = int(eval(input()))
edges = [[] for _ in range(n)]
for i in range(n):
    s = list(map(int, list(input().rstrip())))
    for j in range(n):
        if s[j]:
            edges[i].append(j)


def dfs(v):
    for v2 in edges[v]:
        if color[v2] != -1:
            if color[v] == color[v2]:
                global is_ok
                is_ok = 0
            continue
        color[v2] = color[v] ^ 1
        dfs(v2)


color = [-1] * n
color[0] = 0
is_ok = 1
dfs(0)


def bfs(init_v):
    next_v = deque([init_v])
    INF = 10**18
    dist = [INF] * n
    dist[init_v] = 0
    while next_v:
        v = next_v.popleft()
        for v2 in edges[v]:
            if dist[v2] <= dist[v] + 1:
                continue
            dist[v2] = dist[v] + 1
            next_v.append(v2)
    return dist


if is_ok:
    ans = 0
    for i in range(n):
        dist = bfs(i)
        x = max(dist)
        if ans < x:
            ans = x
    print((ans + 1))
else:
    print((-1))

from collections import deque
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n = int(eval(input()))
edges = [[] for _ in range(n)]
for i in range(n):
    s = list(map(int, list(input().rstrip())))
    for j in range(n):
        if s[j]:
            edges[i].append(j)


def dfs(v, c=0):
    color[v] = c
    for v2 in edges[v]:
        if color[v2] != -1:
            if color[v] == color[v2]:
                global is_ok
                is_ok = 0
            continue
        dfs(v2, color[v] ^ 1)


color = [-1] * n
is_ok = 1
dfs(0)


def bfs(init_v):
    next_v = deque([init_v])
    INF = 10**18
    dist = [INF] * n
    dist[init_v] = 0
    while next_v:
        v = next_v.popleft()
        for v2 in edges[v]:
            if dist[v2] <= dist[v] + 1:
                continue
            dist[v2] = dist[v] + 1
            next_v.append(v2)
    return dist


if is_ok:
    ans = 0
    for i in range(n):
        dist = bfs(i)
        x = max(dist)
        if ans < x:
            ans = x
    print((ans + 1))
else:
    print((-1))

-def dfs(v): +def dfs(v, c=0): +    color[v] = c -        color[v2] = color[v] ^ 1 -        dfs(v2) +        dfs(v2, color[v] ^ 1) -color[0] = 0