Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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;
}