Language: C++

#define MOD 10007

#if 1
//------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;

template <class T>
struct Entry { static void Run() { T().Run(); } };
struct MyMain;

#if defined(TEST)
#include "test.hpp"
#else
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout << fixed << setprecision(16);
	Entry<MyMain>::Run();
	return 0;
}
#endif

//------------------------------------------------------------
using ll = long long;
#define int ll

#define FOR(i, s, e) for (ll i = ll(s); i < ll(e); ++i)
#define RFOR(i, s, e) for (ll i = ll(e) - 1; i >= ll(s); --i)
#define REP(i, n) for (ll i = 0, i##_size = ll(n); i < i##_size; ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)

//------------------------------------------------------------
template <class T>
struct ContainerHolder {
	bool flag = true;
	T& var;
	ContainerHolder(T& v) : var(v) {}
	operator bool() const { return flag; }
	T* operator->() { return &var; }
};
template <class T> ContainerHolder<T> ContainerHold(T& v) { return ContainerHolder<T>(v); }

#define CFOR(it, container) \
	for (auto holder = ContainerHold(container); holder; holder.flag = false) \
	for (auto it = holder->begin(), endIt = holder->end(); it != endIt; ++it)

//------------------------------------------------------------
template <class T>
struct arr : public vector<T> {
	arr() {}
	arr(initializer_list<T> il) : vector<T>(il) {}
	explicit arr(ll n, T v = T()) : vector<T>(n, v) {}

	T& operator()(int i) { return (*this)[i]; }
	T const& operator()(int i) const { (*this)[i]; }
	void init(ll n, T v = T()) {
		this->clear();
		this->resize(n, v);
	}
	ll sz() const { return (ll)this->size(); }
	void pb(T v) { this->push_back(v); }
	void sort() { std::sort(this->begin(), this->end()); }
	template <class PREDICATE> void sort(PREDICATE p) { std::sort(this->begin(), this->end(), p); }
	void rsort() { std::sort(this->begin(), this->end(), greater<T>()); }
	void unique_erase() { this->erase(std::unique(this->begin(), this->end()), this->end()); }
	bool next_permutation() { return std::next_permutation(this->begin(), this->end()); }

	// これ以下はソート済み前提
	template <class P>
	int lower_bound(T const& v, P p) { return std::lower_bound(this->begin(), this->end(), v, p) - this->begin(); }
	int lower_bound(T const& v) { return std::lower_bound(this->begin(), this->end(), v) - this->begin(); }

	template <class P>
	int upper_bound(T const& v, P p) { return std::upper_bound(this->begin(), this->end(), v, p) - this->begin(); }
	int upper_bound(T const& v) { return std::upper_bound(this->begin(), this->end(), v) - this->begin(); }

	int find_nearest(T const& v) {
		int i = this->lower_bound(v);
		if (i >= sz()) {
			--i;
		}
		else if ((*this)[i] != v) {
			int p = i - 1;
			if (p >= 0) {
				int id = abs((*this)[i] - v);
				int pd = abs((*this)[p] - v);
				if (pd < id) {
					i = p;
				}
			}
		}
		return i;
	}

	// 見つからなければ-1
	int find(T const& v) {
		int i = this->lower_bound(v);
		if (i >= sz()) {
			return -1;
		}
		if ((*this)[i] != v) {
			return -1;
		}
		return i;
	}

};
using ints = arr<ll>;

template <class T>
struct que : public queue<T> {
	ll sz() const { return (ll)this->size(); }
	T popfront() {
		T v = this->front();
		this->pop();
		return v;
	}
};

template <class A, class B>
struct pr {
	union {
		A a;
		A key;
		A first;
		A x;
	};
	union {
		B b;
		B value;
		B second;
		B y;
	};

	pr() : a(A()), b(B()) {};
	pr(A a_, B b_) : a(a_), b(b_) {}
	pr(pr const& r) : a(r.a), b(r.b) {};
	pr(pair<A, B> const& r) : a(r.first), b(r.second) {};

