Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <list>
#include <stack>
#include <cctype>
#include <cmath>

using namespace std;

/* typedef */

typedef long long ll;

/* constant */

const int INF = 1 << 30;
const int MAX = 100;
const int mod = 1000000007;
const double pi = 3.141592653589;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

/* global variables */
vector< vector<int> > Adj(MAX, vector<int>(MAX, INF));
vector<int> color(MAX, WHITE);
vector<int> d(MAX, INF);
vector<int> p(MAX, -1);
int n;
int total_cost = 0;

/* function */
void prim();

/* main */
int main() {
    
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int a;
            cin >> a;
            if (a != -1)
                Adj[i][j] = a;
        }
    }

    prim();

    cout << total_cost << '\n';

}

void prim() {

    int u;
    int min_cost = INF;
    d[0] = 0;
    p[0] = -1;

    while (1) {

        u = -1;
        min_cost = INF;
        for (int i = 0; i < n; i++) {
            if (color[i] != BLACK && d[i] < min_cost) {
                u = i;
                min_cost = d[i];
            }
        }
        if (min_cost == INF) {
            break;
        }

        color[u] = BLACK;
        total_cost += min_cost;

        for (int v = 0; v < n; v++) {
            if (color[v] != BLACK && Adj[u][v] != INF) {
                // if (d[u] + Adj[u][v] < d[v]) {
                if (Adj[u][v] < d[v]) {
                    p[v] = u;
                    // d[v] = Adj[u][v] + d[u];
                    d[v] = Adj[u][v];
                    color[v] = GRAY;
                }
            }
        }
    }

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define loop(i,a,b) for(i=a;i<b;i++)
#define rloop(i,a,b) for(i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
const int inf=1000000001;
const ll INF=1e16;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int N=100;

int n;
vii a(N,vi(N));

int prim();

int main(){
	int i,j;
	cin>>n;
	loop(i,0,n){
		loop(j,0,n){
			int k;
			cin>>k;
			a[i][j]=(k==-1)? inf:k;
		}
	}
	int m=prim();
	cout<<m<<endl;
}

int prim(){
	int u,minv,i;
	vi d(n,inf);
	vi p(n,-1);
	vi color(n,WHITE);
	d[0]=0;
	while(1){
		minv=inf;
		u=-1;
		loop(i,0,n){
			if(minv > d[i] && color[i]!=BLACK){
				u=i;
				minv=d[i];
			}
		}
		if(u==-1){
			break;
		}
		color[u]=BLACK;
		int v;
		loop(v,0,n){
			if(color[v]!=BLACK && a[u][v]!=inf){
				if(d[v]>a[u][v]){
					d[v]=a[u][v];
					p[v]=u;
					color[v]=GRAY;
				}
			}
		}
	}
	int sum=0;
	loop(i,0,n){
		if(p[i]!=-1){
			sum+=a[i][p[i]];
		}
	}
	return sum;
}

C++: #include <bits/stdc++.h>
using namespace std;
using uint = uint32_t;
using ll = long long;
using ld = long double;
using vi = vector<int>;    using vvi = vector<vi>;
using vb = vector<bool>;   using vvb = vector<vb>;
using vl = vector<ll>;     using vvl = vector<vl>;
using vd = vector<double>; using vvd = vector<vd>;

#define REP(i,n) for(auto i = 0 * n; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i,s,e) for (ll i = s; i < (ll)e; i++)
#define TEN(x) ((ll)1e##x)


struct Edge{ uint32_t to, id; };
typedef vector<vector<Edge>> Graph;

template<class T> struct Prim {
	Prim(const Graph & es, const vector<T> & ws) : g(es.size()) {
		typedef uint32_t U;
		typedef tuple<T, U, U, U> P;
		U n = es.size();
		vector<U> unexp(n, 1);
		priority_queue<P, vector<P>, greater<P>> q; q.emplace(0, n, 0, 0);
		while (!q.empty()) {
			T w; U u, v, i; tie(w, u, v, i) = q.top(); q.pop();
			if (unexp[v]) {
				unexp[v] = 0;
				if (u != n) { g[u].push_back({ v, i }); g[v].push_back({ u, i }); cost += w; }
				for (auto e : es[v]) if (unexp[e.to]) q.emplace(ws[e.id], v, e.to, e.id);
			}
		}
	}
	Graph g; T cost = 0;
};

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(50);

	uint n; cin >> n;
	Graph g(n);
	vl w;
	REP(i, n) REP(j, n) {
		ll x; cin >> x;
		if (x != -1) {
			g[i].push_back({j, (uint)w.size()});
			g[j].push_back({i, (uint)w.size()});
			w.push_back(x);
		}
	}
	Prim<ll> mst(g, w);
	cout << mst.cost << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX 100
#define INFTY (1<<21)
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define NIL -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(){
  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;
}