N = gets.to_i
S = N.times.map{
	gets.strip.chars.map{|_| _=='1' }
}

d = [nil]*N
(0..(N-2)).each{|i|
	((i+1)..(N-1)).each{|j|
		next unless S[i][j]
		d[i] = true if d[i].nil?
		d[j] = ! d[i] if d[j].nil?
		if d[i] == d[j]
			p(-1)
			exit
		end
	}
}

d = [1]*(N*(N-1)/2)
(0..(N-2)).each{|i|
	ij0 = (N-1)*N/2-(N-i-1)*(N-i)/2
	((i+1)..(N-1)).each{|j|
		next unless S[i][j]
		ij = ij0 + (j-i-1)
		dij = d[ij]
		dij = d[ij] = 2 if dij == 1
		jk0 = (N-1)*N/2-(N-j-1)*(N-j)/2
		((j+1)..(N-1)).each{|k|
			next unless S[j][k]
			next if S[i][k]
			jk = jk0 + (k-j-1)
			djk = d[jk]
			djk = d[jk] = dij+1 if djk <= dij
		}
	}
}
p d.max
