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 <algorithm>
#include <vector>
#include <iostream>

typedef std::vector<int> TTree;
struct TPath
{
    int Id1;
    int Id2;
    int Cost;
    TPath(int AId1, int AId2, int ACost)
     : Id1(AId1), Id2(AId2), Cost(ACost)
    {}
};
bool Compare(const TPath& Left, const TPath& Right)
{
    if(Left.Cost  < 0) return false;
    if(Right.Cost < 0) return true;
    return Left.Cost < Right.Cost;
}


int main()
{
    int n;
    std::cin >> n;
    std::vector<TTree*> Table(n, NULL);
    for(std::size_t i = 0; i < n; ++i){
        Table[i] = new std::vector<int>(1, i);
    }
    std::vector<TPath> PathList;
    PathList.reserve(n*n);
    
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            int Cost;
            std::cin >> Cost;
            PathList.push_back(TPath(i, j, Cost));
        }
    }
    std::sort(PathList.begin(), PathList.end(), Compare);
    
    int Total = 0;
    for(std::vector<TPath>::const_iterator It = PathList.begin();
        It != PathList.end(); ++It)
    {
        const TPath& Path = *It;
        TTree* Tree1 = Table[Path.Id1];
        TTree* Tree2 = Table[Path.Id2];
        if(Tree1 == Tree2){
            continue;
        }
        Tree1->insert(Tree1->end(), Tree2->begin(), Tree2->end());
        for(std::size_t j = 0; j < Tree2->size(); ++j){
            int Id = (*Tree2)[j];
            Table[Id] = Tree1;
        }
        delete Tree2;
        Total += Path.Cost;
        // std::cout << "[" << Path.Id1 << "]" << "[" << Path.Id2 << "] = " << Path.Cost << std::endl;
    }
    std::cout << Total << std::endl;
    return 0;
}

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

int root(int a,int P[])
{
    if(P[a] == a)
        return a;
    else    return (P[a] = root(P[a],P));
}
int is_same_set(int a,int b,int P[])
{
    return root(a,P) == root(b,P);
}

int main()
{
    int n,i,j,M=0,minsum=0;
    cin >> n;
    //init(n);
    int a[n][n],cost[n*n],src[n*n],dst[n*n];
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            cin >> a[i][j];
    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++;
            }
    pair<int,int> edeges[M];
    int P[n];
    for(i=0;i<n;i++)
        P[i] = i;
    for(i=0;i<M;i++)
    {
        edeges[i].first = cost[i];
        edeges[i].second = i;
    }
    sort(edeges,edeges+M);
    for(i=0;i<M;i++)
    {
        if(is_same_set(src[edeges[i].second], dst[edeges[i].second], P))
            continue;
        else    {
                    P[root(src[edeges[i].second],P)] = root(dst[edeges[i].second],P);
                    minsum += edeges[i].first;
                }
    }
    cout << minsum << endl;
    return 0;
}

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

vector< vector < int > > adjMatrix;

int findMSTCost(int n) {
  vector< bool > visited (n, false);
  visited[0] = true;
  int cost = 0;
  for (int numberVertices = 1; numberVertices < n; numberVertices++) {
    int minEdge = 3000, minVertex = n;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
	if ((i != j) &&
	    (visited[i] == true) &&
	    (visited[j] == false) &&
	    (adjMatrix[i][j] != -1) &&
	    (adjMatrix[i][j] < minEdge)) {
	  minEdge = adjMatrix[i][j];
	  minVertex = j;
	}
      }
    }
    cost += minEdge;
    visited[minVertex] = true;
  }
  return cost;
}

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  adjMatrix = vector< vector < int > > (n, vector< int > (n, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> adjMatrix[i][j];
  cout << findMSTCost(n) << endl;
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

struct UnionFind {
    UnionFind(int n) {
        rank.assign(n, 0);
        parent.resize(n);
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
    }

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

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    void unit(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)
            return;

        if (rank[x] < rank[y]) {
            parent[x] = y;
        }
        else {
            parent[y] = x;
            if (rank[x] == rank[y])
                rank[x]++;
        }
    }

    vector<int> rank;
    vector<int> parent;
};

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

    ll N;
    cin >> N;
    priority_queue<tuple<pll, ll>, vector<tuple<pll, ll>>, function<bool(tuple<pll, ll>, tuple<pll, ll>)>> q(
            [](tuple<pll, ll> a, tuple<pll, ll> b) {
                return get<1>(a) > get<1>(b);
            });
    REP(i, N) {
        REP(j, N) {
            ll a;
            cin >> a;
            if (a < 0)
                continue;
            q.push(make_tuple(pll(i, j), a));
        }
    }
    UnionFind uf(N);
    ll ans = 0;
    while (!q.empty()) {
        auto t = q.top();
        q.pop();
        pll p = get<0>(t);
        if (uf.same(p.first, p.second))
            continue;
        uf.unit(p.first, p.second);
        ans += get<1>(t);
    }
    cout << ans << endl;
    return 0;
}