def setup_edges(matrix)
  edges = []
  matrix.each_with_index do |row, ind_row|
    row.each_with_index do |col, ind_col|
      if col >= 0 then
        edges << [ind_col+1, ind_row+1, col]
      end
    end
  end
  return edges.sort_by {|edge| edge[2]}
end


def solve(edges)
  sum = 0
  vertices = Hash.new
  (edges.size).times do |i|
    vertices[i] = i
  end

  until edges.empty? do
    edge = edges.shift
    v1, v2 = vertices[edge[0]-1], vertices[edge[1]-1]
    next if v1 == v2
    sum += edge[2]
    min_id = [v1, v2].min
    vertices.each do |key, val|
      vertices[key] = min_id if val == v1 || val == v2
    end
  end
  return sum
end


# main
n = gets.chomp.to_i
matrix = []
(n).times do 
  matrix << gets.chomp.split.map(&:to_i)
end

edges = setup_edges(matrix)
puts solve(edges)