	bool operator == (pr const& r) const { return a == r.a && b == r.b; }
	bool operator != (pr const& r) const { return !((*this) == r); }
	bool operator < (pr const& r) const {
		if (a == r.a) {
			return b < r.b;
		}
		return a < r.a;
	}

	pr operator + (pr v) const { return pr(x, y) += v; }
	pr operator - (pr v) const { return pr(x, y) -= v; }

	pr& operator += (pr v) {
		x += v.x;
		y += v.y;
		return *this;
	}
	pr& operator -= (pr v) {
		x -= v.x;
		y -= v.y;
		return *this;
	}

	void flip() { swap(x, y); }

	friend istream& operator>>(istream& is, pr& p) {
		is >> p.a >> p.b;
		return is;
	}
	friend ostream& operator<<(ostream& os, pr const& p) {
		os << p.a << " " << p.b;
		return os;
	}
};
using pint = pr<ll, ll>;
using pints = arr<pint>;

template <class K, class V>
struct dic : public map<K, V> {
	bool get(K const& k, V* v) {
		auto it = this->find(k);
		if (it != this->end()) {
			*v = it->second;
			return true;
		}
		return false;
	}
};

template <class T>
struct arr2 {
	vector<T>	m_vec;
	int			m_width;
	int			m_height;

	arr2() : m_width(0), m_height(0) {}
	arr2(int w, int h, T const& value = T()) : m_width(w), m_height(h) {
		m_vec.resize(w * h, value);
	}
	arr2(arr2 const& r) {
		m_vec = r.m_vec;
		m_width = r.m_width;
		m_height = r.m_height;
	}
	arr2(arr2&& r) {
		m_vec = move(r.m_vec);
		m_width = r.m_width;
		m_height = r.m_height;
	}
	arr2& operator=(arr2 const& r) {
		m_vec = r.m_vec;
		m_width = r.m_width;
		m_height = r.m_height;
		return *this;
	}
	arr2& operator=(arr2&& r) {
		m_vec = move(r.m_vec);
		m_width = r.m_width;
		m_height = r.m_height;
		return *this;
	}

	pint size() const { return pint(m_width, m_height); }
	int width() const { return m_width; }
	int height() const { return m_height; }

	void init(int w, int h, T const& value = T()) {
		m_vec.clear();
		m_vec.resize(w * h, value);
		m_width = w;
		m_height = h;
	}
	void init(pint size, T const& value = T()) {
		init(size.x, size.y, value);
	}
	T& operator()(int x, int y) { return m_vec[y * m_width + x]; }
	T const& operator()(int x, int y) const { return m_vec[y * m_width + x]; }

	T& operator()(pint p) { return m_vec[p.y * m_width + p.x]; }
	T const& operator()(pint p) const { return m_vec[p.y * m_width + p.x]; }

	T& operator[](pint p) { return m_vec[p.y * m_width + p.x]; }
	T const& operator[](pint p) const { return m_vec[p.y * m_width + p.x]; }

	bool isIn(int x, int y) const {
		return
			x >= 0 && x < m_width &&
			y >= 0 && y < m_height;
	}
	bool isIn(pint p) const { return isIn(p.x, p.y); }
	bool isOut(int x, int y) const {
		return
			x < 0 || x >= m_width ||
			y < 0 || y >= m_height;
	}
	bool isOut(pint p) const { return isOut(p.x, p.y); }

	struct iterator {
	private:
		arr2<T>* owner;

	public:
		pint pt;

		iterator(arr2<T>* owner_, pint pt_) : owner(owner_), pt(pt_) {}

