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 <iostream>
#include <utility>
#include <algorithm>
using namespace std;

int p[100];
pair<int, int> a[10000];

int root( int m ) {
	if ( p[m] != m )
		p[m] = root( p[m] );
	return p[m];
}

void unit( int x, int y ) {
	p[root( y )] = root( x );
}

bool same( int x, int y ) {
	return root( x ) == root( y );
}

int main() {
	int n, ai, pn, ans;
	cin >> n;
	for ( int i = 0; i < n * n; i++ ) {
		cin >> ai;
		if ( ai == -1 || ai % n <= ai / n )
			ai = 2001;
		a[i] = make_pair( ai, i );
	}
	sort( a, a + ( n * n ) );
	pn = 0;
	ans = 0;
	for ( int i = 0; i < n; i++ )
		p[i] = i;
	for ( int i = 0; i < n * n; i++ ) {
		if ( ! same( a[i].second % n, a[i].second / n ) ) {
			ans += a[i].first;
			pn++;
			unit( a[i].second % n, a[i].second / n );
			if ( pn >= n - 1 ) break;
		}
	}
	cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iomanip>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
using namespace std;
#define INT_MAX 2147483647
#define INT_MIN (-2147483647 - 1)
#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e14
#define _INFLL (long long)-1e14
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> P;

class MST {
private:
	struct node {
		bool done;
		vi to, cost;
		int id, from;
		ll d;
		bool operator<(const node & another) const {
			return !(d != another.d ? d < another.d : id < another.id);
		}
	};
	vector<node> nodes;
	int n;
public:
	vll result;
	MST(vector<vector<P>> data) {
		n = data.size();
		result.resize(n);
		nodes.resize(n);
		Loop(i, n) {
			nodes[i] = { false,{},{}, i, -1, INFLL };
			Loop(j, data[i].size()) {
				nodes[i].to.push_back(data[i][j].first);
				nodes[i].cost.push_back(data[i][j].second);
			}
		}
	}
	void mainfunc(int start) {
		priority_queue<node> pq;
		nodes[start].d = 0;
		pq.push(nodes[start]);
		while (pq.size()) {
			node a = pq.top(); pq.pop();
			if (nodes[a.id].done) continue;
			nodes[a.id].done = true;
			Loop(j, a.to.size()) {
				node* b = &nodes[a.to[j]];
				if ((*b).done) continue;
				if (a.cost[j] < (*b).d) {
					(*b).d = a.cost[j];
					(*b).from = a.id;
					pq.push(*b);
				}
			}
		}
		Loop(i, n) {
			result[i] = nodes[i].d;
		}
	}
};

int main() {
	int n; cin >> n;
	vector<vector<P>> data(n);
	Loop(i, n) {
		Loop(j, n) {
			int x; cin >> x;
			if (x == -1) continue;
			else data[i].push_back({ j,x });
		}
	}
	MST mst(data);
	mst.mainfunc(0);
	ll ans = 0;
	Loop(i, n) {
		ans += mst.result[i];
	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
using namespace std;
static const int N = 100;
static const int INF = 100000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];


int prim() {
    int u, minv;
    int color[N], d[N], p[N];
    for (int i = 0; i < n; i++){
        color[i] = WHITE;
        d[i] = INF;
        p[i] = -1;
    }

    d[0] = 0;

    while(true) {
        minv = INF;
        u = -1;
        // Tからの距離がもっとも短いものを次のノードとして選ぶ
        for (int i = 0; i < n; i++) {
            if (minv > d[i] && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1) break; // なければ修了
        color[u] = BLACK; // uをT側に取り込む
        // d[]を更新(Tからの距離を更新)
        for (int v = 0; v < n; v++){
            if (color[v] != BLACK && M[u][v] != INF){
                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() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int e;
            cin >> e;
            M[i][j] = (e == -1) ? INF : e;
        }
    }

    cout << prim() << endl;

    return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

#define N 100

void searchIndex(int mat[][N], int n, int obj, int *row, int *col);
void changeGroup(int group[], int n, int g_old, int g_new);

int main(void)
{
	int n;
	cin >> n;

	int group[N];
	for (int i = 0; i < n; i++) group[i] = i;

	int a[N][N];	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int aij;
			cin >> aij;
			a[i][j] = aij;
		}
	}

	int e[N * N];
	int k = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			e[k++] = a[i][j];
		}
	}

	sort(e, e + k);

	int p = 0;
	while (e[p] == -1) p++;

	int cnt = 0;
	for (int q = p; q < k; q++) {
		int i, j;
		searchIndex(a, n, e[q], &i, &j);
		if (group[i] == group[j]) continue;
		cnt += e[q];
		if (group[i] > group[j]) changeGroup(group, n, group[i], group[j]);
		else changeGroup(group, n, group[j], group[i]);
	}

	cout << cnt << endl;

	return 0;
}

void searchIndex(int mat[][N], int n, int obj, int *row, int *col)
{
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (mat[i][j] == obj) {
				mat[i][j] = -1;
				*row = i;
				*col = j;
				return;
			}
		}
	}
}

void changeGroup(int group[], int n, int g_old, int g_new)
{
	for (int i = 0; i < n; i++) {
		if (group[i] == g_old) group[i] = g_new;
	}
}