Go: package main

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

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAX=105;
int state[MAX],par[MAX],N,S[MAX][MAX];
priority_queue<pair<int,int>> Q;

int prim(){
    int sum=0,cnt=1;
    state[0]=1;
    for(int i=0;i<N;i++){
        if(S[0][i]!=10000) Q.push(make_pair(-S[0][i],i));
    }
    while(1){
        int a=Q.top().second;
        sum+=-Q.top().first;
        state[a]=1;
        Q.pop();
        cnt++;
        for(int i=0;i<N;i++){
            if(S[a][i]!=10000&&state[i]==0) Q.push(make_pair(-S[a][i],i));
        }
        if(cnt==N) break;
    }
    return sum;
}


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) S[i][j]=10000;
            else S[i][j]=a;
        }
    }
    cout<<prim()<<endl;
}

C++: #include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

#define N 100
#define INF 0x7FFFFFFF

int prim(std::unordered_map<int, int> a[N], const int n){
    std::unordered_set<int> checked;
    int total = 0;
    checked.insert(0);
    while(checked.size() != n){
        int newId = INF;
        int newWeight = INF;
        for(auto &x: checked){
            for(auto &j: a[x]){
                if(checked.find(j.first) == checked.end() && newWeight > j.second){
                    newId = j.first;
                    newWeight = j.second;
                }
            }
        }
        checked.insert(newId);
        total += newWeight;
    }
    return total;
}

int main(){
    int n;
    std::scanf("%d", &n);
    std::unordered_map<int, int> a[N];
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            int v;
            std::scanf("%d", &v);
            if(v != -1){
                a[i].insert(std::make_pair(j, v));
            }
        }
    }
    int val = prim(a, n);
    std::printf("%d\n", val);
    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, A[MAX][MAX];

int prim(){
    int u, minv;
    int d[MAX], p[MAX], color[MAX];

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

    while(1){
        minv = INFTY;
        for(int i = 0; i < n; i++){
            if(color[i] != BLACK && d[i] < minv){
                minv = d[i];
                u = i;
            }
        }

        if(minv == INFTY) break;

        color[u] = BLACK;

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

    int sum = 0;
    for(int i = 0; i < n; i++){
        sum += d[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;
            A[i][j] = (e == -1) ? INFTY : e;
        }
    }

    cout << prim() << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
#define eps 1e-7
//#define Kongxiangzhouye

static const int MAX=100;
static const int INF=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int begin[10000];
int to[10000];
int next[10000];
int weight[10000];
int e,n;
void add(int x,int y,int z){
	to[++e]=y;
	next[e]=begin[x];
	begin[x]=e;
	weight[e]=z;
}

struct p{
	int edge;
	int weight;
	bool operator < (const p& u)const{
		return weight>u.weight;
	}
};
int createtree(){
	priority_queue<p> q;
	int flag[100];
	memset(flag,0,sizeof(flag));
	int te=begin[1];
	while(to[te]!=-1){
		if(flag[to[te]]==0)
			q.push((p){te,weight[te]});
		te=next[te];
	}
	flag[1]=1;
	int ans=0;
	while(1){
		int flag2=1;
		for(int i=1;i<=n;i++){
			if(flag[i]==0){
				flag2=0;
				break;
			}
		}
		if(flag2==1)
			break;
		while(flag[to[q.top().edge]]==1)
			q.pop();
		p now=q.top();
		q.pop();
		ans=ans+now.weight;
		flag[to[now.edge]]=1;
		int te=begin[to[now.edge]];
		while(te!=-1){
			if(flag[to[te]]==0)
				q.push((p){te,weight[te]});
			te=next[te];
		}
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(false);
	#ifdef Kongxiangzhouye
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	while(cin>>n){
		memset(begin,-1,sizeof(begin));
		memset(next,-1,sizeof(next));
		memset(to,-1,sizeof(to));
		e=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int te;
				cin>>te;
				if(te!=-1)
					add(i,j,te);
			}
		}
		cout<<createtree()<<endl;
	}

	return 0;
}