		bool operator ==(iterator const& r) const {
			return pt == r.pt;
		}
		bool operator !=(iterator const& r) const {
			return !((*this) == r);
		}
		void operator++() {
			++pt.x;
			if (pt.x >= owner->width()) {
				++pt.y;
				pt.x = 0;
			}
		}
		T& operator*() {
			return (*owner)(pt);
		}
	};
	iterator begin() {
		return iterator(this, pint(0, 0));
	}
	iterator end() {
		return iterator(this, pint(0, height()));
	}

	void disp(ostream& os) {
		REP(y, m_height) {
			REP(x, m_width) {
				os << setw(2) << (*this)(x, y) << " ";
			}
			os << endl;
		}
		os << endl;
	}
};

const pints around4 = { pint(-1, 0), pint(0, -1), pint(1, 0), pint(0, 1) };

// 6角形グリッド用
const pints around6[2] = {
	{ pint(0, -1), pint(1, -1), pint(-1, 0), pint(1, 0), pint(0, 1), pint(1, 1) },
	{ pint(-1, -1), pint(0, -1), pint(-1, 0), pint(1, 0), pint(-1, 1), pint(0, 1) }
};

//------------------------------------------------------------
template <class T> void chmin(T& a, T b) { if (b < a) { a = b; } }
template <class T> void chmax(T& a, T b) { if (b > a) { a = b; } }

constexpr int gcd(int a, int b) {
	if (a < 0) { a = -a; }
	if (b < 0) { b = -b; }
	if (a == 0) { return b; }
	if (b == 0) { return a; }

	while (int c = a % b) {
		a = b;
		b = c;
	}
	return b;
}

struct bfs {
	ints froms;
	ints steps;

	bfs(int N, int start, function<ints(int)> getAround) {
		froms.init(N, -1);
		steps.init(N, -1);

		que<ll> queue;
		queue.push(start);
		froms[start] = start;
		steps[start] = 0;

		while (queue.empty() == false) {
			ll p = queue.popfront();

			for (ll n : getAround(p)) {
				if (steps[n] != -1) {
					continue;
				}

				froms[n] = p;
				steps[n] = steps[p] + 1;
				queue.push(n);
			}
		}
	}
};

struct BfsResult {
	arr2<pint> froms;
	arr2<int> steps;
};

template <class T, class F>
BfsResult bfs2(arr2<T> const& field, pint start, F canMove) {
	BfsResult result;
	result.froms.init(field.size(), pint(-1, -1));
	result.steps.init(field.size(), -1);

	que<pint> queue;
	queue.push(start);
	result.froms(start) = start;
	result.steps(start) = 0;

	while (queue.empty() == false) {
		auto p = queue.popfront();

		for (auto a : around4) {
			pint n = p + a;
			if (field.isOut(n)) {
				continue;
			}
			if (result.steps(n) != -1) {
				continue;
			}
			if (!canMove(field(n))) {
				continue;
			}

			result.froms(n) = p;
			result.steps(n) = result.steps(p) + 1;
			queue.push(n);
		}
	}

	return result;
}

// getAround(count) -> around
// getCost(from, to) -> cost
int d9str(int N, int a, int b, function<ints(int)> getAround, function<int(int, int)> getCost) {
	struct Node {
		int point;
		int totalCost;

		bool operator < (Node const& n) const {
			return totalCost > n.totalCost;
		}
	};

	ints minCosts(N, INT64_MAX);
	priority_queue<Node> que;

	minCosts(a) = 0;
	que.push(Node{ a, 0});

	while (!que.empty()) {
		Node node = que.top();
		que.pop();

		for (int next : getAround(node.point)) {
			int cost = getCost(node.point, next);
			if (cost < 0) {
				continue;
			}

			Node newNode;
			newNode.point = next;
			newNode.totalCost = node.totalCost + cost;

			if (newNode.totalCost < minCosts[newNode.point]) {
				minCosts[newNode.point] = newNode.totalCost;
				que.push(newNode);
			}
		}
	}

	if (minCosts[b] == INT64_MAX) {
		return -1;
	}

	return minCosts[b];
}

