#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
//#include "libs.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
//#include <opencv2/core.hpp>
//#include <opencv2/highgui.hpp>
//#include <opencv2/imgproc.hpp>

using namespace std;

//呪文
#define DUMPOUT cerr
#define dump(...) DUMPOUT<<"  ";DUMPOUT<<#__VA_ARGS__<<" :["<<__LINE__<<":"<<__FUNCTION__<<"]"<<endl;DUMPOUT<<"    ";dump_func(__VA_ARGS__)

typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef pair<string, string> pss;
template <typename _KTy, typename _Ty> ostream& operator << (ostream& o, const pair<_KTy, _Ty>& m) { o << "{" << m.first << ", " << m.second << "}"; return o; }
template <typename _KTy, typename _Ty> ostream& operator << (ostream& o, const map<_KTy, _Ty>& m) { if (m.empty()) { o << "{ }"; return o; } o << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { o << ", " << *itr; } o << "}"; return o; }
template <typename _KTy, typename _Ty> ostream& operator << (ostream& o, const unordered_map<_KTy, _Ty>& m) { if (m.empty()) { o << "{ }"; return o; } o << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { o << ", " << *itr; } o << "}"; return o; }
template <typename _Ty> ostream& operator << (ostream& o, const vector<_Ty>& v) { if (v.empty()) { o << "{ }"; return o; } o << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { o << ", " << *itr; } o << "}"; return o; }
template <typename _Ty> ostream& operator << (ostream& o, const set<_Ty>& s) { if (s.empty()) { o << "{ }"; return o; } o << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { o << ", " << *itr; } o << "}"; return o; }
template <typename _Ty> ostream& operator << (ostream& o, const unordered_set<_Ty>& s) { if (s.empty()) { o << "{ }"; return o; } o << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { o << ", " << *itr; }	o << "}"; return o; }
template <typename _Ty> ostream& operator << (ostream& o, const stack<_Ty>& s) { if (s.empty()) { o << "{ }"; return o; } stack<_Ty> t(s); o << "{" << t.top(); t.pop(); while (!t.empty()) { o << ", " << t.top(); t.pop(); } o << "}";	return o; }
template <typename _Ty> ostream& operator << (ostream& o, const list<_Ty>& l) { if (l.empty()) { o << "{ }"; return o; } o << "{" << l.front(); for (auto itr = ++l.begin(); itr != l.end(); ++itr) { o << ", " << *itr; } o << "}"; return o; }
template <typename _KTy, typename _Ty> istream& operator >> (istream& is, pair<_KTy, _Ty>& m) { is >> m.first >> m.second; return is; }
template <typename _Ty> istream& operator >> (istream& is, vector<_Ty>& v) { for (size_t i = 0; i < v.size(); i++) is >> v[i]; return is; }
namespace aux { // print tuple
	template<typename Ty, unsigned N, unsigned L> struct tp { static void print(ostream& os, const Ty& v) { os << get<N>(v) << ", "; tp<Ty, N + 1, L>::print(os, v); } };
	template<typename Ty, unsigned N> struct tp<Ty, N, N> { static void print(ostream& os, const Ty& v) { os << get<N>(v); } };
}
template<typename... Tys> ostream& operator<<(ostream& os, const tuple<Tys...>& t) { os << "{"; aux::tp<tuple<Tys...>, 0, sizeof...(Tys)-1>::print(os, t); os << "}"; return os; }

template<typename A, size_t N, typename T> inline void Fill(A(&array)[N], const T &val) { std::fill((T*)array, (T*)(array + N), val); }

void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...); }

#define PI 3.14159265358979323846
#define EPS 1e-10
#define FOR(is,a,n) for(int is=(a);is<(n);++is)
#define REP(is,n)  FOR(is,0,n)
#define all(j) (j).begin(), (j).end()
#define SZ(j) ((int)(j).size())
#define fake false



// pll : (cost, dst)
ll MST(const vector<vector<pll>>& G) {
	const ll INF = 1LL << 60;
	int V = G.size();
	vector<ll> mincost(V, INF);
	vector<ll> used(V, false);

	priority_queue<pll, vector<pll>, greater<pll>> pq;
	mincost[0] = 0;
	pq.push(pll(0, 0));
	ll MSTcost = 0;

	while (!pq.empty()) {
		pll p = pq.top(); pq.pop();
		int v = p.second;
		if (used[v])
			continue;
		used[v] = true;
		MSTcost += mincost[v];
		for (int i = 0; i < G[v].size(); i++) {
			pll e = G[v][i];
			if (mincost[e.second] > e.first) {
				mincost[e.second] = e.first;
				pq.push(e);
			}
		}
	}

	return MSTcost;
}



