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;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
#define MAX 100
static const int INFTY=(1<<21);
typedef long long ll;
int n,u;
int color[MAX];
int m[MAX][MAX];
int d[MAX],p[MAX];
int prim(){
    rp(i,n){
        color[i]=0;d[i]=INFTY;p[i]=NIL;
    }
    d[0]=0;
    while(1){
        int mincost=INFTY;int u=-1;
        rp(i,n){
            if(color[i]!=2&&d[i]<mincost){
                mincost=d[i];
                u=i;
            }
        }
        if(u==-1)break;
        color[u]=2;
        rp(v,n){
            if(color[v]!=2&&m[u][v]!=INFTY){
                if(m[u][v]<d[v]){
                    d[v]=m[u][v];
                    p[v]=u;
                    color[v]=1;
                }
            }
        }
    }
    int sum=0;
    rp(i,n)if(p[i]!=NIL)sum+=m[i][p[i]];
    return sum;
}
int main(){
    int a;
    cin>>n;
    rp(i,n){
        rp(j,n){
            cin>>a;
            if(a!=-1)m[i][j]=a;
            else m[i][j]=INFTY;
        }
    }
    cout<<prim()<<endl;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

typedef struct edge_def {
  int from;
  int to;
  int cost;

  edge_def() {;}
  edge_def(int _from, int _to, int _cost) {
    from = _from;
    to   = _to;
    cost = _cost;
  }
} edge;

const int INF = 1000 * 1000 * 1000;
const int N = 100;
int n;
int ed[N];  //ed[i]:??????i???????°???¨?????¨?????¨?????????????????????????????????i??¨??¨?????¨??????????????????????????????
            //????°???¨?????¨????§?????????????????????????????????£????????????
int u[N];  //??¢???????°???¨?????¨???????????????????????????
vector<edge> es[N];

int prim(int s) {
  int i, j, res = 0;
  priority_queue<PII, vector<PII>, greater<PII > > q;
  memset(u, 0, sizeof(u));
  for (i=0; i<n; i++) ed[i] = INF;
  ed[s] = 0;
  q.push(PII(0, s));

  while (q.size()) {
    PII p = q.top(); q.pop();

    u[p.second] = 1;
    res += p.first;

    int m = INF;
    edge me;
    for (i=0; i<n; i++)
      for (j=0; j<(int)es[i].size(); j++) {
        edge e = es[i][j];
        if (u[e.from] && !u[e.to] && e.cost < m) {
          me = e;
          m = me.cost;
        }
      }
    if (m != INF) {
      q.push(PII(me.cost, me.to)); 
      ed[me.to] = me.cost;
    }
  }

  return res;
}


int main() {
  int i, j;
  scanf("%d ", &n);
  int c;
  for (i=0; i<n; i++)
    for (j=0; j<n; j++) {
      scanf("%d ", &c);
      if (c!=-1) es[i].psb(edge(i,j,c));
    }
  printf("%d\n", prim(0));
  return 0;
}

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

const int MAX=100;
int INFTY=1<<30;
int WHITE=0;
int GRAY=1;
int BLACK=2;

int n,m[MAX][MAX];

void prim()
{
	int u,minv;
	int d[MAX],p[MAX],color[MAX];
	for(int i=0;i<n;i++){
		d[i]=INFTY;
		p[i]=-1;
		color[i]=WHITE;
	}
	d[0]=0;
	while(1)
	{
		minv=INFTY;
		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]!=INFTY)
			if(d[v]>m[u][v]){
				p[v]=u;
				d[v]=m[u][v];
				color[v]=GRAY;
			}
	}
	int sum=0;
	for(int i=0;i<n;i++)
	if(p[i]!=-i) sum+=m[i][p[i]];
	cout<<sum<<endl;
}

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)? INFTY:e;
		}
	}
	prim();
	return 0;
}