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 <iostream>
 
using namespace std;
static const int inf = 10000;
static const int emp = -1;
static const int black = 1;
static const int white = 0;
 
int   graph[124][124] = {{}};;
int d[124] = {};
int color[124] = {};
int p[124] = {};
int n;
 
int  prim(){
 
  for(int i = 0;i < n;i++){
    d[i] = inf;
    color[i] = white;
    p[i] = -1;
  }
 
  d[0] = 0;
  int u ,v;
  
 
  while(1){
 
    v = inf;
    u = -1;
 
    for(int i = 0;i < n;i++){
      if(d[i] < v && color[i] != black){
    v= d[i];
    u = i;
      }
    }
    //    cout << "u = " << u << endl; 
 
    if(u == -1) break;
    color[u] = black;
 
    // cout << 'a' << endl;
    for(int i = 0;i < n;i++){
      if(graph[u][i] != -1 && color[i] != black){
    if(graph[u][i] < d[i]){
      d[i] = graph[u][i];
      p[i]= u;
      //  cout << "d = " << d[i] <<endl;
      // cout << "i = " << i << endl;
    }
      }
    }
  }
 
  int sum = 0;
  for(int i = 0;i < n;i++){
    if(p[i] != -1) sum += graph[i][p[i]];
  }
  return sum;
}
 
 
 
int main(){
 
  cin >> n;
 
  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      cin >> graph[i][j];
    }
  }
 
 
 
  cout <<  prim() << endl;
 
  return 0;
}

C++: #include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include<cstdio>
#include <vector>
#include<queue>
#include<limits.h>
#include<string.h>
#include<functional>
#include<map>
#include<set>
#include<list>
#include<unordered_map>
#include<queue>
#define P pair<int,int>
using namespace std;

int a[100][100];
bool used[100];
signed main(){
	int b; cin >> b;
	for (int c = 0; c < b; c++) {
		for (int d = 0; d < b; d++) {
			cin >> a[c][d];
			if (a[c][d] == -1) {
				a[c][d] = INT_MAX / 3;
			}
		}
	}
	int ans = 0;
	priority_queue<P,vector<P>, greater<P>>Q;
	Q.push({ 0,0 });
	while (Q.size()) {
		P t = Q.top(); Q.pop();
		if (!used[t.second]) {
			ans += t.first;
			used[t.second] = true;
			for (int i = 0; i < b; i++) {
				Q.push({ a[t.second][i],i });
			}
		}
	}
	cout << ans << endl;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <cfloat>
#include <math.h>
#include <numeric>
#include <string.h>
#include <sys/time.h>
#include <random>


#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;


class Graph{
    struct edge{
        int vertice;
        ll weight;
    };

    vector<vector<edge>> g;
    unsigned int n;

public:
    Graph(unsigned int n): n{n}{
        g.resize(n);
    }

    void add_edge(int u, int v){
        g[u].emplace_back(edge{v, 1});
    };

    void add_edge(int u, int v, ll w){
        g[u].emplace_back(edge{v, w});
    }

    vector<edge>& get_edge(int v){
        return g[v];
    }

    unsigned int get_num_vertices() const{
        return n;
    }

    unsigned int size() const{
        return n;
    }

    vector<edge>& operator[](int v){
        return get_edge(v);
    }
};


/**
 * prim法 (O(|E|log(|V|))のアルゴリズム)
 * @param g 連結グラフ
 * @return 連結グラフの最小域木をグラフとして返す
 */
Graph prim2_g(Graph g){
    // Ptmp.fsが今の部分木からのコスト、Ptmp.ｓｃ.fsが次の頂点、Ptmp.sc.scが次の頂点に辺を伸ばす部分木の頂点
    typedef pair<ll, pair<int, int>> Ptmp;

    unsigned int n = g.size();

    Graph mst(n);   // minimum spanning tree
    vector<ll> used(n, false);
    priority_queue<Ptmp, vector<Ptmp>, greater<Ptmp> > que;

    used[0] = true;
    for(auto e: g[0]){
        que.push(Ptmp(e.weight, pair<int, int>(e.vertice, 0)));
    }

    while(!que.empty()){
        Ptmp p = que.top(); que.pop();
        int to = p.second.first, from = p.second.second;
        if(used[to]) continue;

        used[to] = true;
        mst.add_edge(from, to, p.first);
        mst.add_edge(to, from, p.first);

        for(auto e: g[to]){
            if(!used[e.vertice]){
                que.push(Ptmp(e.weight, pair<int, int>(e.vertice, to)));
            }
        }
    }

    return mst;
}


ll prim2(Graph g){
    unsigned int n = g.size();
    Graph mst = prim2_g(g);

    ll res = 0;
    for(int i = 0; i < n; i++){
        for(auto e: mst[i]){
            res += e.weight;
        }
    }

    return res / 2;
}



int main(){
    int n;   cin >> n;
    Graph g(n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int x;  cin >> x;
            if(x != -1){
                g.add_edge(i, j, x);
            }
        }
    }

    cout << prim2(g) << endl;

    return 0;
}