Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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