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<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<numeric>
#include<stdio.h>

using std::cout;
using std::cin;
using std::setw;
using std::setfill;
using std::string;
using std::pair;
using std::make_pair;
using std::vector;
using std::queue;
using std::deque;
using std::priority_queue;
using std::next_permutation;
using std::iota;
using std::sort;
using std::greater;
using std::max;
using std::min;

#define INF 999999999
#define MOD 1000000007

#define EVEL 1

#ifndef EVEL
#define dbug(X) std::cout << #X << ":" <<X<<" " ;
#define dbugf(s) std::cout << s << " ";
#define dbugln std::cout<<"\n";
#else
#define dbug(X) {}
#define dbugf(s) {}
#define dbugln {}
#endif

/*const int M = 10000;

class Matrix{
	public:
	int n;//行
	int m;//列
	vector<int> data;
	Matrix(int a, int b, const vector<int> &v){
		n = a;
		m = b;
		data.resize(n*m);
		for(int i = 0; i < n*m; i++){
			data[i] = v[i];
		}
	}

	int show(){
		cout << this->n << "x" << this->m << "Matrix\n"; 
		for(int i = 0; i < this->n; i++){
			for(int j = 0; j < this->m; j++){
				cout << data[i * this->m + j];
				if(j == this->m-1) cout << "\n";
				else cout << " ";
			}
		}
		return 0;
	}
	Matrix operator * (Matrix obj){
		vector<int> rt;
		int a=0;
		for(int i = 0; i < this->n; i++){
			for(int j = 0; j < obj.m; j++){

				for(int k = 0; k < this->m; k++){
					//a += this->data[i * this->m + k] * obj.data[k * obj.m + j];
					a = (a%M + ((this->data[i * this->m + k]%M)*(obj.data[k * obj.m + j]%M))%M)%M;
				}
				rt.push_back(a);
				a = 0;

			}
		}
		Matrix RT(this->n, obj.m, rt);
		return RT;
	}

	Matrix expt(long long int p){
		if(p == 0){
			vector<int> v{1, 0, 0, 1};
			Matrix RT(2, 2, v);
			return RT;
		}else if(p == 1) return *this;
		else if(p%2 == 1){
			Matrix T = this->expt(p-1);
			return *this * T;
		}else{
			Matrix T = this->expt(p/2);
			return T*T;
		}
	}
};*/

class UF{
	vector<int> oya;

	public:
	UF(int s){
		oya.resize(s);
		for(int i = 0; i < s; i++) oya[i] = i;
	}

	int root(int x){
		vector<int> path;
		while(oya[x] != x){
			path.push_back(x);
			x = oya[x];
		}
		for(int i = 0; i < path.size(); i++){
			oya[ path[i] ] = x;
		}
		return x;
	}

	void unite(int x, int y){
		//x <= s, y <= s が保証されているものとする
		int ox = root(x), oy = root(y);
		oya[oy] = ox;
	}

	bool same(int x, int y){
		int ox = root(x), oy = root(y);
		return (ox == oy);
	}
};

int n, vsum=0;

struct path{
	int value;
	int start;
	int end;
};

vector<path> ps;

int main(){
	cin.tie(0);
	std::ios::sync_with_stdio(false);

	cin >> n;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			int a;
			cin >> a;
			if(a >= 0 && i < j){
				path p;
				p.value = a;
				p.start = i;
				p.end = j;
				ps.push_back(p);
			}
		}
	}

	UF uf(n);
	sort(ps.begin(), ps.end(), [](path a, path b){return (a.value < b.value);});
	
	for(auto itr = ps.begin(); itr != ps.end(); itr++){
		if(!uf.same(itr->start, itr->end)){
			uf.unite(itr->start, itr->end);
			vsum+=itr->value;
		}
		dbug(vsum)dbug(itr->value)dbug(itr->start)dbug(itr->end)dbugln;
	}

	cout << vsum << "\n";


	return 0;
}

C++: //compiledef
#include <bits/stdc++.h>
#define rep(i, n)for(i=0;i<(n);i++)
#define per(i, n) for(i=(n);i>0;i--)
#define repx(i, n, x) for(i=(x);i<(n);i++)
#define xper(i, n, x) for(i=(n);i>(x);i--)
#define pback push_back

//c++def
using namespace std;
typedef long long int ll;
const long long int MOD = 1e9+7;
const long long int MOD2 = 998244353;
int w[200][200];
int prim_mst(int n){
	int i;
	int j;
	int cost = 0;
	int d[200];
	int pi[200];
	int color[200];
	rep(i, n){
		d[i]= 2e9;
		pi[i]=-1;
		color[i] = 0;
	}
	d[0] = 0;
	while(29091101){
		int mincost = 2e9;
		rep(i, n){
			if(color[i] != 2 && d[i] < mincost){
				mincost = d[i];
				j = i;
			}
		}
		if(mincost == 2e9)break;
		color[j] = 2;
		for(int v=0; v<n;v++){
			if(color[v] != 2 && w[j][v] < d[v]){
				pi[v] = j;
				d[v] = w[j][v];
			}
		}
	}
	rep(i, n){
		if(pi[i] != -1) cost += w[i][pi[i]];
	}
	return cost;
}

int main(){
	int n;
	int i, j;
	int tmp;
	cin >> n;
	rep(i, n){
		rep(j, n){
			cin >> tmp;
			if(tmp == -1) w[i][j] = 2e9;
			else w[i][j] = tmp;
		}
	}
	cout << prim_mst(n) << endl;
}

C++: //kruskal tree
#include <iostream>
#include <algorithm>
using namespace std;
#define M 999
int parent[M],a[M],b[M];
pair<int,int>node[M];
int root(int a){return parent[a]==a?a:parent[a]=root(parent[a]);}
int unite(int a,int b){
	int x=root(a),y=root(b);
	if(x==y)return 0;
	parent[x]=y;
	return 1;
}
int main(){
	int i,j,s,n,m,x;
	cin>>n;
		for(m=i=0;i<n;i++)for(j=0;j<n;j++)if(cin>>x,~x)a[m]=i,b[m]=j,node[m].first=x,node[m].second=m++;
		sort(node,node+m);
		for(i=0;i<n;i++)parent[i]=i;
		for(s=i=0;i<m;i++)if(unite(a[node[i].second],b[node[i].second]))s+=node[i].first;
	cout<<s<<endl;
}