require 'set'
N = gets.to_i
S = N.times.map{
	gets.strip.each_char.map{|c| c==?1 }
}

I = lambda{|i,j|
	return i < j ? (N-i-1)*(N-i-2)/2+(j-i-1) : (N-j-1)*(N-j-2)/2+(i-j-1)
}
Ds = [nil]*(N*(N-1)/2) # I[i,j]th element is for dist between i&j
U = Set.new 1...N # unreached
V = Set.new [0]   #   reached
until U.empty?
	i = U.each{|i| break V.each{|j| break i if S[i][j] } }
	U.delete(i); V << i

	uq = []
	V.each{|j|
		next unless S[i][j]
		uq << [i,j,2]
	}
	until uq.empty?
		x, y, d = uq.shift
		di = I[x,y]
		d0 = Ds[di]
		if d0
			if d0&1 != d&1
				p(-1)
				exit
			end
			next if d0 <= d
		end
		Ds[di] = d

		V.each{|z|
			uq << [z,x,d+1] if z != x && S[z][y]
			uq << [z,y,d+1] if z != y && S[z][x]
		}
	end
end

p Ds.max
