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>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int NOT_VISIT = 0;
static const int VISITING = 1;
static const int VISITED = 2;
int n;
int M[MAX][MAX];
int prim()
{
    int minv, u;
    int isVisited[MAX], d[MAX], p[MAX];
    for (int i = 0; i < n; i++) {
	isVisited[i] = NOT_VISIT;
	d[i] = INFTY;
	cout << "d[i]: " << d[i] << endl;
	p[i] = -1;
    }
    d[0] = 0;
    while (1) {
	minv = INFTY;
	cout << "minv: " << minv << endl;
	u = -1;
	for (int i = 0; i < n; i++) {
	    if (minv > d[i] && isVisited[i] != VISITED) {
		u = i;
		minv = d[i];
	    }
	}
	if (u == -1) {
	    break;
	}

	isVisited[u] = VISITED;
	for (int j = 0; j < n; j++) {
	    if (isVisited[j] != VISITED && M[u][j] != INFTY) {
		if (d[j] > M[u][j]) {
		    d[j] = M[u][j];
		    p[j] = u;
		    isVisited[j] = VISITING;
		}
	    }
	}
    }
    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;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 2000000000;
int main() {
	int n; cin >> n;
	int m[101][101];
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
		cin >> m[i][j];
	int res = 0;
	int *d = new int[n];
	fill(d, d + n, INF); 
	d[0] = 0;
	bool *visit = new bool [n];
	fill(visit, visit + n, false);
	priority_queue <P, vector<P>, greater<P> > q;
	q.push(P(d[0], 0));
	while (!q.empty()) {
		int f = q.top().first, s = q.top().second;
		visit[s] = true;
		res += f;
		q.pop();
		for (int i = 0; i < n; i++)
			if (!visit[i] && m[s][i] != -1 && d[s] > d[i] + m[s][i]) {
				d[s] = d[i] + m[s][i];
				q.push(P(d[i], i));
			}
	}
	cout << res << endl;
	delete[] d, visit;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int prim(void);

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int A[MAX][MAX];
int n;

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

int prim(){
  int sum = 0;
  int i,j,minV;
  int d[MAX],p[MAX],color[MAX];
  int u,v; 
  for(i = 0; i < n; i++){
    d[i] = INFINITY;
    p[i] = -1;
    color[i] = WHITE;
  }
  d[0] = 0;
  while(1){
    minV = INFINITY;
    u = -1;
    for(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(v = 0; v < n; v++){
      if(color[v] != BLACK && A[u][v] != INFINITY){
        if(d[v] > A[u][v]){
          d[v] = A[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
  //sum = 0;
  for(i = 0; i < n; i++){
    if(p[i] != -1){
      sum += A[i][p[i]];
    }
  }
  return sum;
}

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

#define MAX_N 2000
int n;
int A[MAX_N][MAX_N];

class Node
{
    public:
        int cost = INT_MAX;;
        bool used = false;
};

int prim()
{
    int i, u, v, minv, sum = 0;
    Node *nodes = new Node[n];
    nodes[0].cost = 0;
    while (true) {
        u = -1; minv = INT_MAX;
        for (i = 0; i < n; i++) {
            if (nodes[i].cost < minv && !nodes[i].used) {
                u    = i;
                minv = nodes[i].cost;
            }
        }
        if (-1 == u) break;
        nodes[u].used = true;
        sum += nodes[u].cost;
        for (v = 0; v < n; v++) {
            if (!nodes[v].used && A[u][v] != -1) {
                nodes[v].cost = A[u][v] < nodes[v].cost ? A[u][v] : nodes[v].cost;
            }
        }
    }
    return sum;
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> A[i][j];
        }
    }
    cout << prim() << endl;
    return 0;
}

C++: #include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <set>
#include <deque>
#include <vector>
#define MAX 150
#define rep(i,p) for(int i=0;i<p;i++)
#define rep1(i,p) for(int i=1;i<=p;i++)
#define pb push_back
using namespace std;
int n,temp,a,b,size,ra,rb;
int A[MAX];
int B[MAX];
int W[MAX];
int V[MAX];
pair<int,int> edge[MAX];
int root (int child) {
  int a = child;
  while (V[a] != a) {
    a = V[a];
  }
  return a;
}
int main(){
  size = 0;
  cin >> n;
  rep(i,n) 
    rep(j,n) {
      cin >> W[size];
      if (i <= j) {        
        A[size] = i;
        B[size] = j;
        edge[size].first = W[size];
        edge[size].second = size;
        size++;
      }
    }
  // sort by lenght of edge
  sort(edge, edge+size+1);
  // printing
  // rep(i,size+1) {
  //   cout << A[edge[i].second] << " " << B[edge[i].second]
  //        << " " << edge[i].first << endl;;
  // }
  int sum = 0;
  rep(i,n) V[i] = i;
  rep(i,size) {
    if (edge[i].first == -1) continue;
    temp = edge[i].second;
    // a , b : vertex
    a = A[temp];
    b = B[temp];
    ra = root(a);
    rb = root(b);
    if (ra != rb) {
      // ?????????????????????
      sum += edge[i].first;
      V[ra] = rb;
      // if (ra != a && rb == b){     
      //   V[b] = ra;
      // } else if (ra == a && rb != b) {
      //   V[a] = rb;
      // } else if (ra == a && rb == b) {        
      //   V[a] = b;
      // } else if (ra != a && rb != b) {                
      //   V[ra] = rb;
      // }
    }
  }
  cout << sum << endl;
  return 0;
}