Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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;
}

C++: #include <iostream>
#include <utility>
#include <algorithm>
#define MAX 150
#define rep(i,p) for(int i=0;i<p;i++)
#define rep1(i,p) for(int i=1;i<=p;i++)
using namespace std;
int n,temp,a,b,size,ra,rb;
int A[MAX];
int B[MAX];
int W[MAX];
int V[MAX];
pair<int,int> edge[MAX];
int root (int child) {
  int a = child;
  while (V[a] != a) {
    a = V[a];
  }
  return a;
}
int main(){
  size = 0;
  cin >> n;
  rep(i,n) {
    rep(j,n) {
      cin >> W[size];
      if (i <= j && W[size] != -1) { 
        A[size] = i;
        B[size] = j;        
        edge[size].first = W[size];
        edge[size].second = size;
        size++;
      }
    }
  }
  // cout << endl;
  // rep(i,size) {
  //   cout << A[edge[i].second] << " " << B[edge[i].second]
  //        << " " << edge[i].first << endl;;
  // }
  // sort by lenght of edge
  sort(edge, edge+size);
  
  int sum = 0;
  rep(i,n) V[i] = i;
  rep(i,size) {
    // if (edge[i].first == -1) continue;
    temp = edge[i].second;
    // a , b : vertex
    a = A[temp];
    b = B[temp];
    ra = root(a);
    rb = root(b);
    if (ra != rb) {
      // ?????????????????????
      sum += edge[i].first;
      V[ra] = rb;
      // if (ra != a && rb == b){     
      //   V[b] = ra;
      // } else if (ra == a && rb != b) {
      //   V[a] = rb;
      // } else if (ra == a && rb == b) {        
      //   V[a] = b;
      // } else if (ra != a && rb != b) {                
      //   V[ra] = rb;
      // }
    }
  }
  cout << sum << endl;
  return 0;
}