Go: package main

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

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

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

	for {
		minv = INFTY
		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 && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += g[i][p[i]]
		}
	}

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #define dbg(...) fprintf(stderr,__VA_ARGS__)
#define dpr(x) cerr<<#x<<": "<<x<<endl;
#define dprc(c) do{cerr<<#c<<":";for(auto&_i:(c)){cerr<<" "<<_i;}cerr<<endl;}while(0)
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
int INF = 1e9+7;
#define all(c) (c).begin(), (c).end()
#define tr(i,c) for(auto i=(c).begin();i!=(c).end();i++)
#define rtr(i,c) for(auto i=(c).rbegin();i!=(c).rend();i++)
#define rep(i,b) for(auto i=0;i<(b);i++)
#define pb push_back
#define sz(c) int((c).size())

#define MAX_N 110

class UnionFind {
public:
  UnionFind(size_t s) : parent_(s), rank_(s, 0) {
    for (size_t i = 0; i < s; i++) {
      parent_[i] = i;
    }
  }

  void unite(size_t x, size_t y) {
    size_t xp = find(x);
    size_t yp = find(y);

    if (xp == yp) return;
    if (rank_[xp] < rank_[yp]) {
      parent_[xp] = parent_[yp];
    } else {
      if (rank_[xp] == rank_[yp]) rank_[xp]++;
      parent_[yp] = parent_[xp];
    }
  }

  bool is_same(size_t x, size_t y) {
    return find(x) == find(y);
  }

  size_t find(size_t x) {
    if (parent_[x] == x) return x;
    else return parent_[x] = find(parent_[x]);
  }

  size_t size() const {
    return parent_.size();
  }

private:
  vector<int> parent_;
  vector<int> rank_;
};

struct edge {
  int w, v, u;
};

void printuf(UnionFind& uf) {
  for (int i = 0; i < sz(uf); i++) {
    fprintf(stderr, " %-2d", i);
  }
  puts("");
  for (int i = 0; i < sz(uf); i++) {
    fprintf(stderr, " %-2lu", uf.find(i));
  }
  puts("");
}

int main(int argc, char **argv)
{

  int n;
  scanf("%d", &n);

  vector<edge> E;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int x;
      scanf("%d", &x);
      if (i >= j) continue;
      edge e = {x, i, j};
      if (x >= 0) E.pb(e);
    }
  }

  sort(all(E), [=](const edge& lhs, const edge& rhs) { return lhs.w < rhs.w; });

  UnionFind S(n);

  int total = 0;

  for (int i = 0; i < sz(E); i++) {
    edge& e = E[i];

    int v = S.find(e.v);
    int u = S.find(e.u);

    if (v == u) continue;
    total += e.w;
    S.unite(e.v, e.u);
  }

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

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf (1 << 30)

static const int N = 100;
static const int white = 0;
static const int gray = 1;
static const int black = 2;

int n;
int m[N][N];

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

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int v;
			cin >> v;
			v == -1 ? m[i][j] = inf : m[i][j] = v;
		}
	}
	prim();
	cout << prim() << endl;
	return 0;
}

C++: #include<iostream>
using namespace std;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int MAXN = 100;
static const int INTFY = (1 << 21);

int n, M[MAXN][MAXN];

int prim()
{
	int u, minv;
	int d[MAXN], p[MAXN], color[MAXN];
	
	for(int i = 0; i < n; i++)
	{
		d[i] = INTFY;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	
	while(true)
	{
		minv = INTFY;
		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] != INTFY)
			{
				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++)
	{
		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)? INTFY : e; 
		}
	}
	cout << prim() << endl;
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
enum Color{WHITE, GRAY, BLACK};

int n;
int **a;
Color *color;

int prim(){
        color[0] = BLACK;
        int sum=0;
        while(1){
                int p, minp=INT_MAX;
                for(int i=0;i<n;i++){
                        if(color[i]!=BLACK) continue;
                        for(int j=0;j<n;j++){
                                if(color[j]==BLACK||a[i][j]==-1) continue;
                                else if(color[j]==WHITE) color[j] = GRAY;
                                if(minp>a[i][j]){
                                        minp = a[i][j];
                                        p = j;
                                }
                        }
                }
                if(minp==INT_MAX) break;
                sum += minp;
                color[p] = BLACK;
        }
        return sum;
}

int main(){
        cin>>n;
        a = new int*[n];
        for(int i=0;i<n;i++) a[i] = new int[n];
        for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                        cin>>a[i][j];
                }
        }
        color = new Color[n];
        for(int i=0;i<n;i++) color[i] = WHITE;

        cout<<prim()<<endl;

        delete a;
        return 0;
}