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

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

n.times do |k|
    n.times do |i|
        n.times do |j|
            x = dp[i][k] + dp[k][j]

            if dp[i][j] != 10000 && (dp[i][j] - x).odd?
                puts -1
                exit
            end

            dp[i][j] = x if dp[i][j] > x
        end
    end
end

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