N = gets.to_i
s = {}
(0..(N-1)).each do |ind|
    array=gets.chomp.split("").map(&:to_i)
    s[ind] = array.map.with_index { |e,i| e ==1 ? i : nil }.compact
end
ret = Array.new(N)
queue = [0]
even_odd = 0
while !queue.empty? do
    queue.each do |i|
        ret[i] = even_odd
        if !s[i].map {|ind| ret[ind] }.select {|e| e!=nil && e==even_odd }.empty?
            puts "-1"
            exit
        end
    end
    queue = queue.map {|e| s[e] }.flatten.uniq
    ret.each_with_index do |e,i|
        queue.delete(i) if e != nil
    end
    even_odd = (even_odd + 1) % 2
end
puts 2