N = gets.to_i
S = N.times.map{
	gets.strip.chars.map{|_| _=='1' }
}

Ds = lambda{|dbase|
	ds, d, q = [nil]*N, 1, {dbase=>true}
	ds[dbase] = d
	until q.empty?
		r = {}
		q.keys.each{|i|
			(0...N).each{|j|
				next unless S[i][j]
				if ds[j].nil?
					ds[j] = d+1
					r[j] = true
				elsif ds[j] == d+1
				elsif ds[j] == d-1
				else
					raise
				end
			}
		}
		q, d = r, d+1
	end
	return ds
}
begin
	ds = Ds[0]
	max = ds.max
	p ([max] + (1...N).select{|i| ds[i] == max }.map{|i| Ds[i].max }).max
rescue RuntimeError
	p(-1)
end
