Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for 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
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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

C++: #include <bits/stdc++.h>
#include <numeric>
#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define SORT_INV(c) sort((c).begin(), (c).end(), greater<int>())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG true
#define LL long long
#define Num 1000000007
// sort(a.begin(), a.end(), std::greater<int>());
using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[n][n] = {0};
    rep(i, n)
    {
        rep(j, n)
        {
            int temp;
            cin>>temp;
            if(temp==-1) temp=INT_MAX;
            a[i][j]=temp;
        }
    }
    int mincost[n];
    bool used[n];
    rep(i, n)
    {
        mincost[i] = INT_MAX;
        used[i] = false;
    }
    //solve by prim
    mincost[0] = 0;
    int res = 0;
    while (true)
    {
        int v = -1;
        rep(i, n)
        {
            if (used[i] == false && (v == -1 || mincost[i] < mincost[v]))
                v = i;
        }
        if (v == -1)
            break;
        used[v] = true;
        res += mincost[v];
        rep(i, n)
        {
            mincost[i] = min(mincost[i], a[v][i]);
        }
    }cout<<res<<endl;
}

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

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100;

int weight[N_MAX][N_MAX];

Vi mst(const int n) {
    bool isrouted[n] = {false};
    Vi route;
    route.push_back(0);
    isrouted[0] = true;
    FOR(t,1,n) {
        int minval = -1;
        int nextnode = -1;
        for (const int i : route)
        FOR(j,0,n) {
            if (isrouted[j]) { continue; }
            const int w = weight[i][j];
            if (w < 0) { continue; }
            if (minval < 0 || minval > w) {
                minval = w;
                nextnode = j;
            }
        }
        route.push_back(nextnode);
        isrouted[nextnode] = true;
    }
    return move(route);
}

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

    int n;
    cin >> n;

    FOR(i,0,n) {
        FOR(j,0,n) {
            cin >> weight[i][j];
        }
    }

    Vi route = mst(n);
    int cost = 0;
    for (auto iter = route.begin(); iter != route.end() - 1; ++iter) {
        cost += weight[*iter][*(iter+1)];
    }
    cout << cost << endl;

    return 0;
}