Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

C++: /**
 * @brief Prim?????¢?????´????????????????????????????????????
 * @note  ??¢??£URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
 */



//****************************************
// ???????????????????????????????????????????????????
//****************************************

#include <functional>
#include <utility>
#include <algorithm>
#include <vector>
#include <cassert>
#include <tuple>
#include <queue>
#include <iostream>



//****************************************
// ???????????§????????¢?????????????????????
//****************************************

#define GRAPH_BEGIN namespace graph {
#define GRAPH_END   }



//****************************************
// ????????????????§????
//****************************************

GRAPH_BEGIN



//****************************************
// ?§????????????????
//****************************************

using weight_t   = std::int32_t;  /**< ???(u, v)????????????w?????¨?????? */
using index_t    = std::int32_t;  /**< ??????v??????????????¨??????       */
using capacity_t = weight_t;      /**< ???(u, v)??????????????¨??????    */



namespace limits {
    enum {  // scoped??§????????????????????????????????????enum(strongly-typed enum)??§?????????
        inf = std::numeric_limits<weight_t>::max() / 3,  /**< @brief ???????????¨???????????´???????????¨????????????     */
        nil = std::numeric_limits<index_t>::min() / 3,   /**< @brief ?????????????????¨???????????´???????????¨???????????? */
    };
}



/**
 * @brief  ????????????????§???????(scoped enum)
 * @detail (u, v) ??? E??§??????u??????????????°??????v?????°????????§??????
 *         ?????????????????????????????£??\????????¨?????????????????????????????§??????
 *         ??°???????????????????????£??\???????????¨???????????????????????????????????¢??????????????¨???????????????????¢?????????????
 */
enum struct vcolor : std::int32_t {
    white,  /**< ??????????????? */
    black,  /**< ??¢???????????? */
    gray,   /**< ??¢???????????? */
};

/**
 * @brief ??°????????¨?????????(??????) 
 */
struct vertex {
    union {
        weight_t d;       /**< ?§????s??????????????¢  */
        weight_t key;     /**< Prim?????¢?????´???????????????????????¨????±?????????????????????¨????????¶?????? */
    };
    index_t pi;           /**< ????????????(?????????) */
    union {
        vcolor color;     /**< ????????????        */
        bool  visited;    /**< ????????????????     */
    };
    // weight_t f;           /**< ??????????????°(DFS?????????????????????????????????????????¨??????????????????)     */
    vertex() : d(0), pi(0), color(vcolor::white)/*, f(0)*/ {}
};

/**
 * @brief ??°????????¨??¨??????(???)
 * @note  G = (V, E)???????????¢??°w???????????????????????°????????¨????????¨???
 *        ???(u, v) ??? E????????????w(u, v)??¨??¨?????????
 */
struct edge {
    index_t  src;   /**< ???????§????u */
    index_t  dst;   /**< ????????????v */
    union {
        weight_t w;     /**< ???(u, v)????????????(?????????) */
        capacity_t c;   /**< ???(u, v)????????? */
    };
    edge() = default;
    edge(index_t src, index_t dst) : src(src), dst(dst), w(1) {}
    edge(index_t src, index_t dst, weight_t w) : src(src), dst(dst), w(w) {}
};



using edges_t    = std::vector<edge>;      /**< ??°??????G=(V, E)????????????E   */
using vertices_t = std::vector<vertex>;    /**< ??°??????G=(V, E)???????????????V */
using array_t    = std::vector<weight_t>;  /**< ??????w?????????  */
using indices_t  = std::vector<index_t>;   /**< ????????????????????? */
using matrix_t   = std::vector<array_t>;   /**< ??°??????G?????£??\????????¨???(???????????¨????????¨???) */
using graph_t    = std::vector<edges_t>;   /**< ??°??????G?????£??\???????????¨???(???????????????????????¨??????) */



//****************************************
// ??¢??°?????????
//****************************************

/**< @brief ????????????????????\??????min????????????????????\????????????????????? < ??????????????? */
static bool operator < (const edge& e, const edge& f) { return e.w > f.w; }



/**
 * @brief  Prim?????¢?????´?????????
 *
 * @note   Prim?????¢?????´?????????????????°??????????????????????±???????Dijkstra?????¢?????´???????????¨?????¨?????????????????????????????????
 *         Prim?????¢?????´??????????????????A???????????????1????????¨?????¢???????????¨????????§????????????. ????????¨???????????????r??????????§???????V???????????¨?????????????????§????????????
 *         ?????????????????§??????A???????????¨???????????????(A????????¨??\?¶??????????????????????)?????£???????????????????????¨A????????????
 *         A?????????????????¨???????????????????????????????????£???A?????????????????????????????¢?????´??????????????????????????¨??????A?????????????°???¨?????¨?????¢?????????
 *         ?????????????????§?????¨?????????????¢????????????????????°????????????????????????¨????????¨???????????????????????§????????????????¬??????\??§??????
 *
 * @note   ????????????????????\?????????????????´??°????????????????????????????????????????????\???????????????????????§????????????????????\????????°?????(E)??§????????????
 *         EXTRACT-MIN????????????????????????????????§????????°????????????????????¨??§?????¨?????¨??????????????????????????(ElgV)??¨????????????
 * 
 * @param  const graph_t& G ??°??????G
 * @param  index_t        r ????°???¨?????¨??????
 */
std::pair<edges_t, weight_t> prim(const graph_t& G, index_t r)
{
    std::int32_t n = G.size();
    std::size_t m = 0;
    for (auto&& es : G) { m += es.size(); }
    std::vector<std::int32_t> visited(n);
    edges_t A; weight_t w;

    
    for (index_t u = 0; u < n; u++) {
        visited[u] = false;  // ??????????????????????????????
    }
    std::priority_queue<edge> Q;
    Q.push(edge(limits::nil, r, w = 0));    // ???r????????????0????¨??????????
    while (!Q.empty()) {
        edge e = Q.top(); Q.pop();          // ????????????????????????
        index_t u = e.dst;
        if (visited[u]) { continue; }       // ??????????????????????????¨????????§???????????´???????????????????????????????????¶???????????????
        
        for (auto&& f : G[u]) {             // u??¨??£??\????????¨????±???????????????????v?????´??°?????????
            if (!visited[f.dst/*??????v*/]) { Q.push(f); }
        }
        visited[u] = true;                  // ??????u????????????????????????
        w += e.w;                           // ????°????????????´??°??????
        if (e.src != limits::nil) {  // ??¢?????´??????????????????????????¨??????min????????????????????\???????????§?????????
            A.emplace_back(e);       // G????????????????°???¨?????¨A???A = { (v, v.??) : v ??? V - { r } }??§??????  
        }
    }
    return std::make_pair(A, w);
}


//****************************************
// ?????????????????????
//****************************************

GRAPH_END



//****************************************
// ??¨?????????????????????
//****************************************

int main(void)
{
    using namespace std;
    int n;   cin >> n;
    graph::graph_t G; G.resize(n);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int k;
            cin >> k;
            if (k != -1) {
                G[i].emplace_back(i, j, k);
            }
        }
    }

    auto mst = prim(G, 0);
    cout << mst.second << endl;
    
    return 0;
}