// graph : 隣接行列。i=jは0、未接続はinfで初期化済みであること
// return false なら負の閉路あり
bool WarshallFloyd(arr2<int>& graph, int inf) {
	int N = graph.width();

	REP(k, N) {
		REP(i, N) {
			REP(j, N) {
				if (graph(i, k) == inf || graph(k, j) == inf) {
					continue;
				}
				chmin(graph(i, j), graph(i, k) + graph(k, j));
			}
		}
	}

	REP(i, N) {
		if (graph(i, i) < 0) {
			return false;
		}
	}

	return true;
}


template <class PREDICATE>
int bsearch(int l, int r, PREDICATE p) {
	while (l < r) {
		int c = (l + r) / 2;

		if (p(c)) {
			r = c;
		}
		else {
			l = c + 1;
		}
	}

	return r;
}

// func(x)の極値となるxを返す
double num_analyze(function<double(double)> func, double l, double r, int step = 10000, double delta = 0.000001) {
	auto diff = [&](double x) {
		double a = func(x);
		double b = func(x + delta);
		return (b * b - a * a);
	};

	REP(i, step) {
		double c = (l + r) / 2.0;
		double d = diff(c);
		if (d < 0) {
			l = c;
		}
		else {
			r = c;
		}
	}
	return l;
}

//------------------------------------------------------------
template <int M>
struct modint {
	int raw;

	modint() { raw = 0; }
	modint(int v) {
		if (v < 0) {
			raw = (v % M) + M;
		}
		else if (v >= M) {
			raw = v % M;
		}
		else {
			raw = v;
		}
	}
	modint operator + (modint v) const { return modint(raw) += v; }
	modint operator - (modint v) const { return modint(raw) -= v; }
	modint operator * (modint v) const { return modint(raw) *= v; }

	modint& operator += (modint v) {
		raw += v.raw;
		if (raw >= M) { raw -= M; }
		return *this;
	}
	modint& operator -= (modint v) {
		raw -= v.raw;
		if (raw < 0) { raw += M; }
		return *this;
	}
	modint& operator *= (modint v) {
		raw = (raw * v.raw) % M;
		return *this;
	}
	modint pow(int n) const {
		return modint::pow(raw, n);
	}
	static modint pow(int a, int n) {
		if (n < 0) {
			// not support
			abort();
		}

		int r = 1;
		while (n) {
			if (n & 1) {
				r = (r * a) % M;
			}
			a = (a * a) % M;
			n >>= 1;
		}
		return modint(r);
	}

	friend istream& operator>>(istream& is, modint& m) {
		int v;
		is >> v;
		m = modint(v);
		return is;
	}
	friend ostream& operator<<(ostream& os, modint const& m) {
		return os << m.raw;
	}
};

using mint = modint<MOD>;
using mints = arr<mint>;

//------------------------------------------------------------
struct LoopIndex {
	int const N;
	LoopIndex(int N_) : N(N_) { }

	int left(int i) {
		return (i - 1 + N) % N;
	}
	int right(int i) {
		return (i + 1) % N;
	}
};

//------------------------------------------------------------
#if defined(TEST)
extern istream& myInputStream;
extern ostream& myOutputStream;
#else
istream& myInputStream = cin;
ostream& myOutputStream = cout;
#endif

template <class T>
auto output(T var) -> decltype(myOutputStream << var) {
	myOutputStream << var << '\n';
	return myOutputStream;
}

template <class T>
auto output(T var, char const* deli) -> decltype(myOutputStream << var) {
	myOutputStream << var << deli;
	return myOutputStream;
}

template <class H, class... T>
auto output(H&& head, T&&... tail) -> decltype(myOutputStream << head)
{
	myOutputStream << head << " ";
	output(forward<T>(tail)...);
	return myOutputStream;
}

