Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
static const int NMAX = 100;
#define INF (1 << 30)
int ADJ[NMAX][NMAX];

int main() {
  int n;
  cin >> n;
  int tmp;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> tmp;
      ADJ[i][j] = (tmp == -1) ? INF : tmp;
    }
  }

  bool visited[n];
  int edges[n];

  // add node 0
  for (int i = 0; i < n; i++) {
    edges[i] = ADJ[0][i];
    visited[i] = false;
  }
  visited[0] = true;

  int cost = 0;
  while (true) {
    int minnode = -1;
    int mincost = INF;
    // find min edge
    for (int i = 0; i < n; i++) {
      if (visited[i] == false && edges[i] < mincost) {
        minnode = i;
        mincost = edges[i];
      }
    }
    if (minnode == -1)
      break;

    // add node
    visited[minnode] = true;
    cost += mincost;

    // update connected edges
    for (int i = 0; i < n; i++) {
      edges[i] = min(edges[i], ADJ[minnode][i]);
    }
  }
  cout << cost << endl;
}

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