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 rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;


struct Unionfind{
    vector<int> parents;
    int n;
    Unionfind(int n):n(n),parents(n,-1){ }

    int find(int x){
        if(parents[x] < 0) return x;
        else return parents[x] = find(parents[x]);
    }

    void unite(int x,int y){
        x = find(x);
        y = find(y);

        if(x==y) return;
        if(parents[x] > parents[y]) swap(x,y);
        parents[x] += parents[y];
        parents[y] = x;
    }  

   int size(int x){
       return -parents[find(x)];
   }

   bool same(int x,int y){
       return find(x) == find(y);
   }

   vector<int> members(int x){
       int root = find(x);
       vector<int> member;
       for(int i=0;i<n;i++){
           if(find(i)==root){
               member.push_back(i);
           }
       }
      return member;
  }

  int group_cnt(){
      int c = 0;
      rep(i,n){
          if(parents[i] < 0) ++c;
      }
      return c;
  }
};

struct edge{
    int from,to,cost;  
};

int main(){
    int n;
    cin >> n;
    vector<edge> eg;
    rep(i,n)rep(j,n){
        int a;
        cin >> a;
        if(i > j) continue;
        if(a==-1) continue;
        eg.push_back(edge{i,j,a});
    }

    sort(eg.begin(),eg.end(),[](const edge &l,const edge &r){
        return l.cost < r.cost;
    });

    Unionfind uf(n);
    int ans = 0;

    rep(i,eg.size()){
        if(uf.same(eg[i].from,eg[i].to)) continue;
        uf.unite(eg[i].from,eg[i].to);
        ans += eg[i].cost;
    }
    
    cout << ans << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define INF 1000000
#define N_MAX 100
#define WH 0
#define GR 1
#define BK 2

int main() {
    int n;
    cin >> n;
    int A[N_MAX][N_MAX] = {};
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int a;
            cin >> a;
            if (a == -1) {
                A[i][j] = INF;
            }
            else {
                A[i][j] = a;
            }
        }
    }

    int color[N_MAX] = {};
    int d[N_MAX];
    for (int i = 0; i < n; i++) {
        d[i] = INF;
    }
    int p[N_MAX] = {};

    while (true) {
        int u;
        int min_cost = INF;
        d[0] = 0;
        p[0] = -1;

        for (int i = 0; i < n; i++) {
            if (color[i] != BK && d[i] < min_cost) {
                min_cost = d[i];
                u = i;
            }
        }
        if (min_cost == INF) {
            break;
        }
        color[u] = BK;

        for (int v = 0; v < n; v++) {
            if (color[v] != BK && A[u][v] != INF) {
                if (A[u][v] < d[v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GR;
                }
            }
        }
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans += d[i];
    }
    cout << ans << endl;

    return 0;
}

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

using namespace std;

const int NIL = 2001;
const int MAX = 100;
int G[MAX][MAX];
int W[MAX];
int n;

void MST() {
	priority_queue<pair<int, int>> PQ;
	PQ.push(make_pair(0, 0));
	W[0] = 0;
	while (!PQ.empty()) {
		int u = PQ.top().second;
		PQ.pop();
		for (int i = 0; i < n; ++i) {
			if (G[u][i] != -1 && G[u][i] < W[i]) {
				PQ.push(make_pair(G[u][i] * -1, i));
				W[i] = G[u][i];
			}
		}
	}
}

int main() {

	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j)
			G[i][j] = NIL;
		W[i] = NIL;
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			int a;
			scanf("%d", &a);
			G[i][j] = a;
		}
	}

	MST();

	int sum = 0;
	for (int i = 0; i < n; ++i) {
		sum += W[i];
	}
	cout << sum << endl;

	return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;

int n;
const int INF = 100100100;

int a[105][105];

int u,minv;
int d[105],p[105],color[105];

int prim(){

  for(int i = 0; i < n; i++){
    d[i] = INF;
    p[i] = -1;
    color[i] = 0;
  }
  d[0] = 0;

  while(1){
    minv = INF;
    u = -1;
    for(int i = 0; i < n; i++){
      if(minv > d[i] && color[i] != 2){
        minv = d[i];
        u = i;
      }
    }
    if(u==-1) break;

    color[u] = 2;

    for(int i = 0; i < n; i++){
      if(color[i]!= 2 && a[u][i] != INF){
        if(d[i] > a[u][i]){
          d[i] = a[u][i];
          p[i] = u;
          color[i] = 1;
        }
      }
    }
  }
  int sum = 0;
  for(int i = 0; i < n; i++){
    if(p[i] != -1) sum+=a[i][p[i]];
  }
  return sum;
}

int main(){
  cin >> n;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      int e;
      cin >> e;
      a[i][j] = (e == -1) ? INF:e;
    }
  }
  cout << prim() << endl;
  return 0;
}