Go: package main

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

var (
	n int
	M [MAX][MAX]int
)

const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())
			if c != -1 {
				M[i][j] = c
			} else {
				M[i][j] = math.MaxInt64
			}

		}
	}
	prim()
}

func prim() {
	var u int
	var minv int
	var d [MAX]int
	var p [MAX]int
	var color [MAX]int

	for i := 0; i < n; i++ {
		d[i] = math.MaxInt64
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		minv = math.MaxInt64
		u = -1
		for i := 0; i < n; i++ {
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK

		for v := 0; v < n; v++ {
			if color[v] != BLACK && M[u][v] != math.MaxInt64 {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}
	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	fmt.Println(sum)

}

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

const int INF = 1001001001;
vector<vector<int>> cost;
vector<int> mincost;
vector<bool> used;
int V;

int prim(){
    mincost.assign(V,INF);
    used.assign(V,0);
    mincost[0] = 0;
    int res = 0;
    while(true){
        int v = -1;
        for(int u = 0; u < V; ++u){
            if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
        }
        if(v == -1) break;//completed
        used[v] = 1;
        res += mincost[v];
        for(int u = 0; u < V; ++u){
            if(cost[v][u] != -1)mincost[u] = min(mincost[u], cost[v][u]);//確定した頂点から行ける頂点について、最小コストを更新
        }
    }
    return res;
}


int main(){
    cin >> V;
    cost.assign(V,vector<int>(V));
    rep(i,V)rep(j,V) cin >> cost[i][j];
    int ans = prim();
    cout << ans << endl;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <tuple>

using namespace std;

class UnionFindTree {
private:
    vector<int32_t> parent, rank;

    int32_t find(int32_t x) {
        if (parent[x] == x) {
            return x;
        } else {
            int32_t p = find(parent[x]);
            parent[x] = p;
            return p;
        }
    }

public:
    UnionFindTree(int32_t n) {
        parent.resize(n);
        rank.resize(n);

        for (int32_t i = 0; i < n; i++) {
            parent[i] = i;
            rank[i] = 0;
        }
    }

    void unite(int32_t x, int32_t y) {
        x = find(x);
        y = find(y);

        if (x == y) return;

        if (rank[x] < rank[y]) {
            parent[x] = y;
        } else {
            parent[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
        }
    }

    bool same(int32_t x, int32_t y) {
        return find(x) == find(y);
    }
};

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

    UnionFindTree uf(n);
    priority_queue<tuple<int32_t, int32_t, int32_t>, vector<tuple<int32_t, int32_t, int32_t>>, greater<tuple<int32_t, int32_t, int32_t>>> E;

    for (int32_t i = 0; i < n; i++) {
        for (int32_t j = 0; j < n; j++) {
            int32_t w;
            cin >> w;

            if (w > -1) {
                E.push(make_tuple(w, i, j));
            }
        }
    }

    int32_t sum = 0;
    while (!E.empty()) {
        int32_t w, u, v;
        tie(w, u, v) = E.top();
        E.pop();
        if (!uf.same(u, v)) {
            sum += w;
            uf.unite(u, v);
        }
    }

    cout << sum << endl;

    return 0;
}