Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

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