class RandomSet {
	struct Xor128 {
		unsigned x, y, z, w;
		Xor128(int _w) { x = 123456789; y = 362436069; z = 521288629; w = _w; }
		unsigned nextUInt() {
			unsigned t = (x ^ (x << 11));
			x = y; y = z; z = w;
			return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
		}
	} rnd;
	unordered_set<int> st;
	vector<unordered_set<int>::iterator> its;
public:
	RandomSet(unsigned seed = 88675123) : rnd(seed) {}
	auto begin() { return st.begin(); }
	auto end() { return st.end(); }
	auto empty() { return st.empty(); }
	auto size() { return st.size(); }
	auto insert(int key) {
		auto ret = st.insert(key);
		if (ret.second == true)
			its.push_back(ret.first);
		return ret;
	}
	auto random() {
		unsigned idx = rnd.nextUInt() % size();
		return *its[idx];
	}
	auto pop_random() {
		unsigned idx = rnd.nextUInt() % size();
		auto key = *its[idx];
		swap(its[idx], its.back());
		st.erase(key);
		its.pop_back();
		return key;
	}
};



// WTy : weight type
template<typename WTy> class RandomGraphGenerator {

	struct SXor128 {
		unsigned x, y, z, w;
		SXor128(unsigned _w) { x = 123456789; y = 362436069; z = 521288629; w = _w; }
		unsigned nextUInt() {
			unsigned t = (x ^ (x << 11));
			x = y; y = z; z = w;
			return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
		}
	} rnd;

	int V, E;
	WTy W;
	unsigned seed;

public:

	RandomGraphGenerator(int V, int E, WTy W = WTy(1), int seed = 88675123) : V(V), E(E), W(W), seed(seed), rnd(seed) {}

	vector<tuple<int, int, WTy>> generate() {
		
		// e = (u, v) を (u << 32) + v にコンバートして管理
		unordered_set<ll> edges;
		edges.reserve(E);

		// --- 全域木の生成 ---
		// T = {}, G = {0, 1, ..., |V| - 1}
		// G からランダムに始点 s を pop して T に追加
		// G が空になるまで:
		//	T からランダムに 1 点 u を選択
		//	G からランダムに 1 点 v を pop
		//	辺 e = (u, v) を張る
		//	v を T に追加
		RandomSet T(rnd.nextUInt()), G(rnd.nextUInt());
		for (int i = 0; i < V; i++)
			G.insert(i);
		T.insert(G.pop_random());
		for (int i = 0; i < V - 1; i++) {
			ll u = T.random();
			ll v = G.pop_random();
			edges.insert((u << 32) + v);
			T.insert((int)v);
		}

		// 辺が E 本になるまでランダムな 2 点に辺を張る
		int R = E - (int)edges.size();
		for (int i = 0; i < R; i++) {
			//if (edges.size() % 100000 == 0) {
			//	cerr << edges.size() << endl;
			//}
			ll u, v;
			do {
				u = T.random();
				v = T.random();
				while (u == v) {
					v = T.random();
				}
				if (u > v)
					swap(u, v);
			} while (edges.find((u << 32) + v) != edges.end()); // 多重辺を避ける
			edges.emplace((u << 32) + v);
		}

		vector<tuple<int, int, WTy>> weighted_edges;
		weighted_edges.reserve(E);

		for (const auto& e : edges)
			weighted_edges.emplace_back(int(e >> 32), int(e & 0xffffffff), WTy(rnd.nextUInt() % W + 1));

		return weighted_edges;
	}
};



int _main() {

	int V = 100000;
	int E = 10000000;
	ll W = 10000;


	clock_t elapses = clock();

	RandomGraphGenerator<ll> rgg(V, E, W);
	auto edges = rgg.generate();

	elapses = clock() - elapses;
	cerr << "Generate random graph: " << (double(elapses) / CLOCKS_PER_SEC) << " sec." << endl;

	
	elapses = clock();

	vector<vector<pll>> G(V);
	for (const auto& e : edges) {
		ll u, v, w;
		tie(u, v, w) = e;
		G[u].push_back(pll(w, v));
		G[v].push_back(pll(w, u));
	}

	elapses = clock() - elapses;
	cerr << "Create adjacent list: " << (double(elapses) / CLOCKS_PER_SEC) << " sec." << endl;


	elapses = clock();

	ll MSTcost = MST(G);

	elapses = clock() - elapses;
	cerr << "Calculate MSTcost: " << (double(elapses) / CLOCKS_PER_SEC) << " sec." << endl;


	cout << "MSTcost: " << MSTcost << endl;

	return 0;
}

int __main() {

	clock_t elapses = clock();

	RandomSet rs;

	int N = 10;

	for (int i = 0; i < N; i++) {
		rs.insert(i);
		cerr << "insert: " << i << endl;
		//cerr << "set: " << rs << endl;
	}

	for (int i = 0; i < N; i++) {
		int x = rs.pop_random();
		cerr << "pop_random: " << x << endl;
		//cerr << "set: " << rs << endl;
	}

	elapses = clock() - elapses;
	cerr << (double(elapses) / CLOCKS_PER_SEC) << " sec." << endl;

	return 0;
}

int main() {
	
	cin.tie(0);
	ios::sync_with_stdio(false);

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

	cout << MST(G) << endl;

	return 0;
}



Measured memory_bytes: 3188