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;

int n,m[100][100];

int prim();

int main(){
  cin>>n;
  
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>m[i][j];
      if(m[i][j]==-1)m[i][j]=200002;
    }
  }

  cout<<prim()<<endl;

}

int prim(){
  int u,mn;
  int d[100],p[100],ck[100];
  
  for(int i=0;i<n;i++){
    d[i]=200002;
    p[i]=-1;
    ck[i]=0;
  }

  d[0]=0;

  while(1){
    mn=200002;
    u=-1;
    for(int i=0;i<n;i++){
      if(mn>d[i] && ck[i]!=2){
	u=i;
	mn=d[i];
      }
    }
    if(u==-1)break;
    ck[u]=2;
    for(int i=0;i<n;i++){
      if(ck[i]!=2 && m[u][i]!=200002){
	if(d[i]>m[u][i]){
	  d[i]=m[u][i];
	  p[i]=u;
	  ck[i]=1;
	}
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    if(p[i]!=-1)sum+=m[i][p[i]];
  }
  return(sum);
}

C++: #include<bits/stdc++.h>
#define wgt(x) (x.first)
#define s(x) (x.second.first)
#define t(x) (x.second.second)
#define mwe(w,s,t) make_pair(w,make_pair(s,t))

using namespace std;

typedef pair<int,int> edge;
typedef pair<int,edge> we;

int main()
{
	int n,G[100][100];
	bool used[100];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			scanf("%d",&G[i][j]);
	memset(used,false,sizeof(used));
	priority_queue<we,vector<we>,greater<we> > pq;
	int tot=0;
	used[0]=true;
	for(int i=0;i<n;i++)
		if(G[0][i]>=0)
			pq.push(mwe(G[0][i],0,i));
	while(!pq.empty())
	{
		we t=pq.top();pq.pop();
		if(used[t(t)]) continue;
		tot+=wgt(t);
		used[t(t)]=true;
		for(int i=0;i<n;i++)
			if(!used[i]&&G[t(t)][i]>=0)
				pq.push(mwe(G[t(t)][i],t(t),i));
	}
	printf("%d\n",tot);
	return 0;
}

C++: #include <cstdio>
#include <limits>

static const int MAX_N   = 100;
static const int MAX_INT = std::numeric_limits<int>::max();
static const int UNKNOWN = 0;
static const int IN_MST  = 1;

int M[MAX_N][MAX_N];

// 任意の頂点uを起点としてMSTを構成する頂点集合Tを定義し、
// MSTに全ての頂点が含まれるまで、Tの周囲の辺のうち重みが
// 最小の辺を都度選択してTに加えていく。
int prim(int n) {
    int *costs   = new int[n];
    int *parents = new int[n];
    int *states  = new int[n];

    // 各変数を初期化する。
    for (int i = 0; i < n; i++) {
        costs[i]   = MAX_INT;
        parents[i] = -1;
        states[i]  = UNKNOWN;
    }

    costs[0] = 0;

    while (true) {
        int mincost = MAX_INT;
        int u = -1;

        // Tの周囲の辺のうち重みが最小の辺およびそれにつながる頂点を探す。
        for (int i = 0; i < n; i++) {
            if ( states[i] != IN_MST && costs[i] < mincost ) {
                u = i;
                mincost = costs[i];
            }
        }
        if ( u == -1 ) break;

        states[u] = IN_MST;
        for (int v = 0; v < n; v++) {
            if ( states[v] != IN_MST && M[u][v] < costs[v] ) {
                costs[v]   = M[u][v];
                parents[v] = u;
            }
        }
    }

    int total_cost = 0;
    for (int i = 0; i < n; i++) {
        if (parents[i] != -1)
            total_cost += M[i][ parents[i] ];
    }
    return total_cost;
}

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

    int cost;
    for (int u = 0; u < n; u++) {
        for (int v = 0; v < n; v++) {
            scanf("%d", &cost);
            M[u][v] = 0 <= cost ? cost : MAX_INT;
        }
    }

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

    return 0;
}

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

const long MAX = 100;
const long INFTY = (1<<21);
const long WHITE = 0;
const long GRAY = 1;
const long BLACK = 2;

long n, M[MAX][MAX];

long prim(){
	long u, minv;
	long d[MAX], p[MAX], color[MAX];

	for (long i = 0; i < n; i++){
		d[i] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
	}

	d[0] = 0;

	while (1){
		minv = INFTY;
		u = -1;
		for (long 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 (long v = 0; v < n; v++){
			if (color[v] != BLACK && M[u][v] != INFTY){
				if (d[v] > M[u][v]){
					d[v] = M[u][v];
					p[v] = u;
					color[v] = GRAY;
				}
			}
		}
	}
	int sum = 0;
	for (long i = 0; i < n; i++){
		if (p[i] != -1) sum += M[i][p[i]];
	}

	return sum;
}

int main(){
	cin >> n;

	for (long i = 0; i < n; i++){
		for (long j = 0; j < n; j++){
			long e;
			cin >> e;
			M[i][j] = (e == -1) ? INFTY : e;
		}
	}

	cout << prim() << endl;
}