Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <unordered_map> //???????????\??¢??°
#include <fstream> //ifstream, ofstream

//#define NDEBUG //NDEBUG???#include <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert


using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???                           ????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif

//?¨??????\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif


#define disp(A) dout << #A << " = " << setw(3) << (A) << endl
#define disP(A) dout << setw(3) << (A) << " " // << setw(3) ??????????????\????????????
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;


const int INF = 2e9-1;
const ll INFLL = 1e18-999;

#define N_MAX 100 //num of vertex
//#define M_MAX 10000 //mum of edge

int N;
ll matrix[N_MAX][N_MAX];

ll minCostTo[N_MAX]; //?????¨?§??????????MST????????????i??????????°???????????????¨???????????????Daijkstra?????§??´??°????????????
vi vertices; //??????MST????????????????????????????????¨???






int main() {
    
    //read input data and initialize
    din >> N;
    int cost;
    rep(i,0,N) {
        vertices.push_back(i); //??¨????????????????????????
        minCostTo[i] = INFLL; //??¨????????????????????????????????§??§?????????
        
        rep(j,0,N) {
            din >> cost;
            matrix[i][j] = (cost==-1 ? INF : cost); //??????????????¨??????????????????INF??¨??????
        }
    }
    
//    //test display
//    rep(i,0,N) {
//        rep(j,0,N) {
//            dout << matrix[i][j] << " ";
//        }
//        dout << endl;
//    }
//    dout << endl;

    
    
    
    //????????????
    ll ans = 0;
    int x; //????????????????????????MST?????????????????????
    minCostTo[0] = 0; //x=0???????§????????????????0??§?????????
    while( !vertices.empty() ) {
//        dout << "---------------------\n";
        
        //?????????????????????????????????????????°????????????????°??????????x?????¢?´¢
        x = vertices.front();
        vi::iterator itr_to_x = vertices.begin();
        for( vi::iterator itr=vertices.begin(); itr!=vertices.end(); itr++) {
            if( minCostTo[x] > minCostTo[*itr] ) {
                x = *itr;
                itr_to_x = itr;
            }
        }
        
//        dout << "add vertex " << x << endl;
        
        //MST?????????x??????????????????????????????????????????????????????x????????????????????§???????°?????????´??°??§?????????????????§??????
        vertices.erase(itr_to_x);
        ans += minCostTo[x];
        minCostTo[x] = 0; //MST????????????????????????????????????0?????????
        
        for( vi::iterator itr=vertices.begin(); itr!=vertices.end(); itr++) {
            if( minCostTo[*itr] > minCostTo[x] + matrix[x][*itr] ) {
                minCostTo[*itr] = minCostTo[x] + matrix[x][*itr];
            }
        }
        
//        //test display
//        dispAll(vertices, vertices.size());
//        
//        rep(i,0,N) {
//            disP(i); disp(minCostTo[i]);
//        }
//        
//        disp(ans);
    }
    
    
    
//    output
    dout << ans << endl;
    
    
    
#ifdef INPUTFILE
    inputfile.close();
#endif
    
#ifdef OUTPUTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}