Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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

struct Data{

  int weight;
  int s,g;

  bool operator < (const Data& p)const {
    if(weight != p.weight)return weight > p.weight;
    if(s != p.s)return s > p.s;
    return g > p.g;
  }
  
};

int par[100],rank[100];
priority_queue<Data> data;

int find(int x){
  
  if( x == par[x]){
    return x;
  }else{
    par[x]=find(par[x]);
  }
  return par[x];
}
bool same(int x,int y){
  if(find(x) == find(y)){
    return true;
  }else{
    return false;
  }
}
void unite(int x,int y){
  
  int x_oya,y_oya;
  
  x_oya=find(x);
  y_oya=find(y);
  
  if(rank[x_oya] > rank[y_oya]){
    par[x_oya]=par[y_oya];
  }else if(rank[x_oya] < rank[y_oya]){
    par[y_oya]=par[x_oya];
  }else{
    par[y_oya]=par[x_oya];
    rank[x_oya]++;
  }
  
}
int kruskal(void){
  
  Data a;
  int cost=0;
  
  while(!data.empty()){
    a=data.top();
    data.pop();
    if(!same(a.s,a.g)){
      cost+=a.weight;
      unite(a.s,a.g);
    }
  }
  return cost;
  
}
int main(){
  
  int n,weight;
  
  for(int i=0 ; i < 100 ; i++){
    par[i]=i;
    rank[i]=0;
  }
  
  cin >>n;

  for(int i=0 ; i < n ; i++){
    for(int j=0 ; j < n ; j++){
      cin >>weight;
      if(weight == -1)continue;
      data.push((Data){weight,i,j});
    }
  }
  
  cout <<kruskal()<<endl;

  return 0;
}

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

const int N=105;
int g[N][N];
int dist[N];
bool vis[N];
int n, ans=0;

void prim(){
    dist[0]=0;
    for(int i=0; i<n; ++i){
        int node=-1;
        for(int j=0; j<n; ++j){
            if(!vis[j] && (node==-1 || dist[j]<dist[node])) node=j;
        }
        if(node==-1) break;
        vis[node]=true;
        ans+=dist[node];
        for(int j=0; j<n; ++j) 
            if(g[node][j]!=-1) dist[j]=min(dist[j], g[node][j]);
    }
}

int main(){
    memset(dist, 0x3f, sizeof dist);
    memset(vis, 0x00, sizeof vis);
    cin>>n;
    for(int i=0; i<n; ++i)
        for(int j=0; j<n; ++j)
            cin>>g[i][j];
            
    prim();
    cout<<ans<<endl;
    return 0;
}

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

#define N_MAX 100
#define WHITE 1
#define GREY 2
#define BLACK 3
#define INF 1000000000
#define NIL -1

int G[N_MAX][N_MAX];
int V[N_MAX];
int V_value[N_MAX];

void init(void){
    for(int i = 0; i < N_MAX; i++){
        V[i] = WHITE;
        V_value[i] = INF;
    }
}

int main(void){
    init();
    int n;
    scanf("%d\n", &n);

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            scanf("%d", &G[i][j]);
        }
    }

    V[0] = BLACK;
    V_value[0] = 0;
    int new_node = 0;
    int sum = 0;
    while(1){
        for(int i = 0; i < n; i++){
            if(G[new_node][i] != -1 && V[i] == WHITE){
                V_value[i] = min(V_value[i], G[new_node][i]);
            }
        }

        int min_value = INF;
        int min_index = NIL;
        for(int i = 0; i < n; i++){
            if(V[i] == WHITE){
                if(V_value[i] < min_value){
                    min_index = i;
                    min_value = V_value[i];
                }
            }
        }
        if(min_index == NIL){
            break;
        }else{
            V[min_index] = BLACK;
            new_node = min_index;
            sum += min_value;
        }
    }

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

C++: #include <algorithm>
#include <cstdint>

using namespace std;

enum status_t {
  WHITE,
  GRAY,
  BLACK,
};

#define GRAPH_SIZE 101

static uint32_t graph[GRAPH_SIZE][GRAPH_SIZE];
static status_t state[GRAPH_SIZE];
static uint32_t shortest_path[GRAPH_SIZE];

size_t minimum_spanning_tree(size_t num)
{
  size_t minimum_cost = 0;

  for (uint32_t i = 0; i < num; i++) {
    shortest_path[i] = UINT32_MAX;
    state[i] = WHITE;
  }

  shortest_path[0] = 0;
  state[0] = GRAY;

  while (true) {
    uint32_t minimum = UINT32_MAX;
    uint32_t to = 0;
    for (uint32_t j = 0; j < num; j++) {
      if ((shortest_path[j] < minimum) && state[j] != BLACK) {
        minimum = shortest_path[j];
        to = j;
      }
    }

    if (minimum == UINT32_MAX) break;

    state[to] = BLACK;
    minimum_cost += minimum;

    for (uint32_t j = 0; j < num; j++) {
      if (state[j] != BLACK) {
        shortest_path[j] = min(shortest_path[j], graph[to][j]);
      }
    }
  }

  return minimum_cost;
}

#include <iostream>

int32_t main()
{
  uint32_t num;
  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    for (uint32_t j = 0; j < num; j++) {
      int32_t val;
      cin >> val;
      if (val == -1) {
        graph[i][j] = UINT32_MAX;
      } else {
        graph[i][j] = val;
      }
    }
  }

  cout << minimum_spanning_tree(num) << endl;

  return 0;
}

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

const int MAX_V = 100010;
struct edge{ int cost, to; };


int V;
vector<edge> G[MAX_V];
bool used[MAX_V];

auto compare = [](edge a, edge b){
    return a.cost > b.cost;
};

int res = 0;
int prim_pq(int s){
    for(int i = 0; i < V; i++) used[i] = false;
    used[s] = true;
    
    priority_queue<edge,vector<edge>,decltype(compare)> pq{compare};
    for(int i = 0; i < G[s].size(); i++) pq.push(G[s][i]);
    
    while(!pq.empty()){
        edge e = pq.top(); pq.pop();
        if(used[e.to]) continue;
        used[e.to] = true;
        res += e.cost;
        for(int i = 0; i < G[e.to].size(); i++) pq.push(G[e.to][i]);
    }
    return res;
}

int main(){
    cin >> V;
    for(int i = 0; i < V; i++){
        for(int j = 0; j < V; j++){
            int x; cin >> x;
            if(x == -1) continue;
            edge e;
            e.to = j; e.cost = x;
            G[i].push_back(e);
        }
    }
    cout << prim_pq(0) << endl;
    return 0;
}