n = gets.to_i
start = [0]
max_path_count = 0
graph = []
n.times do |i|
  m = gets.chomp.split(//).map(&:to_i)
  neighbors = []
  m.each_with_index { |path_exist, v| neighbors << v if path_exist == 1}

  if neighbors.size > max_path_count
    start = [i]
  end

  if neighbors.size == max_path_count
    start << i
  end
  graph << neighbors
end

def bfs(n, graph, start)
  rank = Array.new(n, nil)
  rank[start] = 1
  queue = [start]
  visited = {}
  until queue.empty?
    v = queue.shift
    visited[v] = true
    graph[v].each do |nv|
      if rank[nv].nil?
        rank[nv] = rank[v] + 1
      else
        if (rank[nv] - rank[v]).abs != 1
          return -1
        end
      end

      queue << nv unless visited[nv]
    end
  end

  rank.max
end

ans = -1
start.each do |i|
  ans = [ans, bfs(n, graph, i)].max
end
puts ans