Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

C++: #include <iostream>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1<<21
#define N 100

int n, A[N][N];

int ans() {
	int color[N], p[N], d[N];
	int u, minv;

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

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

		if (u == -1) break;
		color[u] = BLACK;

		for (int i = 0; i < n; i++) {
			if (color[i] != BLACK && A[u][i] != INFTY) {
				if (A[u][i] < d[i]) {
					d[i] = A[u][i];
					p[i] = u;
					color[i] = GRAY;
				}
			}
		}
	}

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

C++: #include <bits/stdc++.h>
using namespace std;
#define INFTY (1<<30)

int n;
vector<int> color(101,0),d(101,INFTY),p(101);
vector<vector<int>> M(101,vector<int>(101,INFTY));

void prim(){
    d[0]=0;
    p[0]=-1;

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

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

    }

}

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>M[i][j];
            if(M[i][j]==-1) M[i][j]=INFTY;
        }
    }

    prim();

    int ans=0;
    for(int i=0;i<n;i++){
        ans+=d[i];
    }
    cout<<ans<<endl;
}

C++: #include <iostream>
#include <queue>
using namespace std;
int main() {
    int n;
    cin >> n;
    int a[100][100];
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j];
    struct Edge {
        int from, to, weight;
        Edge(int from, int to, int weight) : from(from), to(to), weight(weight) {}
        bool operator < (const Edge& e) const {
            return weight > e.weight;
        }
    };
    int sm = 0;
    priority_queue<Edge> q;
    q.push(Edge(-1, 0, 0));
    bool visited[100]{};
    while (q.size()) {
        auto e = q.top();
        q.pop();
        if (visited[e.to]) continue;
        visited[e.to] = true;
        sm += e.weight;
        for (int i = 0; i < n; ++i) {
            if (a[e.to][i] >= 0 && !visited[i])
                q.push(Edge(e.to, i, a[e.to][i]));
        }
    }
    cout << sm << endl;
    return 0;
}

C++: #include<stdio.h>
#include<limits.h>
#define NIL -1
static const int MAX = 500;
static const int INFTY = (1<<21);
int n, i, j, e, v, sum, u , minV;
main(){
 
  int M[MAX][MAX], key[MAX], L[MAX],A[MAX];
  
  scanf("%d", &n);
  
  for ( i = 0; i< n; i++ ){
    for ( j = 0; j < n; j++ ){
      scanf("%d", &e);
      M[i][j] = (e==-1)?INFTY:e;
    }
  }

  for(i = 0; i < n; i++){
    key[i] = INT_MAX;
    L[i] = NIL;
    A[i] = NIL;
  }
  
  key[0]=0;
  
  for(;;){
    
    u = NIL;
    
    minV = INT_MAX;
    
    for(i = 0;  i < n; i++){
      
      if(key[i] < minV && A[i] == NIL){
        
        u = i;
        
        minV = key[i];
        
      }
      
    }  
    if(u == NIL)break;
    
    A[u] = u; 
    
    for(v = 0; v < n; v++){
      if(M[u][v] < key[v] && A[v] == NIL){
        key[v] = M[u][v];
        L[v] = u;
      }
    }
  }
  
  sum = 0;
  
  for ( i = 0; i < n; i++ ){
    if ( L[i] != -1 ) sum +=M[L[i]][i];
  }
  
  printf("%d\n", sum);
  
  
  return 0;
}

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