Go: package main

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

const (
	MAX_N      = 200
	MAX_WEIGHT = 2000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

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

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

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		adjacentMatrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			v, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjacentMatrix[i][j] = v
		}
	}

	mst([]int{1}, shortestList, adjacentMatrix)
	fmt.Println(shortestList)
	fmt.Println(calculateMinWeight(shortestList))
}

func calculateMinWeight(shortestList []ShortestInfo) int {
	visitedList := make([]bool, len(shortestList))
	visitedList[0] = true
	sumWeight := 0
	for !visitedAll(visitedList) {
		shortestInfo := shortestList[0]
		shortestList = shortestList[1:]
		edges := shortestInfo.edges
		for _, edge := range edges {
			if !visitedList[edge.end-1] {
				visitedList[edge.end-1] = true
				sumWeight += edge.weight
			}
		}
	}
	return sumWeight
}

func visitedAll(visitedList []bool) bool {
	for _, v := range visitedList {
		if !v {
			return false
		}
	}
	return true
}

func mst(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			s := getShortest(adj.end, shortestList)
			if currentShortest.shortest+adj.weight < s.shortest || (currentShortest.shortest+adj.weight == s.shortest && len(currentShortest.edges)+1 > len(s.edges)) {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node-1]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node-1] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode-1]
	edges := []Edge{}
	for i, v := range adjacents {
		if v > 0 {
			edges = append(edges, Edge{start: currentNode, end: i + 1, weight: v})
		}
	}
	return edges
}

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

C++: /**
 * @brief Prim?????¢?????´????????????????????????????????????
 * @note  ??¢??£URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
 * @date  2016/04/17~2017/01/03
 */



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

#include <utility>
#include <algorithm>
#include <vector>
#include <tuple>
#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)??????????????¨??????    */
using bool_t     = std::int32_t;  /**< ?????????????????´??°?????§???{ false 0 true ????????\??? }??¨?????? */



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

/**
 * @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_t  visited;  /**< ????????????????     */
    };
    // weight_t f;           /**< ??????????????°(DFS?????????????????????????????????????????¨??????????????????)     */
    vertex() noexcept : 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) noexcept             : src(src), dst(dst), w(1) {}
    edge(index_t src, index_t dst, weight_t w) noexcept : src(src), dst(dst), w(w) {}
};



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 ?????????????????¨???????????´???????????¨???????????? */
    };
}



//********************************************************************************
// ?????????????????????2
//********************************************************************************

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 stamps_t   = std::vector<bool_t>;    /**< ???????????¢???????????? */
using matrix_t   = std::vector<array_t>;   /**< ??°??????G?????£??\????????¨???(???????????¨????????¨???) */
using graph_t    = std::vector<edges_t>;   /**< ??°??????G?????£??\???????????¨???(???????????????????????¨??????) */



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

/**
 * @brief  Prim?????¢?????´???????????????????????????????°???¨?????¨A????±?????????????????????????(A????????¨??\?¶??????????????????????)?????£???????????????????????¢??????
 *         Dijkstra?????¢?????´????????????????????????????§????s?????????????°???????????????¨?????????????????????u ??? V - S???????????????(??????S????±??????????"????????????"??????????±???????)
 *
 * @note   ????????¢??°???????????????????????????Heap???????????????Prim?????????Dijkstra?????¢?????´???????????§?????????
 *         ????????§??????Heap?????¨???????????§min????????????????????\????????????????????????
 *         ?????????????????¢?????´?????????????¨???°??????????????????????????????????????????????????????????????°?????????????????¨????????????
 *         ?????£??????v.visited???false?????¨??????v.color?????????????????°?????§?????????true?????¨??????????????§??????
 *
 * @param  const vertices_t& V ??¢?´¢????±????????????????V
 * @param  index_t n           ????????????V???????????°n
 * @return index_t u           ?????¶??????????????????u(?????¶??????????????????????????¨???????????´??????nil?????????)
 */
static index_t extract_min(const vertices_t& V, index_t n)
{
    index_t  u = limits::nil;
    weight_t d = limits::inf; 
    for (index_t v = 0; v < n; ++v) {
        if (!V[v].visited && V[v].d < d) { d = V[v].d; u = v; }
    }
    return u;
}


/**
 * @brief  Prim?????¢?????´?????????
 *
 * @note   Prim?????¢?????´?????????????????°??????????????????????±???????Dijkstra?????¢?????´???????????¨?????¨?????????????????????????????????
 *         Prim?????¢?????´??????????????????A???????????????1????????¨?????¢???????????¨????????§????????????. ????????¨???????????????r??????????§???????V???????????¨?????????????????§????????????
 *         ?????????????????§??????A???????????¨???????????????(A????????¨??\?¶??????????????????????)?????£???????????????????????¨A????????????
 *         A?????????????????¨???????????????????????????????????£???A?????????????????????????????¢?????´??????????????????????????¨??????A?????????????°???¨?????¨?????¢?????????
 *         ?????????????????§?????¨?????????????¢????????????????????°????????????????????????¨????????¨???????????????????????§????????????????¬??????\??§??????
 *
 * @note   ??????v????±???§v.key???v??¨??¨????±?????????????????????¨????????¶??????????°?????????§??????. ??????????????????????????????????????¨??????????????°???v.key = ?????¨????????????
 *         ?±???§v.???????¨????????????v???????????????
 *
 * @note   ??°??????G = (V, E)?????£??\??????????????£????????????????????¨????????(V^2)??§??°???Prim?????¢?????´??????????????\???????????????????????§??????
 * 
 * @param  const matrix_t& W ??£??\??????W
 * @param  index_t         r ????°???¨?????¨??????
 */
std::pair<vertices_t, weight_t> prim(const matrix_t& W, index_t r)
{
    index_t n = W.size();
    vertices_t A(n); weight_t w;

    
    for (auto&& u : A) {
        u.key = limits::inf;  // ????????????????????????????¨??????????
        u.pi  = limits::nil;  // ??????????????????NIL????¨??????????
        u.visited = false;
    }
    A[r].key = w = 0;         // ???r???????????§????????????????????????????????????????????????0????¨??????????
    while (true) {
        // ????°???¨?????¨A????±?????????????????????????(A????????¨??\?¶??????????????????????)?????£???????????????????????¢???
        index_t u = extract_min(A, n);
        if (u == limits::nil) { break; }   // ??????u???NIL?????????????????°?????¢?´¢???????????§?????????????°???¨?????¨A??? A = {(v, v.??) : v ??? V - { r } }??§??????
        for (index_t v = 0; v < n; ++v) {  // ??????u?????£??\???????????°????????????
            if (W[u][v] < A[v].key) {      // ?????????????????°?????????????????????????±???§?????´??°?¢????????????????????????????
                A[v].pi  = u;              // v??????±???§??¨
                A[v].key = W[u][v];        // key?±???§?????´??°??????
            }
        }
        A[u].visited = true;  // ???(u, u.??)???????°???¨?????¨A????????????(???????????????r?????????)
        w += A[u].key;        // ????°????????????´??°
    }
    return std::make_pair(A, w);
}



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

GRAPH_END



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


int main(void)
{
    using namespace graph;
    using namespace std;
    matrix_t M;
    int n, k;
    
    cin >> n; M.resize(n);
    for (int j = 0; j < n; j++) {
        M[j].resize(n);
        for (int i = 0; i < n; i++) {
            cin >> k;
            M[j][i] = k == -1 ? limits::inf : k;
        }
    }

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