Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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

int main() {
    int i, j, n, res = 0;
    cin >> n;
    int a[n][n];

    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
            cin >> a[i][j];
        }
    }

    vector<int> v;
    set<int> cons;
    v.push_back(0);
    cons.insert(0);
    while (v.size() != n) {
        int nearest_node = 9999;
        int nearest_distance = 9999;
        for (i=0; i<v.size(); i++) {
            for (j=0; j<n; j++) {
                if ((a[i][j] != -1) && (a[i][j] < nearest_distance) && (cons.count(j) == 0)) {
                    nearest_node = j;
                    nearest_distance = a[i][j];
                }
            }
        }
        res += nearest_distance;
        v.push_back(nearest_node);
        cons.insert(nearest_node);
    }
    cout << res << endl;
    return 0;
}

C++: #include<iostream>
#include<sstream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cfloat>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<bitset>
#include<list>
#include<numeric>
#include<complex>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define sc(x) cin >> x
#define sc2(x, y) cin >> x >> y
#define sc3(x, y, z) cin >> x >> y >> z
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define debugn(i, n, a) rep(i, n)cout << " --- " << a[i] << "\n";
#define debugin(i, n, a) rep(i, n)printf(" --- %10d\n", a[i])
#define debugi2n(i, n, a, b) rep(i, n)printf(" --- %10d %10d\n", a[i], b[i])
#define debugiin(i, n, a) rep(i, n)printf(" --- %10d %10d\n", a[i].first, a[i].second)
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

struct unionfind{
	vector<int> data;
	void use(int n){ data.assign(n, -1); }
	bool unionset(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 findset(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 to, cost;
};

vector<edge> G[100];

bool mycom(pair<edge, int> a, pair<edge, int> b){
  return a.X.cost < b.X.cost;
}

ll kruskal(int v){
  ll res = 0;
  int i, j, sz = 0;
  unionfind U; U.use(v);
  vector< pair<edge, int> > lis;
  rep(i, v)rep(j, G[i].size())lis.pb(mp(G[i][j], i));
  sort(all(lis), mycom);
  for(i = 0;i < lis.size();i++){
    pair<edge, int> tmp = lis[i];
    if(!U.findset(tmp.X.to, tmp.Y)){
      res += tmp.X.cost;
      U.unionset(tmp.X.to, tmp.Y);
    }
  }
  return res;
}

signed main(void){
  int i, j;
  for(int testcase = 0;;testcase++){
    int(n);
    rep(i, n)rep(j, n){
      int(tmp);
      if(tmp != -1)G[i].pb((edge){j, tmp});
    }
    pri(kruskal(n));

/*/

//*/ break;
  }
  return 0;
}

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

static int n;
static std::vector<std::vector<int>> link_mat;

enum STATUS {
    IN,
    OUT,
    EDGE,
};


void updateEdge(int node, std::vector<STATUS> &statuses) {
    statuses[node] = STATUS::IN;
    for (int i = 0; i < link_mat[node].size(); i++) {
        if (link_mat[node][i] == -1) {
            continue;
        }
        if (statuses[i] == STATUS::OUT) {
            statuses[i] = STATUS::EDGE;
        }
    }
}


int search(int node, std::vector<STATUS> &statuses) {
    updateEdge(node, statuses);
    int min = 2001;
    int min_node = -1;
    for (int i = 0; i < n; i++) {
        if (statuses[i] != STATUS::EDGE) {
            continue;
        }
        for (int j = 0; j < n; j++) {
            int w = link_mat[i][j];
            if (w == -1 || w > min) {
                continue;
            }
            if (statuses[j] != STATUS::IN) {
                continue;
            }
            min = w;
            min_node = i;
        }
    }
    if (min_node == -1) {
        return 0;
    }
    return min + search(min_node, statuses);
}


int main() {
    std::cin >> n;
    link_mat = std::vector<std::vector<int>>(n, std::vector<int>(n));
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cin >> link_mat[i][j];
        }
    }
    std::vector<STATUS> statuses(n, STATUS::OUT);
    int cost = search(0, statuses);
    
    std::cout << cost << std::endl;
}