Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 2000000000;
class Disjointset {
public:
	vector<int> rank, p;
	Disjointset(int n);
	int find(int x);
	bool same(int x, int y);
	void unite(int x, int y);
};

Disjointset::Disjointset(int n) {
	rank.resize(n, 0);
	for (int i = 0; i < n; i++)
		p.push_back(i);
}
int Disjointset::find(int x) {
	if (x != p[x])
		return p[x] = find(p[x]);
	else return x;
}
bool Disjointset::same(int x, int y) {
	return find(x) == find(y);
}
void Disjointset::unite(int x, int y) {
	int px = find(x);
	int py = find(y);
	if (rank[px] > rank[py]) {
		p[py] = px;
	}
	else {
		p[px] = py;
		if (rank[px] == rank[py]) rank[py]++;
	}
}
struct edge {
	int from, to, cost;
	bool operator < (const edge &e) const {
		return cost > e.cost;
	}
};
int main() {
	int n; cin >> n;
	priority_queue <edge> q;
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) {
		int c; cin >> c;
		if (c != -1) { edge e; e.from = i; e.to = j; e.cost = c; q.push(e); }
	}
	Disjointset D(n);
	int res = 0;
	while (!q.empty()) {
		edge e = q.top(); q.pop();
		int f = e.from, t = e.to, c = e.cost;
		if (D.same(f, t)) continue;
		res += c;
		D.unite(f, t);
	}
	cout << res << endl;
	return 0;
}

C++: #include <stdio.h>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 0;
static const int BLACK = 0;

int n, M[MAX][MAX];

int prim() {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    d[0] = 0;
    p[0] = -1;
    
    while(1) {
        int u;
        int mincost = INFTY;
        for (int i=0; i<n; i++) {
            if (color[i] != BLACK && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        
        if (mincost == INFTY) break;
        
        color[u] = BLACK;
        
        for (int v=0; v<=n; v++) {
            if (color[v] != BLACK && M[u][v] != -1) {
                if(M[u][v] < d[v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    
    int sum = 0;
    for (int i=0; i<n; i++) {
        if(p[i] != -1) sum += M[i][p[i]];
    }
    return sum;
}

int main() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            scanf("%d", &M[i][j]);
        }
    }
    
    int sum = prim();
    printf("%d\n", sum);
    
    return 0;
}

C++: #include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include<cstdio>
#include <vector>
#include<queue>
#include<limits.h>
#include<string.h>
#include<functional>
#include<map>
#include<set>
#include<list>
#include<unordered_map>
#include<queue>
#define P pair<int,int>
using namespace std;

int a[100][100];
bool used[100];
signed main(){
	int b; cin >> b;
	for (int c = 0; c < b; c++) {
		for (int d = 0; d < b; d++) {
			cin >> a[c][d];
			if (a[c][d] == -1) {
				a[c][d] = INT_MAX / 3;
			}
		}
	}
	int ans = 0;
	priority_queue<P,vector<P>, greater<P>>Q;
	Q.push({ 0,0 });
	while (Q.size()) {
		P t = Q.top(); Q.pop();
		if (!used[t.second]) {
			ans += t.first;
			used[t.second] = true;
			for (int i = 0; i < b; i++) {
				Q.push({ a[t.second][i],i });
			}
		}
	}
	cout << ans << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int M[200][200];
string color[200];
int d[200];
int p[200];
int n;
const int INF = 1001001001;

void prim(){
    rep(i, n){
        d[i] = INF;
        color[i] = "WHITE";
    }
    d[0] = 0;
    p[0] = -1;

    int u;

    while(true){
        int mincost = INF;
        for (int i = 0; i < n; i++){
            if(color[i] != "BLACK" && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }
        if(mincost == INF){
            break;
        }
        color[u] = "BLACK";

        for (int i = 0; i < n; i++){
            if(color[i] != "BLACK" && M[u][i] != -1){
                if(M[u][i] < d[i]){
                    d[i] = M[u][i];
                    p[i] = u;
                    color[i] = "GRAY";
                }
            }
        }
    }
}

int main() {
    cin >> n;
    rep(i, n){
        rep(j, n){
            cin >> M[i][j];
        }
    }
    prim();
    int ans = 0;
    rep(i, n){
        if(d[i] != INF){
            ans += d[i];
        }
    }
    cout << ans << endl;

    return 0;
}