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

T = S.map{|si|
	(0...N).select{|j| si[j] == ?1 }
}
IandD = lambda{|dbase|
	i, d, ds, q = nil, 0, [nil]*N, [nil]*N
	ds[dbase] = 1
	q.clear; q << dbase
	until q.empty?
		d += 1
		q.size.times{
			T[i=q.shift].each{|j|
				case ds[j]
				when nil
					ds[j] = d+1
					q << j
				when d+1
				when d-1
				else
					raise
				end
			}
		}
	end
	return i, d
}

begin
	i,* = IandD[0]
	*,d = IandD[i]
	p d
rescue RuntimeError
	p(-1)
end
