Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

C++: #include <stdio.h>
#include <vector>
#include <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>
#include <numeric>
#include <queue>

using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf("%d", list[i]);
        if ( i + 1 != list.size() ) {
            printf(" ");
        }
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 要素数固定で汎用性低し
// index = key, value = parent key
class UnionFindTree{
private:
    vector<int> list;
public:
    UnionFindTree(int num)
        :list(num+1)
    {
        for(int i = 0; i < this->list.size(); i++) {
            list[i] = i;
        }
    }

    int Find(int key) {
        if (list[key] == key) {
            return key;
        } else {
            return Find(list[key]);
        }
    }
    bool AreSame(int lhs, int rhs){
        return Find(lhs) == Find(rhs);
    }
    void Union(int lhs, int rhs){
        this->list[Find(lhs)] = this->list[Find(rhs)];
    }

    void Print() {
        for(int i = 1; i < this->list.size(); i++) {
            printf("%d -> %d\n", i, this->list[i]);
        }
    }
};

class Node{
public:
    Node(int id, int numEdge)
     : id(id)
     , edges(vector<int>(numEdge + 1))
    {
        // constructer
    }
    void Edge(int id, int weight){
        this->edges[id] = weight;
    }
    vector<int> Edges() {
        return this->edges;
    }

    int id;
    bool done = false;
    vector<int> edges;
};

typedef struct _Relation{
    _Relation(int weight, int from, int to)
        : weight(weight)
        , from(from)
        , to(to)
    {}
    int weight;
    int from;
    int to;
} Relation;

class List{
public:
    // constructer
    List(int numNode)
    {
        this->nodes.push_back(Node(0, 0));
        for (int i = 1; i <= numNode; i++) {
            this->nodes.push_back(Node(i, numNode));
        }
    }

    void SetWeight(int from, int to, int weight)
    {
        this->nodes[from].Edge(to, weight);
    }
    
    int CalcWeight() {
        UnionFindTree tree(this->nodes.size() - 1); // init by num elements
        std::list<Relation> costs;
        for (int i = 1; i < this->nodes.size(); i++) {
            for (int j = 1; j < this->nodes[i].Edges().size(); j++) {
                int cost = this->nodes[i].Edges()[j];
                if(cost == -1) continue;
                costs.push_back(Relation(this->nodes[i].Edges()[j], i, j));
            }
        }
        costs.sort([](const Relation& lhs, const Relation& rhs){ return lhs.weight < rhs.weight; });
        vector<Relation> result;
        while(!costs.empty()) {
            auto min = costs.front();
            costs.pop_front();
            if (!tree.AreSame(min.from, min.to)) {
                tree.Union(min.from, min.to);
                result.push_back(min);
            }
        }
        int sum = 0;
        for (auto each : result) {
            sum += each.weight;
        }
        return sum;
    }
    
    

    void Print() {
        for (int i = 1; i < this->nodes.size(); i++) {
            printf("%d:", i);
            for (int j = 1; j < this->nodes[i].Edges().size(); j++) {
                printf(" %d", this->nodes[i].Edges()[j]);
            }
            printf("\n");
        }
    }
private:
    vector<Node> nodes;
};

void solve() {

    int n;
    scanf("%d", &n);
    List list(n);

    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++) {
            int w;
            scanf("%d", &w);
            list.SetWeight(i, j, w);
        }
    }
    // list.Print();
    printf("%d\n", list.CalcWeight());
}

void test(){
    UnionFindTree t(5);
    t.Union(1, 2);
    t.Union(2, 3);
    t.Union(4, 5);
    t.Print();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    solve();
    // test();
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n,a[100][100],b[100]={},c[100]={};
  c[0]=1;
  cin>>n;
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)cin>>a[i][j];

  int k=1,min1,ans=0;
  while(1){
   int min=10000;
   for(int i=0;i<k;i++)
    for(int j=0;j<n;j++)
     if(a[b[i]][j]<min && a[b[i]][j]!=-1 &&c[j]!=1){
      min=a[b[i]][j];
      min1=j;
    }
   if(k==n)break;
   b[k]=min1;
   c[min1]=1;
   ans+=min;
   k++;
  }

 cout<<ans<<endl;
 return 0;
}