Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

C++: #include <iostream>
#include <algorithm>
#include <tuple>

using namespace std;

tuple<int,int,int> a[10010];
int t[101];
int n,w;
int k=0;
int x,y;

int root(int x)
{
     if(t[x]==x)
     {
          return x;
     }
     else
     {
          t[x]=root(t[x]);
          return t[x];
     }
}

void unite(int x,int y)
{
     t[root(x)]=root(y);
}

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

int main()
{
     cin >> n;
     for(int i=0;i<n;i++)
     {
          t[i]=i;
          for(int j=0;j<n;j++)
          {
               cin >> w;
               if(w==-1)
               {
                    continue;
               }
               a[k]=make_tuple(w,i,j);
               k+=1;
          }
     }
     sort(a,a+k);
     w=0;
     for(int i=0;i<k;i++)
     {
          x=get<1>(a[i]);
          y=get<2>(a[i]);
          if(same(x,y))
          {
               continue;
          }
          unite(x,y);
          w+=get<0>(a[i]);
     }
     cout << w << endl;
     return 0;
}

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

using namespace std;

typedef struct b {
	int dis;
	int node1;
	int node2;
}b;

bool cmp(b n1, b n2) {
	return n1.dis < n2.dis;
}

int main() {
	int n;
	cin >> n; 
	int graph[101][101];
	b bs[10000];
	int bCount = 0;
	int ifs[101];
	for (int i = 0; i < n; ++i) {
		ifs[i] = 0;
		for (int j = 0; j < n; ++j) {
			cin >> graph[i][j];
			if (graph[i][j] == -1) {
				continue;
			}
			else if(i>j){
				bs[bCount].dis= graph[i][j];
				bs[bCount].node1 = i;
				bs[bCount++].node2 = j;
			}
		}
	}

	sort(bs, bs + bCount, cmp);

	long totalDis = 0;
	int k = 1;
	for (int i = 0; i < bCount; ++i) {
		b b1 = bs[i];
		if (ifs[b1.node1]>0 && ifs[b1.node2]==ifs[b1.node1]) {
			continue;
		}

		totalDis += b1.dis;
		if (ifs[b1.node1] == 0 && ifs[b1.node2] == 0) {
			ifs[b1.node1] = k;
			ifs[b1.node2] = k++;
		}
		else if (ifs[b1.node1] == 0) {
			ifs[b1.node1] = ifs[b1.node2];
		}
		else if (ifs[b1.node2] == 0) {
			ifs[b1.node2] = ifs[b1.node1];
		}
		else {
			for (int j = 0; j < n; ++j) {
				if (ifs[j] == ifs[b1.node2] && j != b1.node2) {
					ifs[j] = ifs[b1.node1];
				}
			}
			ifs[b1.node2] = ifs[b1.node1];
		}
	}

	cout << totalDis << endl;
	return 0;

}

C++: #include <stdio.h>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 0;
static const int BLACK = 0;

int n, M[MAX][MAX];

int prim() {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    d[0] = 0;
    
    while(1) {
        int u=-1;
        int mincost = INFTY;
        for (int i=0; i<n; i++) {
            if (color[i] != BLACK && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        
        if (mincost == INFTY) break;
        
        color[u] = BLACK;
        
        for (int v=0; v<=n; v++) {
            if (color[v] != BLACK && M[u][v] != INFTY) {
                if(M[u][v] < d[v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    
    int sum = 0;
    for (int i=0; i<n; i++) {
        if(p[i] != -1) sum += M[i][p[i]];
    }
    return sum;
}

int main() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            int tmp;
            scanf("%d", &tmp);
            M[i][j] = (tmp == -1) ? INFTY : tmp;
        }
    }
    
    int sum = prim();
    printf("%d\n", sum);
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];

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

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) {
      if ( minv > d[i] && color[i] != BLACK ) {
        u = i;
        minv = d[i];
      }
    }
    if ( u == -1 ) break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if ( color[v] != BLACK && M[u][v] != INFTY ) {
        if ( d[v] > M[u][v] ) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) sum += M[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;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;

}

C++: // Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <iostream>
using namespace std;
const static int NMAX = 100;
const static int VMAX = 2000;
const static int INF = VMAX + 1;
int n, G[NMAX][NMAX];

// prim's algorithm for Minimum Spanning Tree (MST)
int prim() {
	bool state[NMAX];
	int distance[NMAX];
	for (int i = 0; i < n; i++) {
		state[i] = false;
		distance[i] = INF;
	}
	distance[0] = 0;

	while (true) {
		int u, mincost = INF;
		for (int i = 0; i < n; i++) {
			if (!state[i] && (distance[i] < mincost)) {
				u = i;
				mincost = distance[i];
			}
		}
		if (mincost == INF)	break;
		state[u] = true;

		for (int i = 0; i < n; i++) {
			if (!state[i] && (G[u][i] < distance[i])) {
				distance[i] = G[u][i];
			}
		}
	}

	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (distance[i] != INF) sum += distance[i];
	}

	return sum;
}

int main(void) {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> G[i][j];
			if (G[i][j] == -1) G[i][j] = INF;
		}
	}

	cout << prim() << endl;

	return 0;
}