n = gets.chomp.to_i
INF = 1_000_000
dp = Array.new(n){ Array.new(n){ INF } }

n.times do |i|
  dp[i][i] = 0
end

edges = Array.new(n){ [] }

n.times do |i|
  s = gets.chomp
  s.chars.each_with_index do |j, index|
    if j == "1"
      dp[i][index] = 1
      edges[i] << index
    end
  end
end

# 三角形の頂点かどうかを調べる
edges.each do |edge|
  # [1,2,3] 同士が直接つながっていたらNG
  edge.each do |i|
    if edge.include?(edges[i])
      puts -1
      exit
    end
  end
end

n.times do |i|
  n.times do |j|
    n.times do |k|
      dp[i][j] = [dp[i][j], dp[i][k] + dp[k][j]].min
    end
  end
end

max = 0
dp.each do |i|
  max = [max, i.max].max
end

puts max+1
