N = gets.to_i
S = N.times.map{
	gets.strip
}

T = S.map{|si|
	(0...N).select{|j| si[j]==?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
}
begin
	p (0...N).map{|i| Ds[i].max }.max
rescue RuntimeError
	p(-1)
end
