Go: package main

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

const (
	MAX = 100
	INF = 1 << 21
	WHITE = 0
	GRAY = 1
	BLACK = 2
)

var (
	n int
	M [MAX][MAX]int
)

func prim() int {
	var u, minv int
	var d [MAX]int
	var p [MAX]int
	var color [MAX]int

	for i := 0; i < n; i++ {
		d[i] = INF
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0
	for {
		minv = INF
		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 && M[u][v] != INF {
				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() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			scanner.Scan()
			e, _ := strconv.Atoi(scanner.Text())
			if e == -1 {
				M[i][j] = INF
			} else {
				M[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

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