N=gets.to_i
dp = N.times.map {
  gets.chomp.chars.map{|c|c=='1'?1:666}
}

N.times do |i|
  N.times do |j|
    if i == j
      dp[i][j] = 0
      next
    end
    N.times do |k|
      dp[i][j]=[dp[i][j],dp[i][k]+dp[k][j]].min
    end
  end
end

ret = dp.max + 1
if ret < N-1
  p ret
else
  p -1
end
