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 <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <climits>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repR(i, n) for(int i = (n) - 1; i > -1; i--)
#define rep1(i, n) for(int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for(int i = (n); i > 0; i--)
using namespace std;
static const int INF = INT_MAX;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

static const int N = 2010;
int M[N][N];
int color[N];
int d[N];
int p[N];
int n;

void prim() {
  rep(u, n) {
    color[u] = WHITE;
    d[u] = INF;
  }
  d[0] = 0;
  p[0] = -1;

  while (true) {
    int mincost = INF;
    int u;
    rep(i, n) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INF) break;

    color[u] = BLACK;

    rep(v, n) {
      if (color[v] == BLACK || M[u][v] == INF) continue;
      if (M[u][v] < d[v]) {
        d[v] = M[u][v];
        p[v] = u;
        color[v] = GRAY;
      }
    }
  }
}

int main() {
  cin >> n;
  rep(y, n) rep(x, n) {
    int a;
    cin >> a;
    if (a != -1) M[y][x] = a;
    else M[y][x] = INF;
  }
  prim();
  cout << accumulate(d, d + n, 0) << endl;
  return 0;
}

C++: #include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);

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] = 0;
    }
    d[0] = 0;
    
    while(true)
    {
        minv = INFTY;
        u = -1;
        for(int i=0; i<n; i++)
        {
            if(minv > d[i] && color[i] != 2)
            {
                u = i;
                minv = d[i];
            }
        }
        if(u == -1) break;
        color[u] = 2;
        for(int v=0; v<n; v++)
        {
            if(color[v] != 2 && M[u][v] != INFTY)
            {
                if(d[v] > M[u][v])
                {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = 1;
                }
            }
        }
    }
    int sum = 0;
    for(int i=0; i<n; i++)
    {
        if(p[i] != -1) sum += M[i][p[i]];
    }
    return sum;
}

int main() 
{
    ios::sync_with_stdio(false);
    
    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;
int n;
int dat[100][100];
int cost[100];
int done[100];

void prim(){
	for(int i=0;i<n;i++){
		cost[i]=1<<21;
		done[i]=0;
	}
	int s=0;	
	int next, ncost=1<<21;
	done[s]=1;
	cost[s]=0;
	int cnt=0;
	while(1){
		cnt++;
		if(cnt>20)
			return;
		
		ncost=1<<21;
		for(int j=0;j<n;j++){
			if(done[j]){
				for(int i=0;i<n;i++){
					if(dat[j][i]!=-1){
						if(cost[i]>=dat[j][i]){
							cost[i]=dat[j][i];
						}
						if(ncost>cost[i]&&!done[i]){
							next=i;
							ncost=cost[i];
						}
					}
				}
			}
		}
//		cout<<"next:"<<next<<" cost:"<<cost[next]<<endl;
		done[next]=1;
		int flag=true;
		for(int i=0;i<n;i++){
			if(!done[i]) flag = false;
		}
		if(flag)
			return;
	}
}

int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>dat[i][j];
		}
	}
	prim();
	int sum=0;
	for(int i=0;i<n;i++){
		sum+=cost[i];
	}
	cout<<sum<<endl;
	return 0;
}

C++: 
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e2+5;
const int inf=(1<<21);
const int white=0;
const int gray=1;
const int black=2;

int n,m[maxn][maxn];

int prim()
{
    int u,minv,sum=0;
    int d[maxn],p[maxn],color[maxn];
    for(int i=0;i<n;i++){
        d[i]=inf;
        color[maxn]=white;
        p[i]=-1;
    }
    d[0]=0;
    while(1){
        minv=inf;
        u=-1;
        for(int i=0;i<n;i++){
            if(minv<d[i]&&color[i]==white){
                minv=d[i];
                u=i;
            }
        }
        if(u==-1){
            break;
        }
        color[u]=black;
        sum+=d[u];
        for(int i=0;i<n;i++){
            if(color[i]!=black&&d[i]<m[u][i]){
                d[i]=m[u][i];
                p[i]=u;
                color[i]=gray;
            }
        }
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int e;
            cin>>e;
            m[i][j]=(e==-1)?inf:e;
        }
    }
    cout<<prim()<<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
*/

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int P[1010];
int cost[5000];
int src[5000];	
int dst[5000];
int A[100 + 10][100 + 10];
int M = 0;
vector<pair<int,int>> edges;

void init(int N)
{
    for(int i = 1; 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()
{
	int n = 0;
	cin >> n; 
	for(int r = 0; r < n; ++r)
	{
		for(int c = 0; c < n; ++c)
		{
			cin >> A[r][c];
		}
	}
	for (int r = 0; r < n; ++r)
	{
		for(int c = r + 1; c < n; ++c)
		{
			if(A[r][c] > -1)
			{
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				edges.push_back(make_pair(cost[M],M));
				M += 1;
			}
		}
	}
	sort(edges.begin(),edges.end());
	int sum = 0;
	init(n);
	for(int i = 0; i < M; ++i)
	{
		if(is_same_set(src[edges[i].second], dst[edges[i].second]) == 1) continue;
			unite(dst[edges[i].second],src[edges[i].second]);
			sum += edges[i].first;
	}
	cout << sum << endl;
}