Go: package main

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

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

const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())
			if c != -1 {
				M[i][j] = c
			} else {
				M[i][j] = math.MaxInt64
			}

		}
	}
	prim()
}

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

	for i := 0; i < n; i++ {
		d[i] = math.MaxInt64
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		minv = math.MaxInt64
		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] != math.MaxInt64 {
				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]]
		}
	}
	fmt.Println(sum)

}

C++: #include <cstdio>
#pragma warning(disable:4996)
#define MAX 101
#define INF 100000
#define FLAG 1
#define DEFLAG 0

int main() {
	int i, j, n, min, a[MAX][MAX], k, x[MAX], minNode, sum = 0, g[MAX];

	scanf("%d", &n);
	for (i = 0; i<n; i++) {
		for (j = 0; j<n; j++) {
			scanf("%d", &a[i][j]);
		}
	}
	k = 1;
	x[0] = 0;
	g[0] = FLAG;
	for (i = 1; i<n; i++) {
		g[i] = DEFLAG;
	}
	while (1) {
		min = INF;
		for (i = 0; i<k; i++) {
			for (j = 0; j<n; j++) {
				if (min > a[x[i]][j] && a[x[i]][j] != -1 && g[j] != FLAG) {
					min = a[x[i]][j];
					minNode = j;
				}
			}
		}
		if (k != n) {
			x[k] = minNode;
			g[minNode] = FLAG;
			k++;
			sum += min;
		}
		else { break; }

	}
	printf("%d\n", sum);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

const int INF = 1 << 21;
const int MAX = 103;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
int n, G[MAX][MAX];

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

  fill((int*)d, (int*)(d+MAX), INF);
  memset(p, -1, sizeof(p));
  memset(color, WHITE, sizeof(color));

  d[0] = 0;
  while (true) {
    minv = INF;
    u = -1;
    rep(i,n) {
      if (minv > d[i] && color[i] != BLACK) {
	u = i;
	minv = d[i];
      }
    }
    if (u == -1) break;
    color[u] = BLACK;
    rep(v,n) {
      if (color[v] != BLACK && G[u][v] != INF) {
	if (d[v] > G[u][v]) {
	  d[v] = G[u][v];
	  p[v] = u;
	  color[v] = GRAY;
	}
      }
    }
  }
  
  int sum = 0;
  rep(i,n) {
    if (p[i] != -1) sum += G[i][p[i]];
  }
  return sum;
}

int main() {
  cin >> n;
  rep(i,n) rep(j,n) {
    int e;
    cin >> e;
    G[i][j] = (e == -1) ? INF : e;
  }

  cout << prim() << endl;
  return 0;
}

C++: #include <cstdio>
#define INF 1<<30

int n, M[100][100];

int prim() {
    int e, minv, minx, s;
    int p[n], d[n], v[n];

    for (e = 0; e < n; e++) {
        p[e] = -1;
        d[e] = INF;
        v[e] = 0;
    }

    d[0] = 0;

    while (1) {
        minv = INF;
        minx = -1;
        for (e = 0; e < n; e++) {
            if (minv > d[e] && v[e] != 1) {
                minx = e;
                minv = d[e];
            }
        }

        if (minx == -1) break;
        v[minx] = 1;
        
        for (e = 0; e < n; e++) {
            if (v[e] != 1 && M[minx][e] != INF) {
                if (d[e] > M[minx][e]) {
                    d[e] = M[minx][e];
                    p[e] = minx;
                }
            }
        }
    }

    s = 0;
    for (e = 0; e < n; e++) {
        if (p[e] != -1) s += M[e][p[e]];
    }

    return s;
}

int main(void) {
    int i, j, v;
    scanf("%d", &n);

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

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

    return 0;
}