n = gets.to_i
start = 0
max_zero_count = 0
graph = []
n.times do |i|
  m = gets.chomp.split(//).map(&:to_i)
  zero_count = m.count(0)
  if zero_count > max_zero_count
    start = i
    max_zero_count = zero_count
  end
  graph << m
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_with_index do |exist, nv|
      next if exist == 0
      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

puts bfs(n, graph, start)
