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 <algorithm>
#include <cassert>
const int MaxNode = 100;

struct UnionFind {
    int node_num;
    int parent[MaxNode];
    int weight = 0;
    
    UnionFind(int node_num) : node_num(node_num) {
        std::fill(parent, parent + node_num, -1);
    }
    
    int root(int u) {
        return parent[u] < 0 ? u : parent[u] = root(parent[u]);
    }
    
    bool find(int u, int v) {
        return root(u) == root(v);
    }
    
    bool unite(int u, int v, int cost) {
        if ((u = root(u)) == (v = root(v))) {
            return false;
        } else {
            if(parent[u] > parent[v]) {
                std::swap(u, v);
            }
            parent[u] += parent[v];
            parent[v] = u;
            weight += cost;
            return true;
        }
    }
};
template <typename T>
struct Edge {
    
    Edge() = default;
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
    
    Edge(const Edge&) = default;
    Edge& operator=(const Edge&) = default;
    Edge& operator=(Edge&&) = default;
    
    
    int from;
    int to;
    T cost;
};

#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	UnionFind uf(n);
	
	std::vector<Edge<int>> edges(n);
	
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) {
			int dist;
			cin >> dist;
			
			if(i != j && dist != -1) {
				edges.emplace_back(i, j, dist);
			}
		}
	}
	
	std::sort(edges.begin(), edges.end(), [](Edge<int> a, Edge<int> b){return a.cost < b.cost;});

	for (auto& e : edges) {
        if (!uf.find(e.from, e.to)) {
            uf.unite(e.from, e.to, e.cost);
        }
    }
    
    cout << uf.weight << endl;
	return 0;
}

C++: 
class UnionFind{
    int N;
    vector<int> par;
    vector<int> rank;
    public:
    UnionFind(int N):N(N),par(N),rank(N,0){iota(par.begin(),par.end(),0);}
    int find(int x);
    void unite(int x,int y);
    bool same(int x,int y){return find(x)==find(y);}
};

int UnionFind::find(int x){
    if(par[x]==x) return x;
    return par[x]=find(par[x]);
}
void UnionFind::unite(int x,int y){
    x=find(x),y=find(y);
    if(x==y) return;
    if(rank[x]>=rank[y]){
        if(rank[x]==rank[y]) rank[x]++;
        par[y]=x;
    }else{
        par[x]=y;
    }


}

#include<bits/stdc++.h>
#include"UnionFind.hpp"
using namespace std;

using Weight=long long;
struct Edge{
    int from,to;
    Weight weight;
};
bool operator<(const Edge& lhs,const Edge& rhs){
    return lhs.weight<rhs.weight;
}

using Edges=vector<Edge>;
using Graph=vector<Edges>;


Edges MinimumSpanningTree(int n,Edges edges){
    Edges res;
    UnionFind u(n);
    for(int i=0;i<edges.size();i++){
        if(!u.same(edges[i].from,edges[i].to)){
            res.push_back(edges[i]);
            u.unite(edges[i].from,edges[i].to);
        }
    }
    return res;
}

Edges MinimumSpanningTree(const Graph& g){
    Edges edges;
    for(int i=0;i<g.size();i++){
        copy(g[i].begin(),g[i].end(),back_inserter(edges));
    }
    auto res=MinimumSpanningTree(g.size(),edges);
    return res;
}

int main(){
    int n;
    cin>>n;
    Graph g(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            Weight c;
            cin>>c;
            if(c==-1) continue;
            g[i].push_back({i,j,c});
        }
    }
    auto mst=MinimumSpanningTree(g);
    cout<<accumulate(mst.begin(),mst.end(),Weight(0),[](const Weight& lhs,const Edge& rhs){return lhs+rhs.weight;})<<endl;
    return 0;
}