Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] > 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A > edges[j].A })

	sum := 0
	for len(edges) > 0 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

C++: #include<iostream>

using namespace std;

int n, M[100][100], W = 0, G = 1, B = 2;

int prim () {

    int u, minv, i, v, d[100], p[100], color[100], sum;

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

    d[0] = 0;

    while (1) {

        minv = (1 << 21);
        u = -1;
        for (i = 0; i < n; i++) {
            if (minv > d[i] && color[i] != B) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1) {
            break;
        }
        color[u] = B;
        for (v = 0; v < n; v++) {
            if (color[v] != B && M[u][v] != (1 << 21)) {
                if (d[v] > M[u][v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = G;
                }
            }
        }

    }

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

    return sum;

}

int main () {

    int i, j, e;

    cin >> n;

    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            cin >> e;
            M[i][j] = (e == -1) ? (1 << 21) : e;
        }
    }

    cout << prim () << endl;

    return 0;

}

C++: #define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#include <bits/stdc++.h>
using namespace std;

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 1000000010

  int n;
  int M[100][100];
  int color[100];
  int d[100];
  int p[100];
  int mincost;

void prim(){
  rep(u,n) color[u]=WHITE;
  rep(u,n) d[u]=INF;
  d[0]=0;
  p[0]=-1;
  
  int u;
  while(1){
    mincost=INF; 
    rep(i,n){
      if(color[i] != BLACK && d[i]<mincost){
        mincost=d[i];
        u=i;
      }
    }
    
    if(mincost==INF) break;
    
    color[u]=BLACK;
    
    rep(v,n){
      if(color[v] != BLACK && M[u][v] != -1){
        if(M[u][v]<d[v]){
          d[v]=M[u][v];
          p[v]=u;
          color[v]=GRAY;
        }
      }
    }
  }
}

int main(){
  cin>>n;
  
  rep(i,n){
    rep(j,n) cin>>M[i][j];
  }
  
  prim();
  int sum=0;
  rep(i,n) sum+=d[i];
  
  cout<<sum<<endl;
  
  return 0;
}

C++: #include<iostream>

using namespace std;

int n, M[100][100], W = 0, G = 1, B = 2;

int prim () {

    int u, minv, i, v, d[100], p[100], color[100], sum;

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

    d[0] = 0;

    while (1) {

        minv = (1 << 21);
        u = -1;
        for (i = 0; i < n; i++) {
            if (minv > d[i] && color[i] != B) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1) {
            break;
        }
        color[u] = B;
        for (v = 0; v < n; v++) {
            if (color[v] != B && M[u][v] != (1 << 21)) {
                if (d[v] > M[u][v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = G;
                }
            }
        }

    }

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

    return sum;

}

int main () {

    int i, j, e;

    cin >> n;

    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            cin >> e;
            M[i][j] = (e == -1) ? (1 << 21) : e;
        }
    }

    cout << prim () << endl;

    return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;

#define INF 100000000

//訪問状態
#define White 0
#define Gray 1
#define Black 2

//Treeの状態を表した隣接行列
int Tree[100][100];
int sum = 0;

int prim(int n){
  int u, minv;
  //d = 見て来たノードの中から出る最小の辺を記録
  //p = MSTにおける頂点の親を記録
  int d[100], p[100], color[100];

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

  d[0] = 0;

  while(1){
    minv = INF;
    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 && Tree[u][v] != INF){
	if(d[v] > Tree[u][v]){
	  d[v] = Tree[u][v];
	  p[v] = u;
	  color[v] = Gray;
	}
      }
    }
  }

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

  return sum;

}

int main(){
  int n, status;

  cin >> n;

  for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < n ; j++){
      cin >> status;

      if(status == -1){
	Tree[i][j] = INF;
      }else{
	Tree[i][j] = status;
      }
    }
  }

  cout << prim(n) << endl;

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int INFTY = 2000000000;
static const int NIL = -1;
static const int MAX = 100;
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;   //d[i]??????T??????????????§?????????V????±?????????????i?????????????????????
        p[i] = NIL;
        color[i] = WHITE;
    }

    d[0] = 0;

    while(1){
        minv = INFTY;
        u = NIL;
        for(int i=0;i<n;i++){
            if(d[i] < INFTY && color[i] != BLACK){    //?¢????????????§????????????????????????INFTY??§?????????????????¢???
                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){  //2??????????????¶???????????????????
                if(M[u][v] < d[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(){
    scanf("%d",&n);

    for(int i=0;i<n;i++){
        for(int k=0;k<n;k++){
            int e; scanf("%d",&e);
            M[i][k] = (e==-1) ? INFTY : e;
        }
    }

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

    return 0;
}