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++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define USE_LLONG_AS_INT
#ifdef USE_LLONG_AS_INT
#define int long long
#define inf (1ll<<60)
#else
#define inf (1<<30)
#endif
#define rep(i,n) for(int i=0;i<(n);i++)
#define Rep(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a,b) for(int i=(a);i<=(b);i++)
#define rev(i,n) for(int i=(n)-1;i>=0;i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define eb emplace_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod=1e9+7;
const string sp=" ";

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

int n;
vvi g;

int prim(){
    vi d(n,inf),p(n,-1);
    d[0]=0;
    vector<bool> b(n);
    while(true){
        int u;
        int m=inf;
        rep(i,n){
            if(m>d[i]&&!b[i]){
                u=i;
                m=d[i];
            }
        }
        if(m==inf)break;
        b[u]=true;
        rep(v,n){
            if(!b[v]&&g[u][v]!=inf){
                if(d[v]>g[u][v]){
                    d[v]=g[u][v];
                    p[v]=u;
                }
            }
        }
    }
    int s=0;
    rep(i,n){
        if(p[i]!=-1)s+=g[i][p[i]];
    }
    return s;
}

void run(){
    cin>>n;
    g=vvi(n,vi(n));
    rep(i,n){
        rep(j,n){
            int c;
            cin>>c;
            g[i][j]=c==-1?inf:c;
        }
    }
    cout<<prim()<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll MinimumSpanningTree(const V<V<pair<ll, int>>> &g, int n) { // 最小全域木
    using pi = pair<ll,int>; // pair = (cost, to);
    ll res = 0;

    vector<int> v;
    v.push_back(0);
    priority_queue<pi, vector<pi>, greater<pi>> pq; 
    vector<bool> seen(n);
    seen[0] = true;
    for(pi nv : g[0]) {
        pq.push(nv);
    }

    while(sz(v) != n) {
        pi p = pq.top(); pq.pop();
        if(seen[p.se]) continue;
        v.push_back(p.se);
        seen[p.se] = true;
        res += p.fi;
        for(pi nv : g[p.se]) {
            if(!seen[nv.se])
                pq.push(nv);
        }
    }
    return res;
}

int main(){
    int n;
    cin >> n;
    V<V<pair<ll,int>>> g(n);
    rep(i,n)rep(j,n) {
        ll a;
        cin >> a;
        if(a == -1) continue;
        g[i].emplace_back(a,j);
    }
    ll ans = MinimumSpanningTree(g,n);
    cout << ans << endl;
}

C++: #include <cstdio>
#include <climits>
#include <vector>

int w[100][100];
std::vector<int> mst;
std::vector<int> others;
typedef std::vector<int>::iterator itr;

int minimum(){
    if(others.size()<=0)return 0;
    int candiW=INT_MAX;
    itr candiI;
    for(itr i=mst.begin();i!=mst.end();i++){
        for(itr j=others.begin();j!=others.end();j++){
            if(w[*i][*j]<candiW){
                candiW = w[*i][*j];
                candiI = j;
            }
        }
    }
    mst.push_back(*candiI);
    printf("%d ",*candiI);
    others.erase(candiI);
    return candiW + minimum();
}

int main(){
    int i, j, n;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            scanf("%d",&w[i][j]);
            if(w[i][j]==-1)w[i][j]=INT_MAX;
        }
    }
    mst.push_back(0);
    for(i=1;i<n;i++)others.push_back(i);
    printf("%d\n",minimum());
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << '\n'
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
constexpr int INF = 1<<21;
enum Color { WHITE = 0, GRAY, BLACK };
int n;
vv(int, M, MAX, MAX, INF);
inline int prim() {
	vi color(n, WHITE);
	int d[n];
	d[0] = 0;
	rep(i, 1, n) d[i] = INF;
    int minv, u;
    while(true) {
        minv = INF;
        u = -1;
        rep(i, 0, n) {
            if(minv > d[i] && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }
        if(u == -1) break;
        color[u] = BLACK;
        rep(v, 0, n) {
            if(color[v] != BLACK && d[v] > M[u][v]) {
                d[v] = M[u][v];
                color[v] = GRAY;
            }
        }
    }
    return accumulate(all(d), 0);
}
int main() {
    cin >> n;
    int e;
    rep(i, 0, n) {
        rep(j, 0, n) {
            cin >> e;
            if(e != -1) M[i][j] = e;
        }
    }
    P(prim());
    return 0;
}