Go: package main

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

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: /*
    与えられた重み付きグラフ G=(V,E) に対する最小全域木の辺の重みの総和を計算するプログラムを作成してください。

    入力
    最初の行に G の頂点数 n が与えられます。続く n 行で G を表す n×n の隣接行列 A が与えられます。A の要素 aij は、頂点 i と頂点 j を結ぶ辺の重みを表します。ただし、辺がなければ-1 で示されます。

    出力
    G の最小全域木の辺の重みの総和を１行に出力してください。

    制約
    1≤n≤100
    0≤aij≤2,000 (aij≠−1 のとき)
    aij=aji
    グラフ G は連結である。
*/

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>

using namespace std;

#define SWAP(type, a, b) do { type t = a; a = b; b = t; } while(0)

typedef struct {
    int weight;
    int to;
} Edge;

typedef struct {
    Edge *edge;
    int num;
} Heap;

void makeMinHeap(Heap *heap, int idx);
void insert(Heap *heap, int key);
int extract(Heap *heap);

static inline int getParent(int idx) { return idx / 2; }
static inline int getLeft(int idx) { return idx * 2; }
static inline int getRight(int idx) { return idx * 2 + 1; }
static inline bool isCorrectHeapIdx(int idx, int heapSize) { return idx <= heapSize && 1 <= idx; }
static inline bool isEmpty(Heap *heap) { return heap->num <= 0; }

void makeMinHeap(Heap *heap, int idx) {
    int left = getLeft(idx);
    int right = getRight(idx);

    int minIdx = idx;
    if(isCorrectHeapIdx(left, heap->num) && heap->edge[minIdx].weight > heap->edge[left].weight) {
        minIdx = left;
    } if(isCorrectHeapIdx(right, heap->num) && heap->edge[minIdx].weight > heap->edge[right].weight) {
        minIdx = right;
    }

    if(minIdx != idx) {
        SWAP(Edge, heap->edge[minIdx], heap->edge[idx]);
        makeMinHeap(heap, minIdx);
    }
}

void insert(Heap *heap, Edge* key) {
    heap->edge[++heap->num] = *key;
    for(int i = getParent(heap->num); i >= 1; i = getParent(i)) {
        makeMinHeap(heap, i);
    }
}

void extract(Heap *heap, Edge* key) {
    *key = heap->edge[1];
    heap->edge[1] = heap->edge[heap->num--];
    makeMinHeap(heap, 1);
}

int n;
int* weight;

static inline bool isConnected(int s, int t) {
    return weight[s * n + t] != INT_MAX;
}

static inline int getWeight(int s, int t) {
    return weight[s * n + t];
}

int getMimimumSpanningTree() {
    bool* isAddMst = new bool[n]();
    Heap connectEdge;
    Edge edge;
    int sum = 0;
    int v = 0;

    connectEdge.edge = new Edge[n * n];
    connectEdge.num = 0;
    
    while(1) {
        isAddMst[v] = true;

        for(int i = 0; i < n; i++) {
            if(isConnected(v, i) && !isAddMst[i]) {
                edge.weight = getWeight(v, i);
                edge.to = i;
                insert(&connectEdge, &edge);
            }
        }

        while(1) {
            if(isEmpty(&connectEdge)) {
                return sum;
            }

            extract(&connectEdge, &edge);

            if(!isAddMst[edge.to]) {
                break;
            }
        }

        v = edge.to;
        sum += edge.weight;
    }

    return sum;
}

int main() {

    scanf("%d", &n);

    weight = new int[n * n];
    
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            int w;
            scanf("%d", &w);
            weight[i * n + j] = (w == -1) ? INT_MAX : w;
        }
    }

    cout << getMimimumSpanningTree() << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int,int> pii;
#define fi first
#define se second
#define mp make_pair

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

//const long long INF = 1LL << 60;
const int INF = 1001001001;
const int MAX = 110;

//struct edge{
//  int to, cost;
//  edge(){}
//  edge(int to, int cost): to(to), cost(cost){}
//};

int G[MAX][MAX];
bool seen[MAX];
priority_queue<pii, vector<pii>, greater<pii> > pq;
int n;

int prim(){
  seen[0] = true;
  rep(i, n){
    if(G[0][i] == -1) continue;
    pq.push(mp(G[0][i], i));
  }
  
  int sum = 0;
  while(!pq.empty()){
    pii p = pq.top();
    pq.pop();
    if(seen[p.se]) continue;
    seen[p.se] = true;
    sum += p.fi;
    rep(i, n){
      if(G[p.se][i] == -1) continue;
      pq.push(mp(G[p.se][i], i));
    }
  }

  return sum;
}

int main(){
  cin >> n;
  rep(i, n) rep(j, n) cin >> G[i][j];

  cout << prim() << endl;
 
  return 0;
}