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++: /**
 * @brief ????°???¨?????¨??????(minimum-spanning tree problem)????????????
 *        Kruskal?????¢?????´?????????(Kruskal's algorithms)???????£??????????
 * @note  ??¢??£URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
 * @date  2016/04/17 ~ 2016/06/04
 */



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

#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>




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

#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?????£??\???????????¨???(???????????????????????¨??????) */



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

GRAPH_END



/**< @brief ?´????????£? */
struct disjoint_set {
    using index_t = std::int32_t;
    using rank_t  = std::int32_t;
    
    std::vector<index_t> p;
    std::vector<rank_t> rank;

    explicit disjoint_set(std::size_t size) : p(size, -1), rank(size, -1) {}
    
    /**
     * @brief x????????????????´?(?????£????????£??¨???)??¨??????????????°???????????????????????????.
     * @note  ???????????????????´???§???????????????x????????§?????\???????????????????????????????????¨?????????. 
     *
     * @param int x ??????x
     */
    void make_set(index_t x) {
        p[x]    = x;
        rank[x] = 0;
    }

    /**
     * @brief     x?????????????????????Sx??¨y?????????????????????Sy?????????(??±???)??????
     *            ???????????????????????§????????°???????????????????????????.
     * @attention Sx??¨Sy??????????????????????????????????????????????´???§????????¨????????????.
     *
     * @param int x   ????????????Sx??????x
     * @param int y   ????????????Sy??????y
     */
    void merge(index_t x, index_t y) {
        link(find_set(x), find_set(y));
    }


    /**
     * @brief ????¶????merge????£??????¢??°
     *
     * @param int x ????????????Sx?????£??¨???x(Sx??????)
     * @param int y ????????????Sy?????£??¨???y(Sy??????)
     */
    void link(index_t x, index_t y) {
        // x???????????? > y??????????????????????????????
        if (rank[x] > rank[y]) {  // x.rank > y.rank????????°???
            p[y] = x;             // y?????????x?????????
        }
        else {                    // x.rank <= y.rank????????°??? 
            p[x] = y;             // x?????????y?????????
            
            if (rank[x] == rank[y]) {   // x.rank == y.rank????????°???
                rank[y] = rank[y] + 1;  // y.rank????????¨????¢???????
            }
        }
    }

    /**
     * @brief  x?????????(?????????)???????????£??¨??????????????????????????????.
     *         ????¶????findset???2?????????????????\???.?????°??????????????????????????£??????
     *         find???????????????????????£?????????????????°???????????????????????????????????????
     *         ?????°?????????????????????????????????????????£?????????find???????????????2???????????°???????????£??????
     *         find?????????????????????????????????????????´??\???????????????????????´??°??????.
     *
     * @param  int x   ??????Sx??????x
     * @return int y   ??????Sx?????£??¨???(???)
     */
    index_t find_set(index_t x) {
        // find???????????????????????£?????????????????°???
        if (x != p[x]) {
            p[x] = find_set(p[x]);  // ?????°??????????????????
        }
        return p[x];  // ?????°???????????????????????????????????????
    }
};



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

GRAPH_BEGIN



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

/**
 * @brief  Kruskal?????¢?????´?????????
 *
 * @note   Kruskal?????¢?????´???????????§????????????A????????????????????°??????????????????????????????????????¨???????£???§??????
 *         A?????????????????¨??????????????????2????????°????????£??????????????£???????????°?????????????°????????????§??????
 *
 * @note   Kruskal?????¢?????´???????????§??????????????????????£????????????????????????¨?????????????£?????±???????2????????¨?????£????????????????????????????????§????????????????°???????(u, v)??§??????
 *         (u, v)?????£?????????2????????¨???C1?????????C2??¨??????. (u, v)???C1?????\?????¨??¨??£??????????????????????????????(u, v)??????????£??????????????????¨????????§??????
 *         ?????????????????§?????????????????????????°??????????????£????????????????????????????Kruskal?????¢?????´????????????????¬???¢?????´???????????§??????         
 *
 * @note   Kruskal?????¢?????´????????????????????????????????(ElgV)??§??????
 *
 * @param  const graph& G ??°??????G
 * @return ?????????A??¨????????????(????°???¨?????¨?????????)
 */
std::pair<edges_t, weight_t> kruskal(const graph_t& G)
{
    index_t n = G.size();
    disjoint_set ds(static_cast<std::size_t>(n));  // ??????????´???????????????????????????????????§?????????????
    struct cmp { bool operator()(const edge& e, const edge& f) { return e.w < f.w; } };
    edges_t E;  // ??°??????G????????????G.E???????????????
    for (auto&& es : G) { for (auto&& e : es) { E.push_back(e); } }


    
    weight_t w = 0; edges_t A;             // A??????????????????????????????
    for (index_t v = 0; v < n; ++v) {      // ????????????????????????1????????¨??§??????|V|??¬?????¨???????????????
        ds.make_set(v);
    }
    std::sort(E.begin(), E.end(), cmp());  // ??????w??????????°??????§G.E????????????????????????
    for (auto&& e : E) { // ????????????????°?????????????????????????????¨???????
        // ?????????????????§????????????(u, v)?????????????????????u??¨v???????????¨????±???????????????????????????????
        if (ds.find_set(e.src) != ds.find_set(e.dst)) {  // ????????????????????¨????±??????????????????°???
            A.push_back(e); w += e.w;                    // ???(u, v)???A????????????
            ds.merge(e.src, e.dst);                      // 2????????¨?????????????????????????????????
        }
    }
    return std::make_pair(A, w);
}



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

GRAPH_END



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

int main(void)
{
    using namespace std;
    using namespace graph;
    int n;   cin >> n;
    graph_t G(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 = kruskal(G);
    cout << mst.second << endl;
    
    return 0;
}