Go: package main

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

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;

static const int INFTY = (1<<21);
enum Color{
    WHITE,
    GRAY,
    BLACK
};
int n;

int M[100][100];
Color color[100];
int d[100];
int q[100];

int prim(){
    for(int i=0; i<n; i++){
        color[i] = WHITE;
        d[i] = INFTY;
    }
    d[0] = 0;
    q[0] = -1;

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

int main(){

    scanf("%d", &n);

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            int x;
            scanf("%d", &x);
            if(x==-1) M[i][j] = INFTY;
            else M[i][j] = x;
        }
    }
    int x = prim();

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

    return 0;
}

C++: 
#include<stdio.h>

#define MAX 100
#define INFINITY 10000

int n;
int M[MAX][MAX], color[MAX], d[MAX], p[0];

enum {
    WHITE,
    GRAY,
    BLACK
};

int prim() {
    for (int i = 0; i < n; ++i) {
        color[i] = WHITE;
        d[i] = INFINITY;
        p[i] = -1;
    }
    d[0] = 0;
    p[0] = -1;

    while (true) {
        int min = INFINITY;
        int target = -1;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min && color[i] != BLACK) {
                min = d[i];
                target = i;
            }
        }
        if (target == -1) break;

        color[target] = BLACK;

        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && M[target][v] > 0) {
                if(M[target][v] < d[v]) {
                    d[v] = M[target][v];
                    p[v] = target;
                    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() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &M[i][j]);
        }
    }
    printf("%d\n", prim());
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int main()
{
    int n;
    cin >> n;

    vector<vector<int>> adj_matrix(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> adj_matrix[i][j];
        }
    }

    const int INF = 1e9 + 7;
    vector<bool> use(n, false);
    vector<int> key(n, INF);
    vector<int> parent(n, -1);
    key[0] = 0;
    int cnt = 0;
    while (true)
    {
        int mn = INF;
        int u = -1;
        for (int i = 0; i < n; i++)
        {
            // keyが最小のノードを選択
            if (use[i] == 0 && key[i] < mn)
            {
                mn = key[i];
                u = i;
            }
        }

        // 選択先がなければ終了
        if (u == -1)
        {
            break;
        }
        use[u] = 1;

        // uに隣接するノードで未訪問かつ辺の重みがkeyより小さいものを選び、keyを辺の重みで更新し、訪問したことにする
        for (int j = 0; j < n; j++)
        {
            if (use[j] == 0 && adj_matrix[u][j] >= 0 && adj_matrix[u][j] < key[j])
            {
                key[j] = adj_matrix[u][j];
                parent[j] = u;
            }
        }
    }

    cout << accumulate(key.begin(), key.end(), 0) << endl;

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#define MAXV 100
#define INF 2001
using namespace std;

int n, total = 0, mine, minj;
int mst[MAXV] = {}; //1 origin
int edge[MAXV][MAXV] = {}; //1 origin

void prim(void);
bool doesFinished(void);

int main() {
	int i, j;
	
	cin >> n;
	for (i = 1; i <= n; i++) {
		
		for (j = 1; j <= n; j++) {
			cin >> edge[i][j];
		}
	}

	mst[1] = 1;
	while (!doesFinished()) {
		prim();
	}

	cout << total << endl;

	return 0;
}

void prim(void) {
	int i, j;
	for (i = 1; i <= n; i++) {
		if (mst[i] == 0)continue;
		mine = INF;
		for (j = 1; j <= n; j++) {
			if (edge[i][j] == -1 || mst[j] == 1)continue;
			if (edge[i][j] < mine) {
				mine = edge[i][j];
				minj = j;
			}
		}
	}
	mst[minj] = 1;
	if(mine != INF)total += mine;
}

bool doesFinished(void) {
	for (int i = 1; i <= n; i++) {
		if (mst[i] == 0)return false;
	}
	return true;
}

C++: #include <iostream>
#include <queue>

using namespace std;

using pii = pair<int, int>;

struct Graph{
  int v_size;
  vector<vector<pii>> adj;
  
  Graph(const int v_size):v_size(v_size){
    this->adj.resize(v_size);
  }
  
  void add_edge(const int s, const int t, const int w){
    this->adj[s].emplace_back(pii(t, w));
  }
};

int mst(const Graph& g){
  constexpr int INF{INT_MAX};
  
  vector<int> d(g.v_size, INF);
  priority_queue<pii, vector<pii>, greater<pii>> pq;

  d[0] = 0;
  pq.push(pii(0, 0));

  int ans {0};
  
  while(!pq.empty()){
    auto cost {pq.top().first};
    auto v {pq.top().second};
    pq.pop();

    if(d[v] < cost) continue;

    ans += cost;
    d[v] = -INF;

    for(int i = 0; i < g.adj[v].size(); ++i){
      auto to {g.adj[v][i].first};
      auto next_cost {g.adj[v][i].second};

      if(d[to] > next_cost){
        d[to] = next_cost;
        pq.push(pii(next_cost, to));
      }
    }
  }

  return ans;
}

int main()
{
  int n;
  cin >> n;
  
  Graph g(n);

  int x;
  for(int i = 0; i < n; ++i){
    for(int j = 0; j < n; ++j){
      cin >> x;
      if(x != -1){
        g.add_edge(i, j, x);
      }
    }
  }

  cout << mst(g) << endl;
}