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>
#include<vector>
#include<queue>

using namespace std;
using Graph=vector<vector<int>>;



struct Edge {
	int node1, node2, weight;

	Edge() {}

	Edge(int n1, int n2, int w) :node1(n1), node2(n2), weight(w) {}

	void numset(int n1, int n2, int w) {
		node1 = n1; node2 = n2; weight = w;
	}

};

void eswap(Edge& e1, Edge& e2) {
	Edge temp = e1;   e1 = e2;  e2 = temp;
}

void bubbleSort(vector<Edge>& edge) {
	bool flag;
	for (int i = 0; i < edge.size(); i++) {
		flag = true;
		for (int j = 0; j < edge.size()-i-1; j++) {
			if (edge.at(j).weight > edge.at(j + 1).weight) {
				eswap(edge.at(j), edge.at(j + 1));
				flag = false;
			}
		}
		if (flag) break;
	}
}

int kruskal(Graph &spntree,vector<Edge>& edgeset) {
	int size = spntree.size();
	queue<int> unconnected;
	vector<bool> connected(size,false);
	
	if (edgeset.size() == 0) return 0;
	bubbleSort(edgeset);

	connected.at(edgeset.at(0).node1)=true;
	connected.at(edgeset.at(0).node2) = true;
	spntree.at(edgeset.at(0).node1).push_back(edgeset.at(0).node2);
	spntree.at(edgeset.at(0).node2).push_back(edgeset.at(0).node1);
	int index=0,count = 2,cost=edgeset.at(0).weight;
	Edge ed;
	edgeset.erase(edgeset.begin());

	while (count<size) {
		ed = edgeset.at(index);
		if (connected.at(ed.node1) == true && connected.at(ed.node2) == false) {
			connected.at(ed.node2) = true;
			spntree.at(ed.node1).push_back(ed.node2);
			spntree.at(ed.node2).push_back(ed.node1);
			edgeset.erase(edgeset.begin() + index);
			count++; cost += ed.weight;
			index = 0; continue;
		}
		else if (connected.at(ed.node2) == true && connected.at(ed.node1) == false) {
			connected.at(ed.node1) = true;
			spntree.at(ed.node1).push_back(ed.node2);
			spntree.at(ed.node2).push_back(ed.node1);
			edgeset.erase(edgeset.begin() + index);
			count++; cost += ed.weight;
			index = 0; continue;
		}
		index++;
	}
	return cost;


}

int main(void) {

	int size, temp,cost,edgenum=0;
	Edge etemp;
	vector<Edge> edgeset;
	cin >> size;
	Graph spntree(size);
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size; j++) {
			cin >> temp;
			if (i < j) {
				if (temp != -1) {
					edgenum++;
					etemp.numset(i, j, temp);
					edgeset.push_back(etemp);
				}
			}
		}
	}

	cost=kruskal(spntree, edgeset);
	cout << cost << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxx = 1010;
const int INF = 1 << 30;
int n, m[maxx][maxx];
int u, num, v;
int flag[maxx];

int prim() {
	// d??°????????????V-T??? p??°????????°???MST???
	int d[maxx], p[maxx];
	for (int i = 0; i < n; i++) {
		d[i] = INF;
		p[i] = -1;
		flag[i] = 0;
	}

	d[0] = 0;
	while (1) {
		int minv = INF;
		u = -1;
		// ???????????????????°???????
		for (int i = 0; i < n; i++) {
			if (minv > d[i] && flag[i] == 0) {
				u = i;
				minv = d[i];
			}
		}

		if (u == -1)
			break;

		// ?????°???????????\MST
		flag[u] = 1;

		// ??´??°?????????????????????????????????????????´???????°???????????????????????????????
		for (int v = 0; v < n; v++) {
			if (flag[v] == 0 && m[u][v] != INF && d[v] > m[u][v]) {
				d[v] = m[u][v];
				p[v] = u;
			}
		}
	}


	int ans = 0;
	for (int i = 0; i < n; i++)
		if (p[i] != -1)
			ans += m[i][p[i]];

	return ans;
}
int main()
{
	memset(flag, 0, sizeof(flag));
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			scanf("%d", &m[i][j]);
			if (m[i][j] == -1)
				m[i][j] = INF;
		}
	}
	printf("%d\n", prim());
	return 0;
}

C++: #include <iostream>
#include <array>
#include <vector>
#include <algorithm>

int n;
static const int nmax = 2005;
static const int INFTY = (1 << 21);
static const int WHITE = 0; // Not visited
static const int GRAY = 1;  // Visited
static const int BLACK = 2; // In MST
std::array<std::array<int, nmax>, nmax> adjmat;
std::array<int, nmax> d, p;
std::array<int, nmax> color;

void prim()
{
  color.fill(WHITE);
  d.fill(INFTY);
  p.fill(-1);

  int v = 0;
  d[v] = 0;
  color[v] = GRAY;

  while (true)
    {
      auto mincost = INFTY;

      // Find minimum cost vertex from current MST
      for (int i=0; i<n; ++i)
        {
          if (color[i] != BLACK && d[i] < mincost)
            {
              mincost = d[i];
              v = i;
            }
        }

      // Break if no update
      if (mincost == INFTY) break;

      // Include v in MST
      color[v] = BLACK;

      // Update minimum cost outside current MST
      for (int i=0; i<n; ++i)
        {
          if (color[i] != BLACK && adjmat[v][i] != -1 && adjmat[v][i] < d[i])
            {
              d[i] = adjmat[v][i];
              color[i] = GRAY;
              p[i] = v;
            }
        }
    }
}

int main ()
{
  std::cin >> n;
  for (int i=0; i<n; ++i)
    {
      for (int j=0; j<n; ++j)
        std::cin >> adjmat[i][j];
    }

  prim();

  int ans = 0;
  //std::for_each(d.begin(), d.begin()+n, [&](auto x){ans+=x;});
  for (int i=0; i<n; ++i)
    {
      if (p[i] != -1)
        ans += adjmat[i][p[i]];
    }
  std::cout << ans << std::endl;
}