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++: #define dbg(...) fprintf(stderr,__VA_ARGS__)
#define dpr(x) cerr<<#x<<": "<<x<<endl;
#define dprc(c) do{cerr<<#c<<":";for(auto&_i:(c)){cerr<<" "<<_i;}cerr<<endl;}while(0)
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
int INF = 1e9+7;
#define all(c) (c).begin(), (c).end()
#define tr(i,c) for(auto i=(c).begin();i!=(c).end();i++)
#define rtr(i,c) for(auto i=(c).rbegin();i!=(c).rend();i++)
#define rep(i,b) for(auto i=0;i<(b);i++)
#define pb push_back
#define sz(c) int((c).size())

#define MAX_N 110

class UnionFind {
public:
  UnionFind(size_t s) : parent_(s), rank_(s, 0) {
    for (size_t i = 0; i < s; i++) {
      parent_[i] = i;
    }
  }

  void unite(size_t x, size_t y) {
    size_t xp = find(x);
    size_t yp = find(y);

    if (xp == yp) return;
    if (rank_[xp] < rank_[yp]) {
      parent_[xp] = parent_[yp];
    } else {
      if (rank_[xp] == rank_[yp]) rank_[xp]++;
      parent_[yp] = parent_[xp];
    }
  }

  bool is_same(size_t x, size_t y) {
    return find(x) == find(y);
  }

  size_t find(size_t x) {
    if (parent_[x] == x) return x;
    else return parent_[x] = find(parent_[x]);
  }

  size_t size() const {
    return parent_.size();
  }

private:
  vector<int> parent_;
  vector<int> rank_;
};

struct edge {
  int w, v, u;
};

void printuf(UnionFind& uf) {
  for (int i = 0; i < sz(uf); i++) {
    fprintf(stderr, " %-2d", i);
  }
  puts("");
  for (int i = 0; i < sz(uf); i++) {
    fprintf(stderr, " %-2lu", uf.find(i));
  }
  puts("");
}

int main(int argc, char **argv)
{

  int n;
  scanf("%d", &n);

  vector<edge> E;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int x;
      scanf("%d", &x);
      if (i >= j) continue;
      edge e = {x, i, j};
      if (x >= 0) E.pb(e);
    }
  }

  sort(all(E), [=](const edge& lhs, const edge& rhs) { return lhs.w < rhs.w; });

  UnionFind S(n);

  int total = 0;

  for (int i = 0; i < sz(E); i++) {
    edge& e = E[i];

    int v = S.find(e.v);
    int u = S.find(e.u);

    if (v == u) continue;
    total += e.w;
    S.unite(e.v, e.u);
  }

  printf("%d\n", total);

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf (1 << 30)

static const int N = 100;
static const int white = 0;
static const int gray = 1;
static const int black = 2;

int n;
int m[N][N];

int prim() {
	int color[N];
	int d[N], p[N];
	int u;
	for (int i = 0; i < n; i++) {
		color[i] = white;
		d[i] = inf;
		p[i] = -1;
	}
	//color[0] = black;
	d[0] = 0;
	p[0] = -1;
	u = 0;
	while (true) {
		int mincost = inf;
		for (int i = 0; i < n; i++) {
			if (color[i] != black && d[i] < mincost) {
				mincost = d[i];
				u = i;
			}
		}
		if (mincost == inf) {
			break;
		}
		color[u] = black;	
		for (int v = 0; v < n; v++) {
			if (color[v] != black) {
				if (m[u][v] < d[v]) {
					d[v] = m[u][v];
					color[v] = gray;
					p[v] = u;
				}
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (color[i] == black) {
			sum += d[i];
		}
	}
	return sum;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int v;
			cin >> v;
			v == -1 ? m[i][j] = inf : m[i][j] = v;
		}
	}
	prim();
	cout << prim() << endl;
	return 0;
}

C++: #include<iostream>
using namespace std;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int MAXN = 100;
static const int INTFY = (1 << 21);

int n, M[MAXN][MAXN];

int prim()
{
	int u, minv;
	int d[MAXN], p[MAXN], color[MAXN];
	
	for(int i = 0; i < n; i++)
	{
		d[i] = INTFY;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	
	while(true)
	{
		minv = INTFY;
		u = -1;
		for(int i = 0; i < n; i++)
		{
			if(minv > d[i] && color[i] != BLACK)
			{
				u = i;
				minv = d[i];
			}
		}

		if(u == -1) break;
		color[u] = BLACK;

		for(int v = 0; v < n; v++)
		{
			if(color[v] != BLACK && M[u][v] != INTFY)
			{
				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;
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++) 
		{
			int e;
			cin >> e;
			M[i][j] = (e == -1)? INTFY : e; 
		}
	}
	cout << prim() << endl;
	
	return 0;
}