N = gets.to_i
#s = []

node = {}

N.times { |i|
	temp = []
	s = gets.chomp.split("").map(&:to_i)
	
	s.each_with_index{ |x, j|
		if x == 1 then
			temp << j
		end
	}

	node[i] = temp
}

tail = node.select{ |k, v|
	v.size == 1
}

if tail.size == 0 then
	puts -1
	exit
end

lengthes = []

tail.keys.each{ |key|
	group = {key => true}
	start = [key]
	length = 1
	
	is_valid = true

	while group.keys.size != N
		length += 1
		next_start = {}
		start.each{ |s|
			node[s].each{ |x|
				if next_start[s] then
					is_valid = false
				else
					next_start[x] = true
				end
			}
		}
		
		if ! is_valid then
			length = -1
			break
		end
		
		group.merge!(next_start)
		
		start = next_start.keys
	end

	lengthes << length
}

puts lengthes.max
