$n = gets.to_i
INF = 1 << 30
$mat = []
$n.times{ $mat << gets.split.map(&:to_i) }
$v = []
$e = []
def prim(x)
  $v << x
  while $v.size < $n
    min_v ,min_t = INF
    $v.each do |t|
      $n.times do |i|
        next if $v.include? i 
        if $mat[t][i] != -1 && min_v > $mat[t][i]
          min_v = $mat[t][i]
          min_t = i
        end
      end
    end
    $v << min_t
    $e << [min_v, min_t]
  end
  ret = 0 
  $e.each{ |v| ret += v[0] }
  ret
end
p prim(0)