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;

const int MAX = 100;
const int INFTY = (1 << 21);

class MST
{
    int num, G[MAX][MAX], status[MAX];

public:
    MST()
    {
        int value;
        cin >> num;
        for (int i = 0; i < num; i++)
        {
            for (int j = 0; j < num; j++)
            {
                cin >> value;
                G[i][j] = value == -1 ? INFTY : value;
            }
        }
        for (int i = 0; i < num; i++)
        {
            status[i] = 0;
        }
    }

    int prim(int node)
    {
        int min_index1, min_index2, sum = 0;
        status[node] = 1;
        for (int j = 0; j < num-1; j++)
        {
            min_index1 = min_index2 = 0;
            for (int n = 0; n < num; n++)
            {
                if (status[n] != 0)
                {
                    for (int i = 1; i < num; i++)
                    {
                        if (status[i] == 0 && G[min_index1][min_index2] > G[n][i])
                        {
                            min_index1 = n;
                            min_index2 = i;
                        }
                    }
                }
            }
            status[min_index2] = 1;
            sum += G[min_index1][min_index2];
        }
        return sum;
    }
};

int main()
{
    MST mst;
    cout << mst.prim(0) << endl;
}

C++: #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-12;
static const double PI = acos(-1.0);

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << endl

static const ll INF = std::numeric_limits<ll>::max() / 3;
static const ll MAX_V = 100;  // TODO

ll cost[MAX_V][MAX_V];  // TODO
ll mincost[MAX_V];
bool used[MAX_V];
ll V;  // TODO

typedef pair<ll, ll> Node;

int prim() {
  priority_queue<Node, vector<Node>, greater<Node>> q;
  q.push(Node{0, 0});
  ll res = 0;
  while (!q.empty()) {
    Node node = q.top();
    q.pop();
    if (used[node.second]) {
      continue;
    }
    used[node.second] = true;
    res += node.first;
    for (ll u = 0; u < V; u++) {
      if (cost[node.second][u] != INF) {
        q.push(Node{cost[node.second][u], u});
      }
    }
  }
  return res;
}

void solve(long long n, vector<vector<long long>> a) {
  V = n;
  REP(i, n) {
    REP(j, n) {
      if (a[i][j] == -1) {
        cost[i][j] = INF;
      } else {
        cost[i][j] = a[i][j];
      }
    }
  }
  std::cout << prim() << std::endl;
}

int main() {
  long long n;
  scanf("%lld", &n);
  vector<vector<long long>> a(n, vector<ll>(n));
  REP(i, n) {
    REP(j, n) {
      scanf("%lld", &a[i][j]);
    }
  }
  solve(n, std::move(a));
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
constexpr int INF = 1<<21;
enum Color { WHITE = 0, GRAY, BLACK };
int n;
vv(int, M, MAX, MAX, INF);
inline int prim() {
	vi d(n, INF), t(n, -1), color(n, WHITE);
	d[0] = 0;
	int minv, u;
	while(true) {
		minv = INF;
		u = -1;
		rep(i, 0, n) {
			if(minv > d[i] && color[i] != BLACK) {
				u = i;
				minv = d[i];
			}
		}
		if(u == -1) break;
		color[u] = BLACK;
		rep(v, 0, n) {
			if(color[v] != BLACK && d[v] > M[u][v]) {
				d[v] = M[u][v];
				t[v] = u;
				color[v] = GRAY;
			}
		}
	}
	return accumulate(all(d), 0);
;
}
int main() {
	cin >> n;
	int e;
	rep(i, 0, n) {
		rep(j, 0, n) {
			cin >> e;
			if(e != -1) M[i][j] = e;
		}
	}
	P(prim());
	return 0;
}