from collections import deque

n = int(eval(input()))

s = [eval(input()) for i in range(n)]

ans = -2

for k in range(n):

    q = deque([k])

    d = [float('inf')] * n

    d[k] = 0

    while q:

        flg = 0

        p = q.popleft()

        for i, c in enumerate(s[p]):

            if c == '1':

                if d[i] == float('inf'):

                    d[i] = d[p] + 1

                    q.append(i)

                elif d[i] != d[p] + 1 and d[i] != d[p] - 1:

                    flg = 1

                    break

        if flg:

            break

    if flg:

        continue

    for i in range(n):

        ans = max(ans, d[i])

print((ans + 1))

def warshall_floyd(v_count: int, matrix: list) -> list:

    """ ワーシャルフロイド

    v_count: 頂点数

    matrix: 隣接行列(到達不能はfloat("inf"))

    """

    for i in range(v_count):

        for j, c2 in enumerate(row[i] for row in matrix):

            for k, (c1, c3) in enumerate(zip(matrix[j], matrix[i])):

                if c1 > c2+c3:

                    matrix[j][k] = c2+c3

    return matrix





n = int(eval(input()))

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

m = [list(map(int, eval(input()))) for _ in range(n)]

for i, row in enumerate(m):

    for j, c in enumerate(row):

        if c == 1:

            g[i].append(j)

        elif i != j:

            m[i][j] = 10000000000

d = [-1] * n

d[0] = 0

s = [0]

while s:

    p = s.pop()

    for node in g[p]:

        if d[node] == -1:

            s.append(node)

            d[node] = d[p] ^ 1

        elif d[node] == d[p]:

            print((-1))

            exit(0)

ans = 0

warshall_floyd(n, m)

for row in m:

    ans = max(ans, max(row))

print((ans + 1))

from collections import deque

n = int(eval(input()))
s = [eval(input()) for i in range(n)]
ans = -2
for k in range(n):
    q = deque([k])
    d = [float("inf")] * n
    d[k] = 0
    while q:
        flg = 0
        p = q.popleft()
        for i, c in enumerate(s[p]):
            if c == "1":
                if d[i] == float("inf"):
                    d[i] = d[p] + 1
                    q.append(i)
                elif d[i] != d[p] + 1 and d[i] != d[p] - 1:
                    flg = 1
                    break
        if flg:
            break
    if flg:
        continue
    for i in range(n):
        ans = max(ans, d[i])
print((ans + 1))

def warshall_floyd(v_count: int, matrix: list) -> list:
    """ワーシャルフロイド
    v_count: 頂点数
    matrix: 隣接行列(到達不能はfloat("inf"))
    """
    for i in range(v_count):
        for j, c2 in enumerate(row[i] for row in matrix):
            for k, (c1, c3) in enumerate(zip(matrix[j], matrix[i])):
                if c1 > c2 + c3:
                    matrix[j][k] = c2 + c3
    return matrix


n = int(eval(input()))
g = [[] for _ in range(n)]
m = [list(map(int, eval(input()))) for _ in range(n)]
for i, row in enumerate(m):
    for j, c in enumerate(row):
        if c == 1:
            g[i].append(j)
        elif i != j:
            m[i][j] = 10000000000
d = [-1] * n
d[0] = 0
s = [0]
while s:
    p = s.pop()
    for node in g[p]:
        if d[node] == -1:
            s.append(node)
            d[node] = d[p] ^ 1
        elif d[node] == d[p]:
            print((-1))
            exit(0)
ans = 0
warshall_floyd(n, m)
for row in m:
    ans = max(ans, max(row))
print((ans + 1))

-from collections import deque +def warshall_floyd(v_count: int, matrix: list) -> list: +    """ワーシャルフロイド +    v_count: 頂点数 +    matrix: 隣接行列(到達不能はfloat("inf")) +    """ +    for i in range(v_count): +        for j, c2 in enumerate(row[i] for row in matrix): +            for k, (c1, c3) in enumerate(zip(matrix[j], matrix[i])): +                if c1 > c2 + c3: +                    matrix[j][k] = c2 + c3 +    return matrix + -s = [eval(input()) for i in range(n)] -ans = -2 -for k in range(n): -    q = deque([k]) -    d = [float("inf")] * n -    d[k] = 0 -    while q: -        flg = 0 -        p = q.popleft() -        for i, c in enumerate(s[p]): -            if c == "1": -                if d[i] == float("inf"): -                    d[i] = d[p] + 1 -                    q.append(i) -                elif d[i] != d[p] + 1 and d[i] != d[p] - 1: -                    flg = 1 -                    break -        if flg: -            break -    if flg: -        continue -    for i in range(n): -        ans = max(ans, d[i]) +g = [[] for _ in range(n)] +m = [list(map(int, eval(input()))) for _ in range(n)] +for i, row in enumerate(m): +    for j, c in enumerate(row): +        if c == 1: +            g[i].append(j) +        elif i != j: +            m[i][j] = 10000000000 +d = [-1] * n +d[0] = 0 +s = [0] +while s: +    p = s.pop() +    for node in g[p]: +        if d[node] == -1: +            s.append(node) +            d[node] = d[p] ^ 1 +        elif d[node] == d[p]: +            print((-1)) +            exit(0) +ans = 0 +warshall_floyd(n, m) +for row in m: +    ans = max(ans, max(row))