n = gets.to_i
s = n.times.map { gets.chomp.chars.map(&:to_i) }

has_ans = false
ans = -1

n.times do |start|
  v = [start]
  remain = [*0..n - 1] - [start]
  count = 1
  n.times do
    if remain.empty?
      ans = count if ans < count
      has_ans = true
      break
    end

    tmp = []
    v.each do |t|
      remain.each do |i|
        tmp |= [i] if s[t][i] == 1
      end
    end

    if !tmp.empty? && tmp.combination(2).all? { |i, j| !s[i][j].zero? }
      v = tmp
      remain -= v
      count += 1
    else
      break
    end
  end
end
if has_ans
  puts ans
else
  puts '-1'
end