Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			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 && M[u][v] != INFTY {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

C++: /*#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
int main(){
	int n, a, sum = 0;
	int temp = 1000000;
	int n1, n2;
	int r1[101] = {0}, r2[101] = { 0 };
	int m[101][101] = { 0 };
	cin >> n;
	for (int i = 0; i < n; i++){
		for (int l = 0; l < n; l++){
			cin >> a;
			m[i][l] = a;
		}
	}
	for (int i = 0; i < n-1; i++){
		for (int l = 0; l < n; l++){
			cout << m[i][l] << "   " << i <<endl;

			if (m[i][l] == -1) continue;
			if (m[i][l] < temp&&r1[l] == 0 && r2[i] == 0){
				temp = m[i][l];
				n1 = i; n2 = l;
			}
		}
		if (temp != 1000000){
			sum += temp;
			cout << i + 1 << endl;
			cout << temp <<endl;
			r1[n1] = -1; r2[n2] = -1;
			cout << n1  << n2 << endl;
			n1 = 0; n2 = 0;
			temp = 1000000;
		}
	}
	cout << sum << endl;
	return 0;
}*/
/*
5
-1 2 3 1 -1
2 -1 -1 4 -1
3 -1 -1 1 1
1 4 1 -1 3
-1 -1 1 3 -1
*/
#include<queue>
#include<stack>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
#define WHITE 0
#define BLACK 1
#define GRAY 2
#define INFTY 1000000
int color[101];
int m[101][101];
int d[101];
int p[101];
int mincost = INFTY;
int n;
int yy;


void prim(int u){
	while (1){
		mincost = INFTY;
		for (int i = 0; i < n - 1; 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 - 1; 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 main(){
	int a[10] = { 0 };
	for (int i = 0; i < 101; i++){
		color[i] = INFTY; d[i] = INFTY; p[i] = -1;
	}
	for (int i = 0; i < 101; i++){
		for (int l = 0; l < 101; l++){
			m[i][l] = 0;
		}
	}
	cin >> n;
	for (int i = 0; i < n; i++){
		for (int l = 0; l < n; l++){
			cin >> m[i][l];
		}
	}
	prim(0);
	int sum = 0;
	for (int i = 0; i < n; i++){
		sum += p[i];
	}
	cout << sum*(-1) << endl;
	return 0;
}

C++: #include<stdio.h>

#define INFTY (1<<21)
#define MAX 100
#define WHITE 0
#define GRAY 1
#define 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(){
  
  scanf("%d",&n);

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      int e;
      scanf("%d",&e);
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  printf("%d\n",prim());

  return 0;
}

C++: #include <tuple>
#include <iostream>
#include <algorithm>
using namespace std;
int P[10010]; 
tuple<int,int,int> a[10010];
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(void){
    int n,value;
    cin >> n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> value;
            a[n*i+j] = make_tuple(value,i,j);
        }
    }
    init(n*n);
    sort(a,a+n*n);
    int sum =0;
    int x,y;
    for(int i =0;i<n*n;i++){
        x = get<1>(a[i]);
        y = get<2>(a[i]);
        if(is_same_set(x,y)!=1){
            if(get<0>(a[i])!=-1){
                sum += get<0>(a[i]);
                unite(x,y);
            }
        }
    }
    cout << sum << 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;
	for (int i = 0; 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 {
							int num = color[j];
							std::replace(color.begin(), color.end(), num, 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;
}