Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

struct UF{
    static const int MAX_N = 100000;
    int p[MAX_N];
    int r[MAX_N];

    void init(int n){
        REP(i,n){
            p[i]=i;
            r[i]=0;
        }
    }
    int find(int x){
        if(p[x]==x)return x;
        return p[x] = find(p[x]);
    }
    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y)return;
        if(r[x] < r[y])swap(x,y);
        if(r[x]==r[y])r[x]++;
        p[y]=x;
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
};

int n;
vector<pair<int,P>> es;

int MST(){
    int res = 0;
    sort(es.begin(), es.end());
    UF g;
    g.init(n);
    REP(i,es.size()){
        int cost = es[i].first, u = es[i].second.first, v = es[i].second.second;
        if(g.same(u,v))continue;
        g.unite(u,v);
        res += cost;
    }
    return res;
}

int main() {
    cin >> n;
    REP(i,n){
        REP(j,n){
            int a;
            cin >> a;
            if(a==-1)continue;
            es.push_back({a,{i,j}});
        }
    }
    cout << MST() << endl;
    return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int x[10000][10000];
int MIN[10000];
int MINX = 0;
int MINZ = 0;

int main() {
	memset(MIN, 127, sizeof(MIN));
	int n, a;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a;
			x[i][j] = a;
		}
	}
	for(int h=0;h<n;h++){
		memset(MIN, 127, sizeof(MIN));
		MINX=0;
		MIN[h] = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < n; k++) {
					if (x[j][k] != -1) {
						MIN[k] = min(MIN[k], x[j][k]);
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			MINX += MIN[i];
		}
		MINZ=min(MINZ,MINX);
	}
	cout << MINZ << 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 GRAY = 1;
static const int BLACK = 2;

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

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define ALL(n) begin(n),end(n)
using Graph=vector<vector<int>>;

int n;
// プリムのアルゴリズム
vector<int> included;
int Prim(const Graph &G,int v,int &cost){
    // 頂点vは木に含まれた
    included.push_back(v);
    // n頂点全てが木に含まれていれば終了
    if(included.size()==n) return cost;

    int min_=2001;
    int to=-1;
    for(int x:included){
        for(int y=0; y<n; y++){
            if(G[x][y]<min_ && G[x][y]!=-1){
                // yがincludedに含まれている場合，何もしない
                if(find(included.begin(),included.end(),y)!=included.end()){
                    continue;
                }
                min_=G[x][y];
                to=y;
            }
        }
    }

    // 頂点toを追加して，Primを呼び出す
    cost+=min_;
    return Prim(G,to,cost);
}

int main(){
    cin>>n;
    
    // 隣接行列を受け取る
    Graph G(n,vector<int>(n));
    REP(i,n){
        REP(j,n){
            cin>>G[i][j];
        }
    }

    int cost=0;

    cout<<Prim(G,0,cost)<<endl;
}

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