$UFT = Array.new(100)
0.upto(100) do |i|
  $UFT[i] = i
end

def root(x)
  if ($UFT[x] == x)
    x
  else
    $UFT[x] = root($UFT[x])
    $UFT[x]
  end
end

def unite(x, y)
  $UFT[root(x)] = root(y)
end

if __FILE__ == $0
  n = gets.to_i
  graph = n.times.map{ gets.split.map(&:to_i) }
  bridge = []
  total = 0

  n.times do |i|
    0.upto(i-1) do |j|
      cost = graph[i][j]
      next if cost == -1
      bridge << [cost, i, j]
    end
  end

  bridge.sort!.each do |cost, i, j|
    unless root($UFT[i]) == root($UFT[j])
      total += cost
      unite(i, j)
    end
  end

  puts total
end