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>
#include<vector>

struct Edge {
	int row, column, weight;
};

void divide_graph(bool[], int);

int n;
Edge edges[99]{};

int main()
{
	int in_tmp, min{}, matrix[100][100]{};

	std::cin >> n;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j)
		{
			std::cin >> in_tmp;
			matrix[i][j] = in_tmp;
		}

	int rest = n - 1, pre_size = 0;
	std::vector<int> queue{ 0 };
	bool visited[100]{ true };
	for (int i = 1; rest; ++i)
	{
		int now_size = queue.size();
		for (int j = pre_size; j < now_size; ++j)
			for (int k = 0; k < n; ++k)
			{
				int tmp = matrix[queue[j]][k];
				if (tmp != -1 && !visited[k])
				{
					visited[k] = true;
					rest--;
					edges[queue.size() - 1] = { queue[j], k , tmp};
					queue.push_back(k);
				}
			}
		pre_size = now_size;
	}

	int max_edge = -1, pre_max_edge = 0;
	while (max_edge != pre_max_edge)
	{
		pre_max_edge = max_edge;
		max_edge = 0;
		for (int i = 1; i < n - 1; ++i)
			if (edges[i].weight > edges[max_edge].weight)
				max_edge = i;

		bool conne_row[100]{};
		divide_graph(conne_row, max_edge);

		Edge new_edge = edges[max_edge];
		for (int i = 0; i < n; ++i)
		{
			if (!conne_row[i]) continue;
			for (int j = 0; j < n; ++j)
			{
				if (conne_row[j] || matrix[i][j] >= new_edge.weight) continue;
				if (matrix[i][j] == -1) continue;

				new_edge = { i, j, matrix[i][j] };
			}
		}

		edges[max_edge] = new_edge;
	}

	for (int i = 0; i < n - 1; ++i)
		min += edges[i].weight;

	std::cout << min << std::endl;
	
	return 0;
}

void divide_graph(bool conne_row[], int max_edge)
{
	conne_row[edges[max_edge].row] = true;

	std::vector<int> queue = { edges[max_edge].row };
	int pre_size = 0;

	bool visit_edge[100]{};
	visit_edge[max_edge] = true;

	while (pre_size != queue.size())
	{
		int now_size = queue.size();
		for (int j = pre_size; j < now_size; ++j)
		{
			for (int k = 0; k < n - 1; ++k)
			{
				if (visit_edge[k]) continue;
				if (edges[k].row == queue[j])
				{
					visit_edge[k] = true;
					conne_row[edges[k].column] = true;
					queue.push_back(edges[k].column);
				}
				else if (edges[k].column == queue[j])
				{
					visit_edge[k] = true;
					conne_row[edges[k].row] = true;
					queue.push_back(edges[k].row);
				}
			}
		}
		pre_size = now_size;
	}
	return;
}

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

int n;
vector<pair<int,int>> G[110];
vector<int> seen(110, false);

int prim(){
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> qu;
    int sum = 0, size = 0;

    int u=0;
    seen[u]=true;
    size=1;
    while(size<n){
        for(auto e:G[u]) if(!seen[e.second]) qu.push(e); 
        while(1){
            pair<int, int> p=qu.top(); qu.pop();
            if(!seen[p.second]){
                u = p.second;
                sum += p.first;
                seen[u] = true;
                ++size;
                break;
            }
        }
    }
    return sum;
}

int main(){
    cin >> n;
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            int w; cin >> w;
            if(w!=-1) G[i].push_back(make_pair(w, j));
        }
    }

    cout << prim() << '\n';

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

struct unionfind {
    vector<int> data;
    unionfind(int size) : data(size, -1) { }
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

struct edge {
    int from,to,cost;
};

vector<edge> kruscal(vector<edge> & es){
    sort(es.begin(), es.end(), [](edge a, edge b){
            return a.cost < b.cost;
        });
    unionfind uf(es.size());
    vector<edge> res;
    rep(i,es.size()){
        const edge &e = es[i];
        if(!uf.same(e.from, e.to)){
            uf.unite(e.from,e.to);
            res.push_back(e);
        }
    }
    return res;
}

int main(){
    vector<edge> es;
    int n; cin>>n;
    rep(i,n)rep(j,n){
        int c; cin>>c;
        if(c!=-1) es.push_back({i,j,c});
    }
    auto v = kruscal(es);
    int ans = 0;
    rep(i,v .size()){
        ans += v[i].cost;
    }
    cout << ans << endl;
}