N=gets.to_i
dist_f=N.times.map{gets.split("").map{|e|e.to_i==1 ? 1 : Float::INFINITY}}
N.times do |i|
    dist_f[i][i]=0
end
dist=Array.new(N){Array.new(N,0)}
N.times do |i|
    N.times do |j|
        dist[i][j]=dist_f[i][j]
    end
end
cnt=0
N.times do |k|
    dist_next=Array.new(N){Array.new(N,Float::INFINITY)}
    N.times do |i|
        N.times do |j|
            a=dist[i][j]
            b=dist[i][k]+dist[k][j]
            bet=a<b ? a : b
            dist_next[i][j]=bet
            cnt=cnt < bet ? bet : cnt if bet!=Float::INFINITY
        end
    end
    dist=dist_next
end

color=Array.new(N,false)
stack=Array.new
stack << 0

until stack.empty?
    i=stack.pop
    next if color[i]
    
    z,o=false,false
    dist_f[i].each_with_index do |e,j|
        if e==1
            stack << j
            z=true if color[j]==0
            o=true if color[j]==1
        end   
    end
    if z && o
        puts -1
        exit
    elsif z && !o
        color[i]=1
    else
        color[i]=0
    end
    
end
puts cnt+1