n = gets.to_i
s = n.times.map {gets.chomp}

dp = Array.new(n) {Array.new(n, 10000)}
s.each_with_index do |e, i|
    dp[i][i] = 0
    e.each_char.with_index {|c, j| dp[i][j] = 1 if c == ?1}
end

n.times do |k|
    n.times do |i|
        n.times do |j|
            dp[i][j] = dp[i][k] + dp[k][j] if dp[i][j] > dp[i][k] + dp[k][j]
        end
    end
end

x = Array.new(n)
x[0] = true
que = [0]
while y = que.shift
    s[y].each_char.with_index do |c, i|
        next if c == ?0
        if x[i].nil?
            x[i] = x[y] ^ true
            que << i
        elsif !(x[i] ^ x[y])
            puts -1
            exit
        end
    end
end

#dp.each {|e| p e}
puts dp.map(&:max).max + 1