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++: #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, M[MAX][MAX];

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

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

  while(1){
    minv=INFTY;
    u=-1;
    for(int i=0;i<n;i++){
      if(minv>d[i] && color[i] !=BLACK){
        u=i;
        minv=d[i];
      }
    }
    if(u==-1)break;
    color[u]=BLACK;
    for(int 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;
        }
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    if(p[i]!=-1)sum+=M[i][p[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;
    M[i][j]= (e==-1)? INFTY:e;
    }
  }

  cout << prim() << endl;
}

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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll;  // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll;     // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define TS to_string
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)


ll INF = 1e9;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b;}     //最小公倍数

void yes(){ cout << "Yes" << endl;}
void no(){ cout << "No" << endl;}

//-----------------------------------------
const int MAX = 100;
int n;
int M[MAX][MAX];

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

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

  d[0] = 0;

  while(1){
    minv = INF;
    u = -1;
    for(int i=0;i<n;i++){
      if(minv > d[i] && color[i] != 2){
        u = i;
        minv = d[i];
      }
    }
    if(u == -1) break;
    color[u] = 2;

    for(int v=0;v<n;v++){
      if(color[v] != 2 && M[u][v] != INF){
        if(d[v] > M[u][v]){
          d[v] = M[u][v];
          p[v] = u;
          color[v] = 1;
        }
      }
    }

  }
  int sum = 0;
  for(int i=0;i<n;i++){
    if(p[i] != -1) sum += M[i][p[i]];
  }

  return sum;
}
//-----------------------------------------

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> n;

  REP(i,n){
    REP(j,n){
      int e; cin >> e;
      if(e == -1) M[i][j] = INF;
      else M[i][j] = e;
    }
  }

  cout << prim() <<endl;









  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 2000000000;
int main() {
	int n; cin >> n;
	int m[101][101];
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
		cin >> m[i][j];
	int res = 0;
	int *d = new int[n];
	fill(d, d + n, INF); 
	d[0] = 0;
	bool *visit = new bool [n];
	fill(visit, visit + n, false);
	priority_queue <P, vector<P>, greater<P> > q;
	q.push(P(d[0], 0));
	while (!q.empty()) {
		int f = q.top().first, s = q.top().second;
		visit[s] = true;
		res += f;
		q.pop();
		for (int i = 0; i < n; i++) {
			if (!visit[i]) {
				if (m[s][i] != -1) {
					if (m[s][i] < d[i]) {
						d[i] = m[s][i];
						q.push(P(d[i], i));
					}
				}
				}
			}
		}
	cout << res << endl;
	delete[] d, visit;
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
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;
//?????\?????????M[u][v]?????°???u??°v???????????????
int M[MAX][MAX];

struct segment{
    //??????
    int start;
    //??????
    int end;
    //??????
    int dis;

    segment(int s,int e,int d):start(s),end(e),dis(d){}

    bool operator < (const segment& p)const {
        return dis<p.dis;
    }
};

int prim(){
    //????????¶???
    int u=0;
    //??¨?????°???MST?????¶???V?????¶??????
    int p[MAX];
    //??¨?????°???v???????????¶???WHITE???GRAY???BLACK
    int color[MAX];
    //??¨?????°????????\T?????¶??????V-T?????¶?????????????????????????°????????????????
    priority_queue<segment,vector<segment>> d;

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

    while(true){
        bool ok=true;
        for(int i=0;i<n;i++)
            if(color[i]==WHITE){
                ok= false;
                break;
            }
        if(ok)
            break;

        color[u]=BLACK;
        for(int i=0;i<n;i++)
            if(i!=u&&color[i]==WHITE&&M[u][i]!=INFTY){
                color[i]=GRAY;
                d.push(segment(u,i,M[u][i]));
            }
        u=d.top().end;
        p[u]=d.top().start;
        d.pop();
    }

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

    cout<<prim()<<endl;
}