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 WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n,M[MAX][MAX];

int prim(){
int u,min;
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){
min=INFTY;
u=-1;
for(int i=0;i<n;i++){
if(min>d[i]&&color[i]!=BLACK){
u=i;
min=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++: #include<iostream>

using namespace std;

static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GREY = 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; i++) {
      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++: #include<iostream>
#include<stdio.h>
#define MAX 2001
#define NIL -1
#define B -2
#define W -1
#define G 0
#include<queue>
#include<algorithm>
using namespace std;

int d[MAX];
int p[MAX];
int color[MAX];
int n;
int M[MAX][MAX];
queue <int> Q;

void Prim() {
	for (int i = 0; i < n; i++) {
		d[i] = MAX;
		p[i] = NIL;
		color[i] = W;
	}
	d[0] = 0;
	color[0] = B;
	int s = 0;
	Q.pop();
	while (!Q.empty()) {
		
		for (int i = 0; i < n; i++) {
			if (M[s][i] != -1 && M[s][i] < d[i] && color[i] != B) {
				d[i] = M[s][i];
				p[i] = s;
				color[i] = G;

			}
		}
		int mini = MAX;
		for (int j = 0; j < n; j++) {
			if (color[j] == G) {			
				if (mini > d[j]) {
					s = j;
				}
				mini= min(mini, d[j]);
			}
		}
		color[s] = B;
		Q.pop();
	}

}
int main() {
	
	cin >> n;
	int a;
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a;
			M[i][j] = a;
		}
		Q.push(i);
	}

	Prim();
	int sum = 0;
	for (int i = 0; i < n; i++) {
		sum = sum + d[i];
	}
	cout << sum << endl;
}

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

constexpr int MAXN = 100;
int G[MAXN][MAXN];
int P[MAXN];
int D[MAXN];
int C[MAXN];

int main(){
    int n;
    std::cin >> n;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            std::cin >> G[i][j];
            if (G[i][j] == -1) {
                G[i][j] = std::numeric_limits<int>::max();
            }
        }
    }

    for (int i = 0; i < n; i++) {
        D[i] = std::numeric_limits<int>::max();
    }

    D[0] = 0;
    P[0] = -1;
    while (true) {
        int m;
        int mincost = std::numeric_limits<int>::max();
        for (int i = 0; i < n; i++) {
            if ((C[i] == 0) && (D[i] < mincost)) {
                mincost = D[i];
                m = i;
            }
        }

        if (mincost == std::numeric_limits<int>::max()) {
            break;
        }

        C[m] = 1;
        D[m] = mincost;

        for (int i = 0; i < n; i++) {
            if ((G[m][i] < std::numeric_limits<int>::max()) && (C[i] == 0) && (G[m][i] < D[i])) {
                D[i] = G[m][i];
                P[i] = m;
            }
        }
    }


    int sum = 0;
    for (int i = 0; i < n; i++) {
        if (D[i] != std::numeric_limits<int>::max()) {
            sum += D[i];
        }
    }
//    for (int i = 0; i < n; i++) {
//        std::cout << i << ": " << D[i] << std::endl;
//    }
    std::cout << sum << std::endl;

}

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

int color(std::vector<std::vector<int> > v) {
	std::vector<int> color(v.size(), 0);
	int count = 0, color_count = 1;
	bool flag = false;
	for (int i = 1; i <= 2000; i++) {
		for (size_t j = 0; j < v.size(); j++) {
			for (size_t k = j + 1; k < v.size(); k++) {
				if (v[j][k] == i && ((color[k] == 0 || color[j] == 0) || (color[k] != color[j]))) {
					if (color[k] == 0) {
						if (color[j] != 0) {
							color[k] = color[j];
						} else {
							color[k] = color[j] = color_count;
							color_count++;
						}
					} else {
						if (color[j] == 0) {
							color[j] = color[k];
						} else {
							std::replace(color.begin(), color.end(), color[j], color[k]);
						}
					}
					count += i;
				}
			}
		}
	}
	return count;
}

int main() {
	int num;
	std::cin >> num;

	std::vector<std::vector<int> > v(num);

	for (int i = 0; i < num; i++) {
		for (int j = 0; j < num; j++) {
			int n;
			std::cin >> n;
			v[i].push_back(n);
		}
	}

	

	std::cout << color(v) << std::endl;

	return 0;
}