Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

C++: #include<cstdio>
#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define _USE_MATH_DEFINES // M_PI????????????????????????
#include<math.h>
#include<queue>
#pragma warning(disable:4996)
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;
		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() << "\n";
}

C++: #include<iostream>
#include<stack>
#include<queue>
using namespace std;
#define max 100
#define infty 1<<21

int n;
int aa[max][max];
int color[max];
int d[max];
int p[max];

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

	while (true) {
		int mincost = infty;
		int u = -1;
		for (int i = 0; i < n; i++) {
			if (color[i] != 1 && d[i] < mincost) {
				u = i;
				mincost = d[i];
			}
		}
		if (u == -1) break;
		color[u] = 1;
		for (int i = 0; i < n; i++) {
			if (color[i] != 1 && aa[u][i] != -1) {
				if (aa[u][i] < d[i]) {
					d[i] = aa[u][i];
					p[i] = u;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) ans += aa[i][p[i]];
	}

	return ans;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int num1;
			cin >> num1;
			aa[i][j] = num1;
		}
	}

	int kotae = prim();

	cout << kotae << endl;

	return 0;
}

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

using namespace std;


class UnionFind{
    int N;
    vector<int> par;
    vector<int> rank;
    public:
    UnionFind(int N):N(N),par(N),rank(N,0){iota(par.begin(),par.end(),0);}
    int find(int x);
    void unite(int x,int y);
    bool same(int x,int y){return find(x)==find(y);}
};

int UnionFind::find(int x){
    if(par[x]==x) return x;
    return par[x]=find(par[x]);
}
void UnionFind::unite(int x,int y){
    x=find(x),y=find(y);
    if(x==y) return;
    if(rank[x]>=rank[y]){
        if(rank[x]==rank[y]) rank[x]++;
        par[y]=x;
    }else{
        par[x]=y;
    }
}

using Weight=long long;
struct Edge{
    int from,to;
    Weight weight;
};
bool operator<(const Edge& lhs,const Edge& rhs){
    return lhs.weight<rhs.weight;
}

using Edges=vector<Edge>;
using Graph=vector<Edges>;


Edges MinimumSpanningTree(int n,Edges edges){
    sort(edges.begin(),edges.end());
    Edges res;
    UnionFind u(n);
    for(int i=0;i<edges.size();i++){
        if(!u.same(edges[i].from,edges[i].to)){
            res.push_back(edges[i]);
            u.unite(edges[i].from,edges[i].to);
        }
    }
    return res;
}

Edges MinimumSpanningTree(const Graph& g){
    Edges edges;
    for(int i=0;i<g.size();i++){
        copy(g[i].begin(),g[i].end(),back_inserter(edges));
    }
    auto res=MinimumSpanningTree(g.size(),edges);
    return res;
}

int main(){
    int n;
    cin>>n;
    Graph g(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            Weight c;
            cin>>c;
            if(c==-1) continue;
            g[i].push_back({i,j,c});
        }
    }
    auto mst=MinimumSpanningTree(g);
    cout<<accumulate(mst.begin(),mst.end(),Weight(0),[](const Weight& lhs,const Edge& rhs){return lhs+rhs.weight;})<<endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
#define REP(i,n) for(unsigned int i=0;i<(n);i++)
#define MAX 101
#define INF (1<<21)
#define NONE -1
#define WHITE 0
#define GRAY 1
#define BLACK 2


int node[MAX][MAX];
int N;

int prim()
{
	int cost[MAX];
	int color[MAX];
	int parent[MAX];
	REP(i, N)
	{
		cost[i] = INF;
		color[i] = WHITE;
		parent[i] = NONE;
	}

	cost[0] = 0;
	
	while (true)
	{
		int min_index = NONE;
		int min_value = INF;
		REP(i, N)
		{
			if (color[i] !=BLACK)
			{
				if (cost[i] < min_value)
				{
					min_index = i;
					min_value = cost[i];
				}
			}
		}
		if (min_index == NONE)break;
		color[min_index] = BLACK;
		REP(i, N)
		{
			if (color[i] != BLACK)
			{
				if (node[min_index][i] != INF)
				{
					if (cost[i] > node[min_index][i])
					{
						cost[i] = node[min_index][i];
						parent[i] = min_index;
						color[i] = GRAY;
					}
				}
			}
		}
	}
	int ans = 0;
	REP(i, N)
	{
		if (parent[i]!=NONE)
		{
			ans += node[i][parent[i]];
		}
	}
	return ans;
}


int main()
{
	REP(i, MAX)
	{
		REP(j, MAX)
		{
			node[i][j] = INF;
		}
	}
	cin >> N;
	REP(i, N)
	{
		REP(j, N)
		{
			int tmp;
			cin >> tmp;
			if (tmp == -1)
			{
				node[i][j] = INF;
			}
			else node[i][j] = tmp;
		}
	}
	cout << prim() << endl;
	return 0;
}