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
}

Heuristics = lambda{
	return 0...N if N < 100
	ds0 = Ds[0]
	mx0 = ds0.each_with_index.max
	dsA = Ds[mx0[1]]
	mxA = dsA.each_with_index.max
	dsB = mxA[1] == 0 ? ds0 : Ds[mxA[1]]
	siA, siB = [dsA, dsB].map{|ds| (0...N).sort_by{|i| ds[i] } }
	return siA[-N/5, N/5]|siB[-N/5, N/5]|(siA[N/2-N/10, N/5]&siB[N/2-N/10, N/5])
}

begin
	p Heuristics.call.map{|i| Ds[i].max }.max
rescue RuntimeError
	p(-1)
end
