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;
#include<stdio.h>

class union_find  // union find.
{
    int* U;
public:
    union_find(int n){ U = new int [n + 1]; for(int i = 1; i <= n; i++) U[i] = i; }
    int root(int a){ if(U[a] == a){ return a; } U[a] = root(U[a]); return U[a]; }
    bool same(int a, int b){ return root(a) == root(b); }
    void join(int a, int b){ int x = root(a), y = root(b); U[x] = y; }
    ~union_find(){ delete [] U; }
};

struct edge{ int v1, v2; int weight; };  // edge.

edge init(){
    edge e;
    e.v1 = 0; e.v2 = 0; e.weight = -1;
    return e;
}

edge init(int a, int b, int w){
    edge e;
    e.v1 = a; e.v2 = b; e.weight = w;
    return e;
}

void swap_e(edge e1, edge e2)
{
    swap(e1.v1, e2.v1); swap(e1.v2, e2.v2); swap(e1.weight, e2.weight);
}

class edge_heap  // heap.
{
    edge* E;
    int edge_n;
public:
    edge_heap(int n){ E = new edge [n + 1]; edge_n = 0; }
    void min_heap(int i);
    void insert_heap(edge e);   // sorting.
    void insert_nl(edge e);     // no sorting.
    edge delete_min();
    bool isEmpty(){ return edge_n == 0; }
    int total_weight();
    ~edge_heap(){ delete [] E; }
};

void edge_heap::min_heap(int i)
{
    int l = 2 * i, r = 2 * i + 1;
    if(l > edge_n && r > edge_n) return;
    int minid = i;
    if(l <= edge_n){ if(E[l].weight < E[minid].weight) minid = l; }
    if(r <= edge_n){ if(E[r].weight < E[minid].weight) minid = r; }
    if(minid == i) return;
    swap(E[minid], E[i]);
    min_heap(minid);
}

void edge_heap::insert_heap(edge e)
{
    edge_n++;
    int w = e.weight;
    int i = edge_n;
    while(i > 1){
        if(w < E[i / 2].weight){
        E[i] = E[i / 2];
        }else{ break; }
        i /= 2;
    };
    E[i] = e;
}
void edge_heap::insert_nl(edge e){ edge_n++; E[edge_n] = e; }

edge edge_heap::delete_min()
{
    edge e = E[1];
    E[1] = E[edge_n]; edge_n--;
    min_heap(1);
    return e;
}

int edge_heap::total_weight()
{
    int tw = 0;
    for(int i = 1; i <= edge_n; i++) tw += E[i].weight;
    return tw;
}

int main()
{
    int n, i, j, w;
    scanf("%d", &n);
    edge_heap EH(n * (n + 1) / 2); // All Edge heap.
    edge e;

    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++){
            scanf("%d", &w);
            if(j <= i || w < 0) continue;
            e = init(i, j, w);
            EH.insert_heap(e);
        }
    }

    edge_heap TH(n);   // Tree heap.

    union_find VG(n);  // vertex group.

    while(!EH.isEmpty()){
        e = EH.delete_min();
        if(!VG.same(e.v1, e.v2)){
            VG.join(e.v1, e.v2);
            TH.insert_nl(e);
        };
    };

    printf("%d", TH.total_weight());

    return 0;
}

C++: #include <iostream>
#include <utility>
#include <algorithm>
#define MAX 150
#define rep(i,p) for(int i=0;i<p;i++)
#define rep1(i,p) for(int i=1;i<=p;i++)
using namespace std;
int n,temp,a,b,size,ra,rb;
int A[MAX];
int B[MAX];
int W[MAX];
int V[MAX];
pair<int,int> edge[MAX];
int root (int child) {
  int a = child;
  while (V[a] != a) {
    a = V[a];
  }
  return a;
}
int main(){
  size = 0;
  cin >> n;
  rep(i,n) {
    rep(j,n) {
      cin >> W[size];
      if (i <= j && W[size] != -1) { 
        A[size] = i;
        B[size] = j;        
        edge[size].first = W[size];
        edge[size].second = size;
        size++;
      }
    }
  }
  // cout << endl;
  rep(i,size) {
    cout << A[edge[i].second] << " " << B[edge[i].second]
         << " " << edge[i].first << endl;;
  }
  // sort by lenght of edge
  sort(edge, edge+size);
  
  int sum = 0;
  rep(i,n) V[i] = i;
  rep(i,size) {
    // if (edge[i].first == -1) continue;
    temp = edge[i].second;
    // a , b : vertex
    a = A[temp];
    b = B[temp];
    ra = root(a);
    rb = root(b);
    if (ra != rb) {
      // ?????????????????????
      sum += edge[i].first;
      V[ra] = rb;
      // if (ra != a && rb == b){     
      //   V[b] = ra;
      // } else if (ra == a && rb != b) {
      //   V[a] = rb;
      // } else if (ra == a && rb == b) {        
      //   V[a] = b;
      // } else if (ra != a && rb != b) {                
      //   V[ra] = rb;
      // }
    }
  }
  cout << sum << endl;
  return 0;
}