Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

C++: #include<iostream>
using namespace std;

const int MAX = 100, INF = 1e9, white = 0, gray = 1, black = 2;
int G[MAX][MAX], color[MAX], d[MAX], p[MAX], n;

int prim(){
    int u, minv;
    for (int i = 0; i < n; i++)
    {
        d[i] = INF;
        p[i] = -1;
        color[i] = white;
    }
    d[0] = 0;
    while (true)
    {
        minv = INF;
        u = -1;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != black && d[i] < minv)
            {
                minv = d[i];
                u = i;
            }
        }
        if (u == -1)
        {
            break;
        }
        color[u] = black;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != black && G[u][i] != INF)
            {
                if (d[i] > G[u][i])
                {
                    d[i] = G[u][i];
                    p[i] = u;
                    color[i] = gray;
                }
                
            }
            
        }
        
    }
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        if (p[i] != -1){
            sum += G[i][p[i]];
        }
    }
    return sum;
}

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

C++: #include <iostream>
#include <algorithm>
using namespace std;
int P[10000];
void init(int N) {
  for (int i = 0; i < N; i++) {
    P[i] = i;
  }
}

int root(int a) {
  if(P[a] == a)
    return a;
  return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b) {
  return root(a) == root(b);
}

void unite(int a, int b) {
  P[root(a)] = root(b);
}

int main()
{
  int N;
  cin >> N;
  int A[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int x;
      cin >> x;
      A[j][i] = x;
      // cout << "cost: " << x << " = " <<  j << "->"  << i << endl;
    }
  }
  // cout << "-----エッジ-------" << endl;
  int cost[10000], src[10000], dst[10000];
  int M = 0;
  for (int r = 0; r < N; r++)
  {
    for (int c = r + 1; c < N; c++)
    {
      if (A[r][c] > -1)
      {
        cost[M] = A[r][c];
        src[M] = r;
        dst[M] = c;
        // cout << "cost: " << cost[M] << " = " <<  src[M] << "->"  << dst[M] << endl;
        M += 1;
      }
    }
  }
  // cout << "M = " << M << " N = " << N << endl;
  init(N);
  pair<int, pair<int, int> > edges[M + 1];
  for (int m = 0; m < M; m++)
  {
    edges[m] = make_pair(cost[m], make_pair(src[m], dst[m]));
  }
  // cout << "-----sort前-------" << endl;
  // for (int i = 0; i < M; i++)
  //     cout << "cost: " << edges[i].first << " = " << edges[i].second.first << "->"  << edges[i].second.second << endl;
  sort(edges, edges+M);
  // cout << "-----sort後-------" << endl;
  // for (int i = 0; i < M; i++)
  //     cout << "cost: " << edges[i].first << " = " << edges[i].second.first << "->"  << edges[i].second.second << endl;
  int sum = 0;
  for (int i=0; i<M; i++) {
    if (is_same_set(edges[i].second.first, edges[i].second.second))
      continue;
    unite(edges[i].second.first, edges[i].second.second);
    sum += edges[i].first;
  }
  cout << sum << endl;
}