N = gets.to_i
S = N.times.map{
	gets.to_i(2)
}

T = S.map{|si|
	(0...N).select{|j| (si>>(N-1-j))&1 == 1 }
}
Ds = lambda{|dbase|
	ds, d, q = [nil]*N, 1, [nil]*N
	ds[dbase] = d
	q.clear; q << dbase
	until q.empty?
		q.size.times{
			T[q.shift].each{|j|
				case ds[j]
				when nil
					ds[j] = d+1
					q << j
				when d+1
				when d-1
				else
					raise
				end
			}
		}
		d += 1
	end
	return ds
}
DsMax = lambda{|dbase|
	d, p, q = 0, 0, 1<<(N-1-dbase)
	until p == (1<<N)-1
		d, p, q, r = d+1, p|q, 0, (p^q)&q
		# assert(r != 0)
		until 0 == (s = r&(-r))
			q |= S[N-s.bit_length]
			r -= s
		end
	end
	return d
}
begin
	p [Ds[0].max, (1...N).map{|i| DsMax[i] }.max].max
rescue RuntimeError
	p(-1)
end
