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 <cstdio>
#include <vector>
#include <list>

#define INFTY 1000000001

class Graph{
public:
	std::vector< std::vector<int> > A;
	int n;

	Graph(int n);

	void SetEdge(int a, int b, int weight);
	void SortEdge();
	void SortEdge_Rec1(int left, int right);
	void SortEdge_Rec2(int left, int mid, int right);

	int Kruskal();

};

Graph::Graph(int n){;
	this->n = n;
}

void Graph::SetEdge(int a, int b, int weight){
	std::vector<int> x;
	x.push_back(a); x.push_back(b); x.push_back(weight);
	A.push_back(x);
	return;
}

void Graph::SortEdge(){
	SortEdge_Rec1(0, A.size());
}

void Graph::SortEdge_Rec1(int left, int right){
	if (left + 1 < right){
		int mid = (left + right) / 2;
		SortEdge_Rec1(left, mid);
		SortEdge_Rec1(mid, right);
		SortEdge_Rec2(left, mid, right);
	}
}

void Graph::SortEdge_Rec2(int left, int mid, int right){
	int n1 = mid - left;
	int n2 = right - mid;
	std::vector< std::vector<int> > L(n1 + 1);
	for (int i = 0; i < n1; i++) L[i] = A[left + i];
	std::vector< std::vector<int> > R(n2 + 1);
	for (int i = 0; i < n2; i++) R[i] = A[mid + i];
	std::vector<int> x;
	x.push_back(0); x.push_back(0); x.push_back(INFTY);
	L[n1] = x;
	R[n2] = x;
	int i = 0;
	int j = 0;
	for (int k = left; k < right; k++){
		if (L[i][2] <= R[j][2]){
			A[k] = L[i];
			i = i + 1;
		}
		else{
			A[k] = R[j];
			j = j + 1;
		}
	}
}

int Graph::Kruskal(){
	std::vector<int> f(n);
	for (int i = 0; i < n; i++) f[i] = i;
	int branch = 0;

	while (!A.empty()){
		std::vector<int> e = A[0];
		A.erase(A.begin());
		if (f[e[0]] != f[e[1]]){
			int tmp = f[e[1]];
			for (int i = 0; i < n; i++) if (f[i] == tmp) f[i] = f[e[0]];
			branch += e[2];
		}
	}
	return branch;
}

int main(){
	int n, e;
	scanf("%d", &n);
	Graph G(n);
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++){
			scanf("%d", &e);
			if (j >= i && e != -1) G.SetEdge(i, j, e);
		}
	}

	G.SortEdge();

	printf("%d\n", G.Kruskal());

	return 0;
}

C++: #include <iostream>
using namespace std;
int n,m[100][100];
void mins(){
    int u,min;
    int d[100],p[100],color[100];
    for(int i=0;i<n;i++){
        d[i]=2001;
        p[i]=-1;
        color[i]=1;
    }
    d[0]=0;
    while(1){
        min=2001;
        u=-1;
        for(int i=0;i<n;i++){
            if(color[i]!=3&&d[i]<min){
                u=i;
                min=d[i];
            }
        }
        if(u==-1){
            break;
        }
        color[u]=3;
        for(int v=0;v<n;v++){
            if(color[v]!=3&&m[u][v]!=-1){
                if(d[v]>m[u][v]){
                    p[v]=u;
                    d[v]=m[u][v];
                    color[v]=2;
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<n;i++){
        if(p[i]!=-1){
            ans+=d[i];
        }
    }
    cout<<ans<<endl;
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>m[i][j];
        }
    }
    mins();
    return 0;
}

C++: #include<stdio.h>
#define INFTY (1<<21)
int main(){
	int n,c[100]={},m[100][100],d[100],p[100],i,j,u,a,sum=0;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			scanf("%d",&a);
			m[i][j]=a==-1?INFTY:a;
		}
	}
	for(i=0;i<n;i++){
		d[i]=INFTY;
		p[i]=0;
	}
	p[0]=-1;
	d[0]=0;
	c[0]=1;
	while(1){
		int minv=INFTY;
		u=-1;
		for(i=0;i<n;i++){
			if(minv>d[i]&&c[i]!=2){
				minv=d[i];
				u=i;
			}
		}
		if(u==-1)break;
		c[u]=2;
		for(i=0;i<n;i++){
			if(c[i]!=2&&m[u][i]!=INFTY){
				if(d[i]>m[u][i]){
					d[i]=m[u][i];
					p[i]=u;
					c[i]=1;
				}
			}
		}
	}
	for(i=0;i<n;i++){
		if(p[i]!=-1){
			sum+=m[i][p[i]];
		}
	}
	printf("%d\n",sum);
	return 0;
}

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

static const int MAX = 100;
static const int INF = (1<<21);
int color[MAX] = {0};
int M[MAX][MAX];
int d[MAX];

void prim(int n) {
    d[0] = 0;
    int minconst = INF, u, i;
    for ( i = 1; i < n; i++ ) d[i] = INF;

    while(1) {
        minconst = INF;
        for ( i = 0; i < n; i++ )
        {
            if ( color[i] != 1 && minconst > d[i] ) {
                minconst = d[i];
                u = i;
            }
        }

        if ( minconst == INF )
            break;

        color[u] = 1;

        for ( i = 0; i < n; i++ ) {
            if ( color[i] != 1 && M[u][i] != -1 ) {
                if ( M[u][i] < d[i] ) d[i] = M[u][i];
            }
        }
    }
}

int main() {
    int n, i, j;

    cin >> n;
    for ( i = 0; i < n; i++ ) {
        for ( j = 0; j < n; j++ ) {
            cin >> M[i][j];
        }
    }

    prim(n);

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

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, 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;
}