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++: #include <iostream>
using namespace std;
static const int MAX = 101;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim()
{
    int u, mincost;
    int d[MAX], p[MAX], color[MAX];
     for(int i=0; i<MAX; i++)
     {
         color[i] = WHITE;
         d[i] = INFTY;
         p[i] = -1;
     }
     d[0] = 0;
     while(1)
     {
         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()
{
     cin >> n;
     int e;
     for(int i=0; i<n; i++)
     {
         for(int j=0; j<n; j++)
         {
             cin >> e;
             if(e == -1)
                M[i][j] = INFTY;
             else
                M[i][j] = e;
         }
     }
     cout << prim() << endl;
     return 0;
}

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

using Weight = int;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src_, int dst_, int weight_) :
        src(src_), dst(dst_), weight(weight_) { }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

bool operator < (const Edge &e, const Edge &f){ return e.weight > f.weight; }
pair<Weight, Edges> prim(const Graph &g, int r = 0) {
    Edges T; Weight total = 0; vector<int> vis(g.size());
    priority_queue<Edge> q;
    q.emplace(-1, r, 0);
    while (!q.empty()) {
        Edge e = q.top(); q.pop();
        if (vis[e.dst]) continue;
        vis[e.dst] = true;
        total += e.weight; if(e.src != -1) T.push_back(e);
        for(auto& f : g[e.dst]) if (!vis[f.dst]) q.push(f);
    }
    return make_pair(total, T);
}

int main(){
    int n;
    cin >> n;
    Graph g(n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int x;
            cin >> x;
            if(x != -1) g[i].emplace_back(i,j,x);
        }
    }
    cout << prim(g).first << endl;
}

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

const int INF = numeric_limits<int>::max();

struct Graph {
    enum Color {WHITE, GRAY, BLACK};
    vector<vector<int>> cost;
    vector<Color> color;
    vector<int> mincost;
    Graph(int n) {
        cost.assign(n, vector<int>(n, INF));
        color.assign(n, WHITE);
        mincost.assign(n, INF);
    }
};

int main()
{
    int n;
    cin >> n;
    Graph g(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> g.cost[i][j];
            g.cost[i][j] = g.cost[i][j] == -1 ? INF : g.cost[i][j];
        }
    }
    
    int ans = 0;

    // Prim
    g.mincost[0] = 0;
    while (true) {
        int u = -1;
        for (int v = 0; v < n; v++) {
            if (g.color[v] == Graph::WHITE && (u == -1 || g.mincost[v] < g.mincost[u])) {
                u = v;
            }
        }
        if (u == -1) break;
        g.color[u] = Graph::BLACK;
        ans += g.mincost[u];
        for (int v = 0; v < n; v++) {
            g.mincost[v] = min(g.mincost[v], g.cost[u][v]);
        }
    }
    cout << ans << endl;
}