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 <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;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define REP(i, n) for (long long i = 0; i < (n); i++)
struct UnionFind {
    vector<int> parent, size;
    UnionFind(int n) {
        parent.resize(n, -1);
        size.resize(n, 1);
    }
    void unite(int x, int y) {
        if (same(x, y)) {
            return;
        }
        x = root(x);
        y = root(y);
        if (size[x] <= size[y]) {
            swap(x, y);
        }
        parent[y] = x;
        size[x] += size[y];
    }
    bool same(int x, int y) { return (root(x) == root(y)); }
    int root(int x) {
        while (parent[x] != -1) {
            x = parent[x];
        }
        return x;
    }
    int getsize(int x) { return size[root(x)]; }
};
vector<vector<P>> mst(vector<vector<P>> graph) {
    vector<pair<ll, P>> as;
    REP(i, graph.size()) {
        REP(j, graph[i].size()) { as.push_back({graph[i][j].second, {i, graph[i][j].first}}); }
    }
    sort(as.begin(), as.end());
    UnionFind uf(graph.size());
    vector<vector<P>> ans(graph.size());
    REP(i, as.size()) {
        if (!uf.same(as[i].second.first, as[i].second.second)) {
            ans[as[i].second.first].push_back({as[i].second.second, as[i].first});
            ans[as[i].second.second].push_back({as[i].second.first, as[i].first});
            uf.unite(as[i].second.second, as[i].second.first);
        }
    }
    return ans;
}
signed main() {
    int v;
    cin >> v;
    vector<vector<P>> graph(v);
    vector<vector<P>> ans(v);
    REP(i, v) {
        REP(j, v) {
            int a;
            cin >> a;
            if (a != -1) {
                graph[i].push_back({j, a});
            }
        }
    }
    ans = mst(graph);
    ll sum = 0;
    REP(i, v) {
        REP(j, ans[i].size()) { sum += ans[i][j].second; }
    }
    cout << sum / 2 << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
  static const int MAX=100;
  static const int INF=(1<<21);
  static const int WHI=0;
  static const int GRA=1;
  static const int BLA=2;
  int n,M[MAX][MAX];
  int prim(){
    int u,minv;
    int d[MAX],p[MAX],color[MAX];
    for(int i=0;i<n;i++){
      d[i]=INF;
      p[i]=-1;
      color[i]=WHI;
    }
    
    d[0]=0;
    while(1){
      minv=INF;
      u=-1;
      for(int i=0;i<n;i++){
	if(minv>d[i]&&color[i]!=BLA){
	  u=i;
	}
      }

      if(u==-1){
	break;
      }
      color[u]=BLA;

      for(int v=0;v<n;v++){
	if(color[v]!=BLA&&M[u][v]!=INF){
	  if(M[u][v]<d[v]){
	    d[v]=M[u][v];
	    p[v]=u;
	    color[v]=GRA;
	  }

	}
      }
    }

    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;
	if(e==-1){
	  M[i][j]=INF;
	}else{
	  M[i][j]=e;
	}
      }
    }
    
    cout<<prim()<<endl;
    return 0;
  }

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

#define NIL -1
#define MAX (1 << 24)

struct Node {
	int dis = MAX;
	bool flag = false;
	vector<int> d;
	vector<int> id;
};

void dfs(vector<Node>& u, int id)
{
	u[0].dis = 0;

	while (1)
	{
		bool fin = true;
		int mini = MAX;
		int n;
		for (int i = 0; i < u.size(); i++)
		{
			if (!u[i].flag && u[i].dis < mini) {
				mini = u[i].dis;
				n = i;
				fin = false;
			}
		}

		if (fin) return;
		u[n].flag = true;

		for (int i = 0; i < u[n].id.size(); i++)
		{
			if (u[u[n].id[i]].dis > u[n].d[i] && !u[u[n].id[i]].flag) {
				u[u[n].id[i]].dis = u[n].d[i];
			}
		}
	}
}

int main(void)
{
	int i, N, m, q = 0;
	cin >> N;
	vector<Node> u(N);
	for (i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			cin >> m;
			if (m == NIL) continue;
			u[i].d.push_back(m);
			u[i].id.push_back(j);
		}
	}

	
	dfs(u, 0);

	for (i = 0; i < N; i++) q += u[i].dis;

	cout << q << endl;

	return 0;
}