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 <bits/stdc++.h>
using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind() {}
  UnionFind(int n) : data(n, -1) {}
  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 rank(int x) { return -data[root(x)]; }
  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;
  }
};
template<typename T> struct Graph {
  struct Edge { int s, t; T cost; };
  int sz;
  vector<Edge> es;
  Graph(int n = 0) : sz(n) {}

  void addEdge(int u, int v, T w) {
    sz = max(sz, max(u, v) + 1);
    es.push_back({u, v, w});
  }
  T kruskal() {
    UnionFind uf(sz);
    sort(begin(es), end(es), [](Edge e1, Edge e2) {
      return e1.cost < e2.cost;
    });
    T res = 0;
    for (auto&& e : es) {
      if (uf.unite(e.s, e.t)) res += e.cost;
    }
    return res;
  }
};

int main() {
  int n; cin >> n;
  vector<vector<int>> table(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> table[i][j];
    }
  }
  Graph<int> g;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (table[i][j] != -1) g.addEdge(i, j, table[i][j]);
    }
  }
  cout << g.kruskal() << endl;
  return 0;
}

C++: #include <iostream>
using namespace std;

int n;
int M[100][100];

int prim(){
  int d[100]; //最小コスト
  int color[100];
  int p[100];
  for(int i=0;i<n;i++){
    d[i] = (1 << 21);
    color[i] = 0;
    p[i] = -1;
  }
  d[0] = 0;
  p[0] = 0;

  int mincost;
  int u;
  int cost = 0;

  while(true){
    mincost = (1 << 21);
    for(int i=0;i<n;i++){
      if(color[i] != 2 && d[i] < mincost){
        mincost = d[i];
        u = i;
      }
    }

    if(mincost == (1 << 21)) break;

    color[u] = 2;
    cost += mincost;

    // uと隣接する頂点についてdの更新
    for(int i=0;i<n;i++){
      if(M[u][i] != -1 && color[i] != 2 && M[u][i] < d[i]){
        d[i] = M[u][i];
        p[i] = u;
        color[i] = 1;
      }
    }
  }
  return cost;
}

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin >> M[i][j];
    }
  }
  cout << prim() << endl;
  return 0;
}

C++: #include<iostream>
using namespace std;

static const int N=100;
static const int INFTY=2001;
static const int WHITE=0;//未探索
static const int GRAY=1;//MSTに隣接
static const int BLACK=2;//MSTに含まれる

int n,v,M[N][N];

int prim(){
    int sum_weight; //MSTの辺の重みの総和
    int cost[N]; //MSTから頂点に伸びている辺の重みの最小値をその頂点に保持する
    int color[N]; //MSTか、MSTに隣接しているか、それ以外か
    //初期化
    for(int i=0;i<n;i++){
        cost[i]=INFTY;
        color[i]=WHITE;
    }
    cost[0]=0;
    color[0]=GRAY;
    sum_weight=0;

    while(true){
        int vertex=-1;//次に含む頂点の候補
        int min_cost=INFTY;//次に選ぶ辺の最小コスト
        for(int i=0;i<n;i++){
            //MSTに隣接した頂点から最小コストのものを探す
            if(color[i]==GRAY&&cost[i]<min_cost){
                min_cost=cost[i];
                vertex=i;
            }
        }
        //T(部分グラフ)＝Vなら終了
        if(vertex==-1) break;
        //該当の頂点がある場合
        color[vertex]=BLACK;
        sum_weight+=cost[vertex];

        //vertexをMSTに含めたために、それによる他の頂点の最小コストの変化と状態を更新
        for(int i=0;i<n;i++){
            if(color[i]!=BLACK&&M[vertex][i]<INFTY){
                color[i]=GRAY;
                if(cost[i]>M[vertex][i]) cost[i]=M[vertex][i];
            }
        }
    }
    return sum_weight;
}

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>v;
            if(v==-1) M[i][j]=INFTY;
            else M[i][j]=v;
        }
    }

    cout<<prim()<<endl;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
const int num = 10000;
const int INF = 2000000000;
int c[num];
int pe[num];
int d[num];
typedef pair<int, int> p;
vector <p> v[num];
int main() {
	int n;
	cin >> n;
	fill(pe, pe + n, INF);
	fill(c, c + n, 0);
	fill(d, d + n, INF);
	d[0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int w; cin >> w;
			if (w != -1) v[i].push_back(p(w, j));
		}
	}
	priority_queue <p, vector<p>, greater<p>> q;
	for (int i = 0; i < v[0].size(); i++) {
		q.push(p(v[0][i].first, v[0][i].second));
		d[v[0][i].second] = v[0][i].first;
		pe[v[0][i].second] = 0;
	}
	c[0] = 1;
	while (!q.empty()) {
		int f = q.top().first;
		int s = q.top().second;
		q.pop();
		c[s] = 1;
		if (d[s] < f) continue;
		for (int i = 0; i < v[s].size(); i++) {
			int fi = v[s][i].first;
			int se = v[s][i].second;
			if (c[se]) continue;
			if (pe[se] == INF || d[se] > fi) {
				d[se] = fi;
				pe[se] = s;
				q.push(p(fi, se));
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++)
		sum += d[i];
	cout << sum << endl;
	return 0;
}