template <class T>
auto output(arr<T> const& var) -> ostream& {
	for (int i = 0; i < var.sz() - 1; ++i) {
		myOutputStream << var[i] << ' ';
	}
	if (var.sz() > 0) {
		myOutputStream << var[var.sz() - 1] << '\n';
	}
	else {
		myOutputStream << '\n';
	}
	return myOutputStream;
}

void flush() {
	myOutputStream << '\n';
}

//------------------------------------------------------------

template <class T>
struct in_base {
	T value;
	in_base() { myInputStream >> value; }
	operator T() { return value; }
};

struct in_int {
	int value;

	in_int() {
		myInputStream >> value;
	}
	in_int(int add) {
		myInputStream >> value;
		value += add;
	}
	operator int() {
		return value;
	}
};

template <class A, class B>
struct in_pr {
	pr<A, B> value;

	in_pr() {
		myInputStream >> value.a >> value.b;
	}
	in_pr(int addA, int addB) {
		myInputStream >> value.a >> value.b;
		value.a += addA;
		value.b += addB;
	}
	in_pr(bool flip) {
		myInputStream >> value.b >> value.a;
	}
	in_pr(int addA, int addB, bool flip) {
		myInputStream >> value.b >> value.a;
		value.a += addB;
		value.b += addA;
	}
	operator pr<A, B>() {
		return value;
	}
};

template <class T>
struct in_arr {
	arr<T> value;

	in_arr(int N) {
		value.init(N);
		REP(i, N) {
			myInputStream >> value[i];
		}
	}
	in_arr(int N, int add) {
		value.init(N);
		REP(i, N) {
			myInputStream >> value[i];
			value[i] += add;
		}
	}
	operator arr<T>() {
		return value;
	}
};

template <class A, class B>
struct in_dic {
	dic<A, B> value;

	in_dic(int N) {
		REP(i, N) {
			pair<A, B> pr;
			myInputStream >> pr.first >> pr.second;
			value.insert(pr);
		}
	}
	in_dic(int N, int addA, int addB) {
		REP(i, N) {
			pair<A, B> pr;
			myInputStream >> pr.first >> pr.second;
			pr.first += addA;
			pr.second += addB;
			value.insert(pr);
		}
	}
	in_dic(int N, bool flip) {
		REP(i, N) {
			pair<A, B> pr;
			myInputStream >> pr.second >> pr.first;
			value.insert(pr);
		}
	}
	in_dic(int N, int addA, int addB, bool flip) {
		REP(i, N) {
			pair<A, B> pr;
			myInputStream >> pr.second >> pr.first;
			pr.first += addB;
			pr.second += addA;
			value.insert(pr);
		}
	}
	operator dic<A, B>() {
		return value;
	}
};

template <class T>
struct in_arr2 {
	arr2<T> value;

	in_arr2(int H, int W) {
		value.init(W, H);
		REP(y, H) {
			REP(x, W) {
				myInputStream >> value(x, y);
			}
		}
	}
	in_arr2(int H, int W, int add) {
		value.init(W, H);
		REP(y, H) {
			REP(x, W) {
				myInputStream >> value(x, y);
				value(x, y) += add;
			}
		}
	}

	operator arr2<T>() {
		return value;
	}
};

template <class... ARGS>
struct inputWithParam {
	tuple<ARGS...> param;
	inputWithParam() {}
	inputWithParam(tuple<ARGS...>&& param_) : param(param_) { }

	template <int N>
	struct Expand {
		template <typename F, typename Tuple, typename... Args>
		static F apply(Tuple& t, Args&... args)
		{
			return Expand<N - 1>::apply<F>(t, std::get<N - 1>(t), args...);
		}
	};

	template <>
	struct Expand<0> {
		template <typename F, typename Tuple, typename... Args>
		static F apply(Tuple& t, Args&... args)
		{
			return F(args...);
		}
	};

	template <typename F, typename Tuple>
	F apply(Tuple& t)
	{
		return Expand<tuple_size<Tuple>::value>::apply<F>(t);
	}

