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>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[10000][1261],ch[10000][1261],b,c,d,e;
		r(i,0,p)r(j,0,1261)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;j>=0;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}
		}
		cin>>q;
		while(q--){
			int s=0;
			cin>>b>>c>>d;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, n) for (int i = 0; i < n; i++)
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dxx[8] = {1, 1, 1, 0, 0, -1, -1, -1}, dyy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int n;
const int MAX = 100;
int g[MAX][MAX];

int prim() {
    int u, minv;
    int d[MAX], p[MAX];
    bool seen[MAX] = {};
    rep (i, n) {
        d[i] = inf;
        p[i] = -1;
    }
    d[0] = 0;
    while (1) {
        minv = inf;
        u = -1;
        rep (v, n) {
            if (!seen[v] && minv > d[v]) {
                u = v;
                minv = d[v];
            }
        }
        if (u == -1) break;
        seen[u] = 1;
        rep (i, n) {
            if (!seen[i] && g[u][i] != -1) {
                if (d[i] > g[u][i]) {
                    d[i] = g[u][i];
                    p[i] = u;
                }
            }
        }
    }
    int sum = 0;
    rep (i, n) sum += d[i];
    // rep (i, n) cout << d[i] << " ";
    return sum;
}

int main() {
    cin >> n;
    rep (i, n) rep (j, n) cin >> g[i][j];
    // cout << prim << "\n";
    cout << prim() << "\n";
}

C++: #include <stdio.h>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    
    d[0] = 0;
    
    while(1) {
        int u;
        int mincost = INFTY;
        for (int i=0; i<n; i++) {
            if (color[i] != BLACK && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        
        if (mincost == INFTY) break;
        
        color[u] = BLACK;
        
        for (int v=0; v<=n; v++) {
            if (color[v] != BLACK && M[u][v] != INFTY) {
                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() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            int tmp;
            scanf("%d", &tmp);
            M[i][j] = (tmp == -1) ? INFTY : tmp;
        }
    }
    
    int sum = prim();
    printf("%d\n", sum);
    
    return 0;
}

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

using namespace std;

#define N 101

int main(){

  int n;

  int g[N][N];
  int min=2000;
  int weight[N];
  int sum=0;
  //load n
  cin >>n;

  //load data
  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      scanf("%d",&g[i][j]);
    }
  }

  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      if(g[i][j]!=-1 &&g[i][j]<min) min=g[i][j];
      weight[i]=min;
    }
  }

  for(int i=1;i<=n;i++){
    sum+=weight[i];
  }

  cout <<sum<<endl;

  return 0;

}

C++: #include <bits/stdc++.h>
#define rep(i,a) for(i=0;i<a;i++)
using namespace std;
static const int W = 0;
static const int G = 1;
static const int B = 2;

void prim(int n, int &total,vector<vector<int> > &M) {
    int i,u,min;
    vector<int> d(n);
    vector<int> p(n);
    vector<int> color(n);

    rep(i,n){
        d[i] = INT_MAX;
        p[i] = -1;
        color[i] = W;
    }
    d[0] = 0;
    while(true){
        min = INT_MAX;
        u = -1;
        rep(i,n){
            if(min > d[i] && color[i] != B) {
                u = i;
                min = d[i];
            }
        }
        if(u==-1) break;
        color[u] = B;
        rep(i,n){
            if(color[i] != B && M[u][i]){
                if(d[i] > M[u][i]) {
                    d[i] = M[u][i];
                    p[i] = u;
                    color[i] = G;
                }
            }
        }
    }
    rep(i,n){
        if(p[i] != -1) total += M[i][p[i]];
    }
}

int main(void) {
    int i,j,n,total = 0;
    vector<vector<int> > M;
    cin >> n;
    M.resize(n);

    rep(i,n){
        M[i].resize(n);
    }
    rep(i,n){
        rep(j,n){
            cin >> M[i][j];
            if(M[i][j] == -1) M[i][j] = INT_MAX;
        }
    }
    prim(n,total,M);
    cout << total << endl;
    return 0;
}