#include "bits/stdc++.h"
#include <unordered_set>

#define _CRT_SECURE_NO_WARNINGS

#define REP(i, n) for(decltype(n) i = 0; i < (n); i++)
#define REP2(i, x, n) for(decltype(x) i = (x); i < (n); i++)
#define REP3(i, x, n) for(decltype(x) i = (x); i <= (n); i++)
#define RREP(i, n) for (decltype(n) i = (n) - 1; i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

using namespace std;

// N, E, S, W
const int dx[4] = { 1, 0, -1, 0};
const int dy[4] = { 0, 1, 0, -1};

using LL = long long int;
using LD = long double;

const int INF = (1 << 30) - 1;
const LL INF64 = ((LL)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

using pii = pair<int, int>;
using pll = pair<LL, LL>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<LL>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vd = vector<double>;
using vs = vector<string>;
using vb = vector<bool>;

//==============================================
int n, e;
vvl M;

void warshallFloyd() {
	REP(k, n) {
		REP(i, n) {
			if (M[i][k] == INF64) continue;
			REP(j, n) {
				if (M[k][j] == INF64) continue;
				M[i][j] = min(M[i][j], M[i][k] + M[k][j]);
			}
		}
	}
}

void print() {
	REP(i, n) {
		if (M[i][i] < 0) {
			cout << "NEGATIVE CYCLE" << "\n";
			return;
		}
	}

	REP(i, n) {
		REP(j, n) {
			if (j) cout << " ";
			if (M[i][j] == INF64) cout << "INF";
			else cout << M[i][j];
		}
		cout << "\n";
	}
	
}

void solve() {
	warshallFloyd();
	print();
}

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

	cin >> n >> e;

	M.resize(n, vl(n, INF64));
	REP(i, n) M[i][i] = 0;

	LL s, t, d;
	REP(i, e) {
		cin >> s >> t >> d;
		M[s][t] = d;
	}

	solve();
	return 0;
}
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

vll par;
vll rnk;
void init(lli n){
    par = vll(n);
    rnk = vll(n);
    for(lli i = 0;i < n;i++) par[i] = i;
}
lli find(lli x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}
void unite(lli x,lli y){
    x = find(x);y = find(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) par[x] = y;
    else {
        par[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}
bool same(lli x,lli y){
    return find(x) == find(y);
}

lli n;
vll c,r,l;
vll p;
mat g;
lli ans = 0;

int main(){
    cin >> n;
    g = mat(n,vll(n));
    for(lli i = 0;i < n;i++){
        for(lli j = 0;j < n;j++){
            cin >> g[i][j];
        }
    }
    for(lli i = 0;i < n;i++){
        for(lli j = i+1;j < n;j++){
            if(g[i][j] >= 0) c.push_back(g[i][j]),r.push_back(i),l.push_back(j);
        }
    }
    p = vll(c.size());
    for(lli i = 0;i < p.size();i++) p[i] = i;
    sort(p.begin(),p.end(),[](lli a,lli b){
        return c[a] < c[b];
    });
    init(n);
    for(lli i = 0;i < p.size();i++){
        if(!same(r[p[i]],l[p[i]])) unite(r[p[i]],l[p[i]]),ans += c[p[i]];
    }
    cout << ans << endl;
    return 0;
}