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++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_12_A: Minimum Spanning Tree

#include <algorithm>
#include <cstdio>

using uint = unsigned int;

class Graph {
  const uint n_;
  uint* const g_;
  
public:
  const static uint INFINIT = 2000+1;

  Graph(uint n) : n_(n), g_(new uint[(n_*n_)]) { }

  ~Graph() { delete[] g_; }
  
  uint* operator [](uint i) const { return g_+(n_*i); }

  uint mst()
  {
    uint min[n_];
    std::fill_n(min, n_, INFINIT);
    bool visited[n_];
    std::fill_n(visited, n_, false);
    auto total_weight = 0u;
    auto i = 0u; // 0-start
    visited[i] = true;
    for (auto n = 0u; n < n_-1; ++n) {
      auto cur_min = INFINIT;
      auto es = (*this)[i];
      for (auto j = 0u; j < n_; ++j) {
        if (visited[j]) continue;
        min[j] = std::min(min[j], es[j]);
        if (cur_min > min[j]) {
          cur_min = min[j];
          i = j;
        }
      }
      visited[i] = true;
      total_weight += cur_min;
    }
    return total_weight;
  }
};

const uint Graph::INFINIT;

int main()
{
  uint n;
  std::scanf("%u", &n);

  Graph g(n);

  int e;
  for (auto i = 0u; i < n; ++i) {
    for (auto j = 0u; j < n; ++j) {
      std::scanf("%i", &e);
      g[i][j] = (e != -1) ? static_cast<uint>(e) : Graph::INFINIT;
    }
  }

  auto total_weight = g.mst();
  std::printf("%u\n", total_weight);
}

// eof

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long int;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)

#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

static const int MAX = 100;
static const int White = 0;
static const int Gray = 1;
static const int Black = 2;

int n, M[MAX][MAX],color[MAX];

int prim() {
	int d[MAX], p[MAX];
	int i, sum = 0;
	rep(i, n) {
		d[i] = INF;
		p[i] = -1;
		color[i] = White;
	}
	d[0] = 0;
	while (true) {
		int u = -1;
		int minv = INF;
		rep(i, n) {
			if (color[i] != Black && d[i] < minv) {
				minv = d[i];
				u = i;
			}
		}
		if (u == -1)
			break;
		color[u] = Black;
		int v;
		rep(v, n) {
			if (color[v] != Black && M[u][v] != INF) {
				if (M[u][v] < d[v]) {
					color[v] = Gray;
					d[v] = M[u][v];
					p[v] = u;
				}
			}
		}
	}
	rep(i, n) {
		if (p[i] != -1) {
			sum += M[i][p[i]];
		}
	}
	return sum;
}

signed main() {
	ios::sync_with_stdio(false);
	cin >> n;
	int i,j;
	rep(i, n) {
		rep(j, n) {
			int a;
			cin >> a;
			M[i][j] = (a == -1) ? INF : a;
		}
	}
	cout << prim() << '\n';
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;    using vvi = vector<vi>;
using vb = vector<bool>;   using vvb = vector<vb>;
using vl = vector<ll>;     using vvl = vector<vl>;
using vd = vector<double>; using vvd = vector<vd>;

#define REP(i,n) for(auto i = 0 * n; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i,s,e) for (ll i = s; i < (ll)e; i++)
#define TEN(x) ((ll)1e##x)


struct Edge{ uint32_t to, id; };
typedef vector<vector<Edge>> Graph;

template<class T> struct Prim {
	Prim(const Graph & es, const vector<T> & ws) : g(es.size()) {
		typedef uint32_t U;
		typedef tuple<T, U, U, U> P;
		U n = es.size();
		vector<U> unexp(n, 1);
		priority_queue<P, vector<P>, greater<P>> q; q.emplace(0, n, 0, 0);
		while (!q.empty()) {
			T w; U u, v, i; tie(w, u, v, i) = q.top(); q.pop();
			if (unexp[v]) {
				unexp[v] = 0;
				if (u != n) { g[u].push_back({ v, i }); g[v].push_back({ u, i }); cost += w; }
				for (auto e : es[v]) if (unexp[e.to]) q.emplace(ws[e.id], v, e.to, e.id);
			}
		}
	}
	Graph g; T cost = 0;
};

int main() {
#ifdef INPUT_FROM_FILE
	ifstream cin("sample.in");
	ofstream cout("sample.out");
#endif
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(50);

	uint32_t n; cin >> n;
	Graph g(n);
	vl w;
	REP(i, n) REP(j, n) {
		ll x; cin >> x;
		if (x != -1) {
			g[i].push_back({j, w.size()});
			g[j].push_back({i, w.size()});
			w.push_back(x);
		}
	}
	Prim<ll> mst(g, w);
	cout << mst.cost << endl;
}