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 <iostream>
#include <algorithm>
#define N 100
#define INF 2000000000
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

int n;
int M[N][N],color[N],d[N],p[N];
   //M??????????????????color????¨??????¶???(0,1,2),d????????????????°???????????¨????
   //p???MST???????°???¨?????¨??????????????????????????????

void Prim() {
	for (int i = 0;i < n;i++) d[i] = INF;
	d[0] = 0;  //?????????????????????????????§???????????§????´???????????????????????????????
	p[0] = -1;  //??????????????????????????????????????????

	int u;
	while (true) {
		int min = INF;
		for (int i = 0;i < n;i++) {
			if(color[i]!=BLACK&&min>d[i]){
				min = d[i];
				u = i;
			}
		}

	if (min == INF) break;

	color[u] = BLACK;

	for (int v = 0;v < n;v++) 
		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 sum = 0;
	for (int i = 0;i < n;i++) sum += d[i];
	cout << sum << endl;
		
}

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] = INF;
		}

	Prim();

}

C++: #include <iostream>

using namespace std;

static const int N = 100;
static const int INF = 2001;

struct Vertex {
    bool is_visit;
    int min_w;
    int parent;
};

class MST {
private:
    int matrix[N][N];
    Vertex vertex[N];
    int n;

public:
    MST()
    {
        cin >> n;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                cin >> matrix[i][j];
                if(matrix[i][j] == -1)
                    matrix[i][j] = INF;
            }
        }
    }

    void prim()
    {
        for(int i = 0; i < n; i++)
        {
            vertex[i].is_visit = false;
            vertex[i].min_w = INF;
        }
        vertex[0].min_w = 0;
        vertex[0].parent = -1;

        while(true)
        {
            int u = -1;
            int mincost = INF;
            for(int i = 0; i < n; i++)
            {
                if(!vertex[i].is_visit && vertex[i].min_w < mincost)
                {
                    mincost = vertex[i].min_w;
                    u = i;
                }
            }

            if(mincost == INF)
                break;

            vertex[u].is_visit = true;

            for(int v = 0; v < n; v++)
            {
                if(!vertex[v].is_visit && matrix[u][v] != INF)
                {
                    if(matrix[u][v] < vertex[v].min_w)
                    {
                        vertex[v].min_w = matrix[u][v];
                        vertex[v].parent = u;
                    }
                }
            }
        }
    }

    void print()
    {
        int sum = 0;
        for(int i = 0; i < n; i++)
            if(vertex[i].parent != -1)
                sum += matrix[i][vertex[i].parent];
        cout << sum << endl;
    }
};

int main()
{
    MST mst;
    mst.prim();
    mst.print();
}

C++: #include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 105;

int m[105][105];
int n,e;

int prim(int s)
{
    int minv,cnt[MAXN],k,ans = 0;
    for(int i = 1; i <= n; ++i)
        cnt[i] = m[s][i];
    cnt[s] = 0;
    for(int i = 1; i < n; ++i)
    {
        minv = INF;
        for(int j = 1; j <= n; ++j)
        {
            if(cnt[j] && cnt[j]<minv)
                minv = cnt[j],k = j;
        }
        cnt[k] = 0;
        ans += minv;
        for(int j = 1; j <= n; ++j)
            if(m[k][j] < cnt[j])
                cnt[j] = m[k][j];
    }
    return ans;
}

int main(void)
{
    cin>>n;
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            cin>>e;
            m[i][j] = e==-1?INF:e;
        }
    }
    cout<<prim(1)<<'\n';
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct edge{
    int u,v,cost;
    bool operator<(const edge& v) const {
        return cost < v.cost;
    }
};
const int INF = 1001001001;
const edge imEg = {-1,-1,INF};
vector<edge> es(10000, imEg);
int V,E;
vector<int> par;
vector<int> rnk;

void init_union_find(int n) {
    par.resize(n);
    rnk.resize(n);
    for(int i=0; i<n; i++) {
        par[i] = i;
        rnk[i] = 0;
    }
}

int find(int x) {
    if(x != par[x]) par[x] = find(par[x]);
    return par[x]; 
}

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(rnk[x] > rnk[y]) par[y] = x;
    else par[x] = y;
    if(rnk[x] == rnk[y]) rnk[y]++;
}

int kruskal() {
    init_union_find(V);
    sort(es.begin(), es.end());
    int res = 0;
    for(int i=0; i<E; i++) {
        edge e = es[i];
        if(find(e.u) != find(e.v)) {
            unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main() {
    cin >> V;
    E = 0;
    for(int i=0; i<V; i++) {
        for(int j=0; j<V; j++) {
            int w;
            cin >> w;
            if(w != -1) {
                struct edge e = {i,j,w};
                es[E] = e;
                E++;
            }
        }
    }
    cout << kruskal() << endl;
    return 0;
}