Go: package main

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

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <cstdio>
#include <climits>
#include <vector>

int w[100][100];
std::vector<int> mst;
std::vector<int> others;
typedef std::vector<int>::iterator itr;

int minimum(){
    if(others.size()<=0)return 0;
    int candiW=INT_MAX;
    itr candiI;
    for(itr i=mst.begin();i!=mst.end();i++){
        for(itr j=others.begin();j!=others.end();j++){
            if(w[*i][*j]<candiW){
                candiW = w[*i][*j];
                candiI = j;
            }
        }
    }
    mst.push_back(*candiI);
    others.erase(candiI);
    return candiW + minimum();
}

int main(){
    int i, j, n;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            scanf("%d",&w[i][j]);
            if(w[i][j]==-1)w[i][j]=INT_MAX;
        }
    }
    mst.push_back(0);
    for(i=1;i<n;i++)others.push_back(i);
    printf("%d\n",minimum());
    return 0;
}

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

typedef long long ll;

struct Edge {
    ll s,t;
    ll w;
    bool operator < (const Edge& tgt) const {
	return w > tgt.w;
    }
};

ll Prim(const vector<vector<Edge>>& G) {
    const ll V = G.size();
    priority_queue<Edge> edges;
    
    vector<int> used(V,0);
    edges.push( Edge{-1,0,0} );
    ll ans = 0;
    while( edges.size() ) {
	Edge e = edges.top(); edges.pop();

	if( used[e.t] == 1 ) continue;
	used[e.t] = 1;
	ans += e.w;

	for(auto& nxt : G[e.t]) {
	    if( used[nxt.t] == 1 ) continue;
	    edges.push(nxt);
	}
    }
    return ans;
}


int main () {

    ll V; cin >> V;
    vector<vector<Edge>> G(V);
    for(int i=0; i<V; i++) {
	for(int j=0; j<V; j++) {
	    ll w; cin >> w;
	    if( w != -1 ) {
		G[i].push_back( Edge{i,j,w} );
	    }
	}
    }
    cout << Prim(G) << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using pii = pair<int, int>;

template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

//pow(llpow,modpow)
template <class T>
ll llpow(ll x, T n)
{
    ll ans = 1;
    if (x == 0)
        ans = 0;
    while (n)
    {
        if (n & 1)
            ans *= x;
        x *= x;
        n >>= 1;
    }
    return ans;
}
long long modpow(long long a, long long n, long long mod)
{
    long long res = 1;
    while (n > 0)
    {
        if (n & 1)
            res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
//最大公約数
template <class T>
inline T gcd(T x, T y)
{
    if (y == 0)
        return x;
    else
    {
        return gcd(y, x % y);
    }
}
//最小公倍数
template <class T>
inline T lcm(T x, T y) { return x / gcd(x, y) * y; }
//逆元
long long modinv(long long a, long long m)
{
    long long b = m, u = 1, v = 0;
    while (b)
    {
        long long t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0)
        u += m;
    return u;
}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)

long long INF = 1LL << 60;
int main()
{
    int n;
    cin >> n;
    vector<vi> a(n,vector<int> (n));
    rep(i, 0, n)
    {
        rep(j, 0, n)
        {
            cin >> a[i][j];
            if(a[i][j]==-1)a[i][j]=1<<30;
        }
    }
    ll res = 0;
    set<int> rem,visited;
    visited.insert(0);
    rep(i,1,n)rem.insert(i);
    while(!rem.empty()){
        int minv=1<<30,mini=-1;
        for(int prev:visited){
            for(int next:rem){
                if(a[prev][next]<minv){
                    minv=a[prev][next];
                    mini=next;
                }
            }
        }
        res+=minv;
        rem.erase(mini);
        visited.insert(mini);
    }
    cout<<res<<endl;
    return 0;
}