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++: /* primのアルゴリズム */
#include<iostream>
#include<stdio.h>
using namespace std;


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

int color[N_MAX];
int M[N_MAX][N_MAX];
int d[N_MAX];
int p[N_MAX];
int n;


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

    while(true)
    {
        minv=INFTY;
        //T(最小全域木)に追加するノードuの探索
        for(i=0;i<n;i++)
        {
            //未選択でT-Vの接続がもっとも小さいノードの選択
            if(color[i]!=BLACK && d[i]<minv)
            {
                minv=d[i];
                u=i;
            }
        }

        //もし最小接続コストがINFTYなら探索終了
        if(minv==INFTY) break;

        //Tに追加されたuは選択済みのフラグを立てる
        color[u]=BLACK;

        //元グラフVからu-vで最小のコストのエッジをd[v]に保存
        //親も保存
        //探査はした証拠にGRAYにする
        //ここでの代入操作は次のループで上のfor文内で使われる
        for(v=0;v<n;v++)
        {
            //未選択でuとのエッジが存在するvについて
            if(color[v]!=BLACK && M[u][v]!=INFTY)
            {
                //そのエッジコストがvが記録する
                //T - (V-T)間の最小コストより小さければ
                //エッジコスト更新
                //vの親の更新
                if(M[u][v]<d[v])
                {
                    d[v]=M[u][v];
                    p[v]=u;
                    color[v]=GRAY;
                }
            }
        }

    }

    int sum=0;
    for(i=0;i<n;i++)
    {
        //親が存在すれば、i-p[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++)
        {
            int e;
            scanf("%d",&e);
            M[i][j]=(e==-1) ? INFTY : e ;
        }
    }

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

    return 0;
}

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

const int NOTV = 0;
const int VIST = 1;
const int NMAX = 100;
const int VMAX = 2000;
const int NILL = -1;
const int INFTY = 1 << 21;
int n;
int a[NMAX][NMAX];  //??£??\??????
int v[NMAX];  //????°???¨?????¨?????????
int d[NMAX];  //????°???¨?????¨??????????????????
int vis[NMAX];
//priority_queue <greater<int>> pq;

int prim() {
	for (int i = 0; i < n; i++) {
		vis[i] = NOTV;
		d[i] = INFTY;
		v[i] = -1;
	}
	d[0] = 0;
	int u = 0;
	while (1) {
		int mincost = INFTY;
		u = -1;
		for (int i = 0; i < n; i++) {
			if (vis[i] == NOTV and mincost > d[i]) {
				mincost = d[i];
				u = i;
			}
		}
		if (u == -1) break;
		vis[u] = VIST;
		for (int ver = 0; ver < n; ver++) {
			if (vis[ver] == NOTV and a[u][ver] != INFTY) {
				if (a[u][ver] < d[ver]) {
					d[ver] = a[u][ver];
					v[ver] = u;
					vis[ver] = NOTV;
				}
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] != -1) ans += a[i][v[i]];
	}
	return ans;
}

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;
			if (a[i][j] == -1) {
				a[i][j] = INFTY;
			}
		}
	}
	
	cout << prim() << endl;
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <list>
#include <utility>
#define N 100
using namespace std;

typedef pair<int,int> pii;
int mat[N][N];
bool color[N];
int n;
int sum=0;
priority_queue<pii> pq;

int mini(int k){
	
	color[k]=true;
	for(int i=0;i<n;i++){
		if(mat[k][i]!=-1)pq.push(make_pair(-mat[k][i],i));
	}
	while(!pq.empty()){
		pii temp=pq.top();pq.pop();
		temp.first=-temp.first;
	//	cout<<"popping:"<<temp.first<<" "<<temp.second<<endl;
		if(color[temp.second])continue;
		color[temp.second]=true;
		sum+=temp.first;
	//	for(int i=0;i<n;i++) cout<<i<<":"<<((color[temp.second])?"t ":"f ");
//		cout<<endl;
		for(int i=0;i<n;i++){
			if(mat[temp.second][i]!=-1 && !color[i])pq.push(make_pair(-mat[temp.second][i],i));
		}
	}
	cout<<sum<<endl;
	return 0;
}
int main(void){
	cin>>n;
	for(int i=0;i<n;i++)color[i]=false;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>mat[i][j];
		}
	}
	mini(0);
	return 0;
}