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<vector>

struct Edge {
	int row, column, weight;
};

void divide_graph(bool[], int);

int n;
Edge edges[99]{};

int main()
{
	int in_tmp, min{}, matrix[100][100]{};

	std::cin >> n;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j)
		{
			std::cin >> in_tmp;
			matrix[i][j] = in_tmp;
		}

	int rest = n - 1, pre_size = 0;
	std::vector<int> queue{ 0 };
	bool visited[100]{ true };
	for (int i = 1; rest; ++i)
	{
		int now_size = queue.size();
		for (int j = pre_size; j < now_size; ++j)
			for (int k = 0; k < n; ++k)
			{
				int tmp = matrix[queue[j]][k];
				if (tmp != -1 && !visited[k])
				{
					visited[k] = true;
					rest--;
					edges[queue.size() - 1] = { queue[j], k , tmp};
					queue.push_back(k);
				}
			}
		pre_size = now_size;
	}

	int max_edge = -1, pre_max_edge = 0;
	while (max_edge != pre_max_edge)
	{
		pre_max_edge = max_edge;
		max_edge = 0;
		for (int i = 1; i < n - 1; ++i)
			if (edges[i].weight > edges[max_edge].weight)
				max_edge = i;

		bool conne_row[100]{};
		divide_graph(conne_row, max_edge);

		Edge new_edge = edges[max_edge];
		for (int i = 0; i < n; ++i)
		{
			if (!conne_row[i]) continue;
			for (int j = 0; j < n; ++j)
			{
				if (conne_row[j] || matrix[i][j] >= new_edge.weight) continue;
				if (matrix[i][j] == -1) continue;

				new_edge = { i, j, matrix[i][j] };
			}
		}

		edges[max_edge] = new_edge;
	}

	for (int i = 0; i < n - 1; ++i)
		min += edges[i].weight;

	std::cout << min << std::endl;
	
	return 0;
}

void divide_graph(bool conne_row[], int max_edge)
{
	conne_row[edges[max_edge].row] = true;

	std::vector<int> queue = { edges[max_edge].row };
	int pre_size = 0;

	bool visit_edge[100]{};
	visit_edge[max_edge] = true;

	while (pre_size != queue.size())
	{
		int now_size = queue.size();
		for (int j = pre_size; j < now_size; ++j)
		{
			for (int k = 0; k < n - 1; ++k)
			{
				if (visit_edge[k]) continue;
				if (edges[k].row == queue[j])
				{
					visit_edge[k] = true;
					conne_row[edges[k].column] = true;
					queue.push_back(edges[k].column);
				}
				else if (edges[k].column == queue[j])
				{
					visit_edge[k] = true;
					conne_row[edges[k].row] = true;
					queue.push_back(edges[k].row);
				}
			}
		}
		pre_size = now_size;
	}
	return;
}

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

int n;
vector<pair<int,int>> G[110];
vector<int> seen(110, false);

int prim(){
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> qu;
    int sum = 0, size = 0;

    int u=0;
    seen[u]=true;
    size=1;
    while(size<n){
        for(auto e:G[u]) if(!seen[e.second]) qu.push(e); 
        while(1){
            pair<int, int> p=qu.top(); qu.pop();
            if(!seen[p.second]){
                u = p.second;
                sum += p.first;
                seen[u] = true;
                ++size;
                break;
            }
        }
    }
    return sum;
}

int main(){
    cin >> n;
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            int w; cin >> w;
            if(w!=-1) G[i].push_back(make_pair(w, j));
        }
    }

    cout << prim() << '\n';

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

struct unionfind {
    vector<int> data;
    unionfind(int size) : data(size, -1) { }
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

struct edge {
    int from,to,cost;
};

vector<edge> kruscal(vector<edge> & es){
    sort(es.begin(), es.end(), [](edge a, edge b){
            return a.cost < b.cost;
        });
    unionfind uf(es.size());
    vector<edge> res;
    rep(i,es.size()){
        const edge &e = es[i];
        if(!uf.same(e.from, e.to)){
            uf.unite(e.from,e.to);
            res.push_back(e);
        }
    }
    return res;
}

int main(){
    vector<edge> es;
    int n; cin>>n;
    rep(i,n)rep(j,n){
        int c; cin>>c;
        if(c!=-1) es.push_back({i,j,c});
    }
    auto v = kruscal(es);
    int ans = 0;
    rep(i,v .size()){
        ans += v[i].cost;
    }
    cout << ans << endl;
}