Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define rrep(i,n) for(int i=(int)n-1;i>=0;--i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<typename T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T& a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U& u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U& u,const V... v){for(auto& e:u)fill_v<T>(e,v...);}

struct unionfind {
    vector<int> d;
    unionfind(int n=0) : d(n,-1) {}
    int find(int x) {
        if (d[x] < 0) return x;
        return d[x] = find(d[x]);
    }
    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return false;
        if (d[x] > d[y]) swap(x, y);
        d[x] += d[y];
        d[y] = x;
        return true;
    }
    bool same(int x, int y) { return find(x) == find(y);}
    int size(int x) { return -d[find(x)];}
};

struct edge{ int u, v; long long weight;};
bool comp(const edge &e1, const edge &e2) { return e1.weight < e2.weight;}
vector<edge> es;

long long kruskal(int n) {
    sort(es.begin(), es.end(), comp);
    unionfind uf(n);
    long long res = 0;
    for (size_t i = 0; i < es.size(); ++i) {
        edge e = es[i];
        if (!uf.same(e.u, e.v)) {
            uf.unite(e.u, e.v);
            res += e.weight;
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    rep(i, n) rep(j, n) {
        int w;
        cin >> w;
        if (w >= 0) es.push_back({i, j, w});
    }
    cout << kruskal(n) << endl;
}

C++: //?????????????????????????¨???????pair???????????¨???????????????????????£????????§31-179573????????????????????????????????????????????????????????????init(n)???????????????????????¨????°????????????????30?????????????????????????????£??????
#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
using namespace std;

int P[10010];
void init(int N){
  for (int i=0;i<=N;i++) P[i]=i;
}

int root(int a){
  if(P[a]==a) return a;
  return(P[a] = root(P[a]));
}

bool is_same_set(int a,int b){
  return root(a) == root(b);
}
void unite(int a,int b){
  P[root(a)] = root(b);
}

int main(){
  int n;
  cin>>n;
  init(n);
  int A[101][101];
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin >> A[i][j];
    }
  }
  int cost[10010],src[10010],dst[10010];
  int M=0;
  for(int r=0;r<n;r++){
    for(int c=r+1;c<n;c++){
      if(A[r][c]>-1){
	cost[M]=A[r][c];
	src[M]=r;
	dst[M]=c;
	M+=1;
      }
    }
  }
  pair<int,int> edges[10010];
  for(int i=0;i<M;i++){
    edges[i] =make_pair(cost[i],i);
  }

  sort(edges,edges+M);
  int s=0;
  for(int i=0;i<M;i++){
    int k= edges[i].second;
    int p=src[k];
    int q=dst[k];
    if(is_same_set(p,q)) {
      continue;
    }
    unite(p,q);
    s+=edges[i].first;
  }
  cout << s<<endl;
}

C++: 
#include<stdio.h>

#define MAX 1000
#define INFINITY 10000

int n;
int M[MAX][MAX], color[MAX], d[MAX], p[MAX];

enum {
    WHITE,
    GRAY,
    BLACK
};

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

    while (true) {
        int min = INFINITY;
        int target = -1;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min && color[i] != BLACK) {
                min = d[i];
                target = i;
            }
        }
        if (target == -1) break;

        color[target] = BLACK;

        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && M[target][v] > 0) {
                if(M[target][v] < d[v]) {
                    d[v] = M[target][v];
                    p[v] = target;
                    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 j = 0; j < n; ++j) {
            scanf("%d", &M[i][j]);
        }
    }
    printf("%d\n", prim());
    return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 110
#define INF 0x3f3f3f3f
int m[N][N];
int used[N];
int d[N];
int n;
int sum = 0;
void prim(){
    used[1] = 1;
    d[1] = 0;
    for(int k = 1; k < n; k++){
        int min = INF, u;
        for(int i = 1; i <= n; i++){
            if(!used[i] && d[i] < min){
                min = d[i];
                u = i;
            }
        }

        used[u] = 1;
        sum += d[u];
        for(int v = 1; v <= n; v++){
            if(!used[v] && m[u][v] < d[v]){
                d[v] = m[u][v];
            }
        }
    }
    printf("%d\n", sum);
}
int main(){

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

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            int p;
            scanf("%d", &p);
            if(p != -1) m[i][j] = p;
        }
    }
    for(int i = 1; i <= n; i++) d[i] = m[1][i];
    prim();
    return 0;
}