Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

C++: #ifdef LOCAL
#pragma GCC optimize ("O0")
#else
#pragma GCC optimize ("O3")
#endif


#include <bits/stdc++.h>


using namespace std;

using ll = long long;
const int INF = 1e9 + 7;
const int MOD = INF;
const ll INF_LL = 1e18 + 7;

#define __overload3(_1, _2, _3, name,...) name
#define rep(...) __overload3(__VA_ARGS__, repFromUntil, repUntil, repeat)(__VA_ARGS__)
#define repeat(times) repFromUntil(__name, 0, times)
#define repUntil(name, times) repFromUntil(name, 0, times)
#define repFromUntil(name, from, until) for (int name = from, name##__until = (until); name < name##__until; name++)
#define repFromTo(name, from, to) repFromUntil(name, from, to + 1)
#define repr(...) __overload3(__VA_ARGS__, reprFromUntil, reprUntil, repeat)(__VA_ARGS__)
#define reprUntil(name, times) reprFromUntil(name, 0, times)
#define reprFromUntil(name, from, until) for (int name = until - 1, name##__from = (from); name >= name##__from; name--)
#define reprFromTo(name, from, to) reprFromUntil(name, from, to + 1)

#define debugos OUT
#ifdef LOCAL
#define debug(v) {printf("L%d %s > ",__LINE__,#v);debugos<<(v)<<newl;}
#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:(v)){debugos<<e<<" ";}debugos<<newl;}
#define debuga(m,w) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(w);x++){debugos<<(m)[x]<<" ";}debugos<<newl;}
#define debugaa(m,h,w) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){debugos<<(m)[y][x]<<" ";}debugos<<newl;}}
#else
#define debug(v) {}
#define debugv(v) {}
#define debuga(m,w) {}
#define debugaa(m,h,w) {}
#endif

#define newl "\n"

#define all(iter) begin(iter), end(iter)


template <class T> bool chmin(T& var, T x) {
  if (var > x) {
    var = x;
    return true;
  } else return false;
}
template <class T> bool chmax(T& var, T x) {
  if (var < x) {
    var = x;
    return true;
  } else return false;
}

class MyScanner {
  public:
    template<typename T> void input_integer(T& var) {
        var = 0; T sign = 1;
        int cc = getchar();
        for (; cc<'0' || '9'<cc; cc = getchar())
            if (cc == '-') sign = -1;
        for (; '0' <= cc && cc <= '9'; cc = getchar())
            var = (var << 3) + (var << 1) + cc - '0';
        var = var * sign;
    }
    int c() { char c; while (c = getchar(), c == ' ' or c == '\n'); return c; }
    MyScanner& operator>>(char& var) { var = c(); return *this; }
    MyScanner& operator>>(int& var) { input_integer<int>(var); return *this; }
    MyScanner& operator>>(ll& var) { input_integer<ll>(var); return *this; }
    MyScanner& operator>>(string& var) {
        int cc = getchar();
        for (; !isvisiblechar(cc); cc = getchar());
        for (; isvisiblechar(cc); cc = getchar())
            var.push_back(cc);
        return *this;
    }
    operator int() {
      int n;
      *this >> n;
      return n;
    }
    operator ll() {
      ll n;
      *this >> n;
      return n;
    }
    private:
      int isvisiblechar(int c) {
        return 0x21 <= c && c <= 0x7E;
      }
};
class MyPrinter {
  public:
    template<typename T>
    void output_integer(T var) {
        if (var == 0) { putchar('0'); return; }
        if (var < 0)
            putchar('-'),
            var = -var;
        char stack[32]; int stack_p = 0;
        while (var)
            stack[stack_p++] = '0' + (var % 10),
            var /= 10;
        while (stack_p)
            putchar(stack[--stack_p]);
    }
    MyPrinter& operator<<(char c) { putchar(c); return *this; }
    template <typename T>
    MyPrinter& operator<<(T var) { output_integer<T>(var); return *this; }
    MyPrinter& operator<<(char* str_p) { while (*str_p) putchar(*(str_p++)); return *this; }
    MyPrinter& operator<<(const char* str_p) { while (*str_p) putchar(*(str_p++)); return *this; }
    MyPrinter& operator<<(const string& str) {
        const char* p = str.c_str();
        const char* l = p + str.size();
        while (p < l) putchar(*p++);
        return *this;
    }
    // MyPrinter& operator<<(const modint& var) { output_integer<ll>(var.value); return *this; }
    template <typename T>
    void operator()(T x) {
        *this << x << newl;
    }
};
MyScanner IN;
MyPrinter OUT;

class union_find {
  public:
    /// <param name="len">要素数</param>
    union_find(int len) {
      parents = new int[len];
      depths = new int[len];
      for (int i = 0; i < len; i++) parents[i] = i;
      memset(depths, 0, len);
    }
    /// 根を特定　あまり外部から使わない
    int root(int x) {
      return parents[x] == x ? x :
             root(parents[x]);
    }
    /// 2つの要素が同じ木に属すると宣言
    void unite(int x, int y) {
      x = root(x);
      y = root(y);
      if (x == y) return;
      if (depths[x] < depths[y]) parents[x] = y;
      else {
        parents[y] = x;
        if (depths[x] == depths[y]) depths[x]++;
      }
    }
    /// 同じ木に属するか
    bool same(int x, int y) {
      return root(x) == root(y);
    }
  private:
    int *parents;
    int *depths;
};

/// 最小全域木 O(|E|log|V|)
/// <param name="g">隣接行列のグラフ　辺がなければある程度大きな値</param>
/// <returns>重みの総和の最小値</returns>
int prim(vector<vector<int>>& g) {
  int V = g.size();
  int *mincost = new int[V];
  bool *done = new bool[V];
  fill(mincost, mincost + V, INF);
  fill(done, done + V, false);
  mincost[0] = 0;
  int res = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++)
      if (!done[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
    if (v == -1) break;
    done[v] = true;
    res += mincost[v];
    for (int u = 0; u < V; u++)
      mincost[u] = min(mincost[u], g[v][u]);
  }
  return res;
}

int main() {
  int n = IN;
  auto g = vector<vector<int>>(n, vector<int>(n));
  rep(i, n) rep(j, n) {
    int t = IN;
    g[i][j] = t == -1 ? INF : t;
  }
  OUT(prim(g));
}