N=int(eval(input()))



graph=[]

for i in range(N):

  graph.append([])



for i in range(N):

  S=list(map(int,eval(input())))

  for j in range(N):

    if S[j]==1:

      graph[i].append(j)

      

#print(graph)

max_set=-1

for i in range(N):

  node_list=[-1]*N

  #bfs

  d=1

  queue={i}

  illegal=False

  while(True):

    new_queue=set()

    for q in queue:

      node_list[q]=d

      for qq in graph[q]:

        if node_list[qq]==-1:

          new_queue.add(qq)

        elif node_list[qq]!=d-1:

          illegal=True

          break

      

    if illegal:

      break

    elif len(new_queue)==0:

      #print(node_list,d)

      max_set=max(max_set,d)

      break

    else:

      #print(node_list,new_queue)

      queue=new_queue

      d+=1

      

print(max_set)

N=int(eval(input()))



graph=[]

for i in range(N):

  graph.append([])



for i in range(N):

  S=list(map(int,eval(input())))

  for j in range(N):

    if S[j]==1:

      graph[i].append(j)

      

#print(graph)

max_set=-1

illegal=False

for i in range(N):

  if illegal:

    break

  

  node_list=[-1]*N

  #bfs

  d=1

  queue={i}

  while(True):

    new_queue=set()

    for q in queue:

      node_list[q]=d

      for qq in graph[q]:

        if node_list[qq]==-1:

          new_queue.add(qq)

        elif node_list[qq]!=d-1:

          illegal=True

          break

      

    if illegal:

      break

    elif len(new_queue)==0:

      #print(node_list,d)

      max_set=max(max_set,d)

      break

    else:

      #print(node_list,new_queue)

      queue=new_queue

      d+=1

      

print(max_set)

N = int(eval(input()))
graph = []
for i in range(N):
    graph.append([])
for i in range(N):
    S = list(map(int, eval(input())))
    for j in range(N):
        if S[j] == 1:
            graph[i].append(j)
# print(graph)
max_set = -1
for i in range(N):
    node_list = [-1] * N
    # bfs
    d = 1
    queue = {i}
    illegal = False
    while True:
        new_queue = set()
        for q in queue:
            node_list[q] = d
            for qq in graph[q]:
                if node_list[qq] == -1:
                    new_queue.add(qq)
                elif node_list[qq] != d - 1:
                    illegal = True
                    break
        if illegal:
            break
        elif len(new_queue) == 0:
            # print(node_list,d)
            max_set = max(max_set, d)
            break
        else:
            # print(node_list,new_queue)
            queue = new_queue
            d += 1
print(max_set)

N = int(eval(input()))
graph = []
for i in range(N):
    graph.append([])
for i in range(N):
    S = list(map(int, eval(input())))
    for j in range(N):
        if S[j] == 1:
            graph[i].append(j)
# print(graph)
max_set = -1
illegal = False
for i in range(N):
    if illegal:
        break
    node_list = [-1] * N
    # bfs
    d = 1
    queue = {i}
    while True:
        new_queue = set()
        for q in queue:
            node_list[q] = d
            for qq in graph[q]:
                if node_list[qq] == -1:
                    new_queue.add(qq)
                elif node_list[qq] != d - 1:
                    illegal = True
                    break
        if illegal:
            break
        elif len(new_queue) == 0:
            # print(node_list,d)
            max_set = max(max_set, d)
            break
        else:
            # print(node_list,new_queue)
            queue = new_queue
            d += 1
print(max_set)

+illegal = False +    if illegal: +        break -    illegal = False