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 <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Edge {
    ll s,t;
    ll w;
    bool operator < (const Edge& tgt) const {
	return w > tgt.w;
    }
    void dump(){
	cout << s << " --> " << t << " : " << w << endl; 
    }
};

ll Prim(const vector<vector<Edge>>& G) {
    const ll V = G.size();
    priority_queue<Edge> edges;
    
    vector<int> used(V,0);
    //??????0??????????????????
    used[0] = 1;
    for(auto& e : G[0]) edges.push(e);

    ll ans = 0;
    while( !edges.empty() ) {
	Edge e = edges.top(); edges.pop();

	assert(used[e.s] == 1);
	if( used[e.t] == 1 ) continue;
	used[e.t] = 1;
//	e.dump();
	ans += e.w;

	for(auto& nxt : G[e.t]) {
	    if( used[nxt.t] == 1 ) continue;
	    edges.push(nxt);
	}
    }
    for(auto& e : used) assert(e == 1);

    return ans;
}


int main () {

    ll V;
    cin >> V;
    vector<vector<Edge>> G(V);

    for(int i=0; i<V; i++) {
	for(int j=0; j<V; j++) {
	    ll w; cin >> w;
	    if( w != -1 and w != 0) {
		G[i].push_back( Edge{i,j,w} );
//		G[j].push_back( Edge{j,i,w} );
//		cout << i << " <--> " << j << " : " << w << endl; 
	    }
	}
    }

    ll ans = Prim(G);
    cout << ans << endl;
        
    return 0;
}

C++: #include <iostream> 
#include<vector>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<queue>
#include<deque>
#include<iomanip>
#include<sstream>
#include<cmath>
#include<bitset>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(int i = 1;i <=  n; i++)
#define rrep(i,n) for(int i = n -1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
typedef long long int ll;
typedef long double ld;
const ll INF = (1 << 29);
ll h, m, n; string s;
const ll MOD= 1000000007;
const ld PI = acos(-1);
ll sum = 0;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
    std::fill((T*)array, (T*)(array + N), val);
}int a[110][101];
int prim() {
    vector<int> d(110, INF), p(110, -1),co(110,0);
    d[0] = 0; int u, miv = 0;
    while (1) {
        miv = INF;
        u = -1;
        rep(i, n) {
            if (miv > d[i] && co[i] != 2) {
                u = i; miv = d[i];
            }
        }
        if (u == -1) break;
        co[u] = 2;
        rep(v, n) {
            if (co[v] != 2 && a[u][v] != INF) {
                if (d[v] > a[u][v]) {
                    d[v] = a[u][v];
                    p[v] = u; co[v] = 1;
                }
            }
        }
    }
    rep(i, n) {
        if (p[i] != -1) sum += a[i][p[i]];
    }return sum;
}
int main() {
    cin >> n; 
    rep(i, n) {
        rep(j, n) {
            cin >> a[i][j];
            if (a[i][j] == -1) {
                a[i][j] = INF;
            }
        }
    }
    cout << prim() << endl;
}

C++: #include <iostream>
#include <stdio.h>
using namespace std;

#define MAX 100
#define INF 2097152
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];

int main(void){

	int i,j,f;
	int n,u,v,minv,d[MAX],p[MAX],color[MAX],sum=0;
	scanf("%d",&n);
  
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			scanf("%d",&f);
			if(f == -1){
				M[i][j] = INF;
			}else{
				M[i][j] = f;
			}
		}
	}


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

	d[0] = 0;

	while(1){
		minv = INF;
		u = -1;
		for(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(v=0;v<n;v++){
			if(color[v] != BLACK && M[u][v] != INF){
				if(d[v] > M[u][v]){
					d[v] = M[u][v];
					p[v] = u;
					color[v] = GRAY;
				}
			}
		}
	}

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

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


return 0;

}

C++: #include<iostream>
using namespace std;

const int MAX = 100;

int count(bool check[MAX], int graph[MAX][MAX], int n,int i) {
	check[i] = true;
	int sum = 0;
	for (int j = 0; j < n; j++) {
		if (graph[i][j] != -1 && check[j] == false) {
			sum += count(check,graph, n, j);
			sum += graph[i][j];
		}
	}
	return sum;
}

int main() {
	int n;
	int g;
	int graph[MAX][MAX];

	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> graph[i][j];
		}
	}

	//?§????
	for (int s = 0; s < n; s++) {
		//?????±???
		for (int m = 0; m < n; m++) {
			if (graph[s][m] == -1)continue;
			//??????
			for (int e = 0; e < n; e++) {
				if (graph[s][e] == -1 || graph[m][e] == -1)continue;
				if (graph[s][e] > graph[s][m] + graph[m][e]) {
					graph[s][e] = -1;
				}
			}
		}
	}

	bool check[MAX] = { false };
	cout << count(check, graph, n, 0);
	return 0;
}

C++: #include<stdio.h>
  
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define NIL -1
#define INF 100000

int n,G[MAX][MAX];

int prim(){
  int u,mincost,i;
  int d[MAX],pi[MAX],color[MAX];
  
  for( u = 0 ; u < n ; u++ ){
    d[u] = INF;
    pi[u] = NIL;
    color[u] = WHITE;
  }
  
  d[0] = 0;
  
  while(1){
    mincost = INF;
    i = NIL;
    for( u = 0 ; u < n ; u++ ){
      if(color[u] != BLACK && d[u] < mincost){
    i = u;
    mincost = d[u];
      }
    }
  
    if( i == NIL ) break;
  
    color[i] = BLACK;
  
    for( u = 0 ; u < n ; u++ ){
      if(color[u] != BLACK && G[i][u] != INF ){
    if( d[u] > G[i][u] ){
      d[u] = G[i][u];
      pi[u] = i;
      color[u] = GRAY;
    }
      }
    }
  }
  
  int sum = 0;
  
  for( u = 0 ; u < n ; u++ ){
    if( pi[u] != NIL ) sum += G[u][pi[u]];
  }
  
  return sum;
} 

  
int main(){
  int i,j,m;
  scanf("%d",&n);
  
  for( i = 0 ; i < n ; i++ ){
    for( j = 0 ; j < n ; j++ ){
      scanf("%d",&m);
      if(m==NIL) G[i][j]=INF;
      else G[i][j]=m; 
    }
  }
  
  printf("%d\n",prim());
  
  return 0;
}