Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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; // u??????
    int d[MAX], p[MAX], color[MAX]; // d:??¢?´¢??????????±?, p:MST??????????????????v??????

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

    d[0] = 0; // MST?????¢?´¢????§?????????????????????°???

    while (1){
	    minv = INFTY;
	    u = -1;
	    for(int i = 0; i < n; i++){
		    // ??¢?´¢????±?????????§??????????°??????????????????§??????????????¢?´¢???????????????????????????	
		    if(minv > d[i] && color[i] != BLACK){
			    u = i; // MST?????????????????????????±???????
			    minv = d[i];
		    }
	    }
	    if(u == -1) break; //??¨??????????±??????¢?´¢????????¨???????????¢?´¢??????????????? 
	    color[u] = BLACK; // ??¢?´¢???????????????
	    for(int v = 0; v < n; v++){
		   //?????¢?´¢????????§?????????u??¨?????????????????£???????????????
		   //  ?¬??????¢?´¢????±???¨????????????????????????????????????
		   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]]; // MST???????????????????????¨???
    }

    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 BLACK=2;
static const int GRAY=1;
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(void){
    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 <algorithm>
#include <climits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

int n;
vector<int> g[101];
vector<int> d(101);
vector<int> p(101);
vector<int> vis(101);

void prim(){
    for (int i = 0; i < n; ++i) {
        vis[i] = 0;
        d[i] = INT_MAX;
        p[i] = -1;
    }

    d[0] = 0;
    while(true) {
        int mincost = INT_MAX;
        int next = -1;
        for (int i = 0; i < n; ++i) {
            if(vis[i] == 0 && d[i] < mincost) {
                mincost = d[i];
                next = i;
            }
        }
        if(next == -1) break;
        vis[next] = 1;

        for (int j = 0; j < n; ++j) {
            if(g[next][j] < d[j] && vis[j] != 1 && g[next][j] != -1) {
                p[j] = next;
                d[j] = g[next][j];
            }
        }
    }
}

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

    prim();

    long cst=0;
    for (int k = 0; k < n; ++k) {
        cst += d[k];
    }
    cout << cst << 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;
    }
    d[0] = 0;
    p[0] = -1;
    
    while(1) {
        int u;
        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] != -1) {
                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++) {
        sum += d[i];
    }
    
    return sum;
}

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

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

int n; // number of vertices 
int P[2020], A[2020], B[2020], C[2020];
pair<int, int> D[2020]; // Weights and coordinates 

int root(int a) { // find the root of a
  if (P[a] == a) return a; // a is the root
  return  (P[a] = root(P[a])); // find root of a, make it a's parent
}

bool is_same_set(int a, int b) { // a and b are in the same group?
  return root(a) == root(b);
}

void unite(int a, int b) { // put a and b in same group
  P[root(a)] = root(b);
}


int main() {
  cin >> n; // the number of vertices
  for (int i=0; i<n; ++i) {
    P[i] = i; // Initialize vertices to disjoint sets
    for (int j=0; j<n; ++j) {
      A[(n * i) + j] = i;
      B[(n * i) + j] = j;
      cin >> C[(n * i) + j];
    }
  }
  for (int i=0; i < n*n; i++) {
    D[i].first = C[i];
    D[i].second = i;
  }
  sort(D, D+n*n);
  int sum = 0;

  for (int i=0; i<n*n; ++i) {
    if (D[i].first >= 0) { // Assuming the weight is not -1
      if (!is_same_set(A[D[i].second], B[D[i].second])) {
	unite(A[D[i].second], B[D[i].second]);
	sum += D[i].first;
      }
    }
  }
  cout << sum << endl;
}