	operator int() { return apply<in_int>(param); }
	operator double() { return apply<in_base<double>>(param); }
	operator string() { return apply<in_base<string>>(param); }
	operator char() { return apply<in_base<char>>(param); }

	template <class A, class B>
	operator pr<A, B>() { return apply<in_pr<A, B>>(param); }

	template <class T>
	operator arr<T>() { return apply<in_arr<T>>(param); }

	template <class A, class B>
	operator dic<A, B>() { return apply<in_dic<A, B>>(param); }

	template <class T>
	operator arr2<T>() { return apply<in_arr2<T>>(param); }

};

struct input {
	operator int() { return inputWithParam<>(); }
	operator double() { return inputWithParam<>(); }
	operator string() { return inputWithParam<>(); }
	operator char() { return inputWithParam<>(); }

	template <class A, class B>
	operator pr<A, B>() { return inputWithParam<>(); }

	template <class... ARGS>
	inputWithParam<ARGS...> operator()(ARGS&&... args) {
		inputWithParam<ARGS...> holder = forward_as_tuple(args...);
		return holder;
	}
} in;

//------------------------------------------------------------
#endif
#define INF INT64_MAX

struct MyMain {
	int N = in;
	arr2<int> A = in(N, N);

	void Run() {
		arr2<int> B(N, N);

		REP(i, N) {
			REP(j, N) {
				int a = INF;
				int b = INF;
				REP(k, N) {
					if (A(i, k) == INF || A(k, j) == INF) {
						continue;
					}

					chmin(a, A(i, k) + A(k, j));

					if (k != j && i != k) {
						chmin(b, A(i, k) + A(k, j));
					}
				}
				if (a != A(i, j)) {
					output("-1");
					return;
				}
				if (a == b) {
					B(i, j) = INF;
					B(j, i) = INF;
				}
				else {
					B(i, j) = a;
					B(j, i) = a;
				}
			}
		}

		int ans = 0;
		REP(i, N) {
			FOR(j, i + 1, N) {
				if (B(i, j) == INT64_MAX) {
					continue;
				}
				ans += B(i, j);
			}
		}

		output(ans);
	}
};



Compiler output:
a.cc:788:19: error: explicit specialization in non-namespace scope 'struct inputWithParam<ARGS>'
  788 |         template <>
      |                   ^
a.cc:789:16: error: too few template-parameter-lists
  789 |         struct Expand<0> {
      |                ^~~~~~~~~
a.cc: In static member function 'static F inputWithParam<ARGS>::Expand<N>::apply(Tuple&, Args& ...)':
a.cc:784:47: warning: expected 'template' keyword before dependent template name [-Wmissing-template-keyword]
  784 |                         return Expand<N - 1>::apply<F>(t, std::get<N - 1>(t), args...);
      |                                               ^~~~~
      |                                               template
a.cc:784:54: error: expected primary-expression before '>' token
  784 |                         return Expand<N - 1>::apply<F>(t, std::get<N - 1>(t), args...);
      |                                                      ^
a.cc:784:83: error: expected ')' before '...' token
  784 |                         return Expand<N - 1>::apply<F>(t, std::get<N - 1>(t), args...);
      |                                                                                   ^~~
a.cc:784:55: note: to match this '('
  784 |                         return Expand<N - 1>::apply<F>(t, std::get<N - 1>(t), args...);
      |                                                       ^
a.cc: In member function 'F inputWithParam<ARGS>::apply(Tuple&)':
a.cc:800:58: warning: expected 'template' keyword before dependent template name [-Wmissing-template-keyword]
  800 |                 return Expand<tuple_size<Tuple>::value>::apply<F>(t);
      |                                                          ^~~~~
      |                                                          template
a.cc:800:65: error: expected primary-expression before '>' token
  800 |                 return Expand<tuple_size<Tuple>::value>::apply<F>(t);
      |                                                                 ^