// GRL_1_A????????????
// ????????????????????? + ????????????????????????

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
using namespace std;

#define rep(i,a,n) for(int i=a; i<n; i++)
#define repr(i,a,n) for(int i=a; i>=n; i--)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF INT_MAX

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long int ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int const MOD = 1000000007;

// ????????¨???????????????????¨???????????§???????
struct Edge {
    int to, cost;
    Edge(int s, int d) : to(s), cost(d) {}
};

// 1??????????????????????????????????????±????????????????????????????????? vector
// Edges????????????????????????????????????????????°??????????????????
typedef vector<Edge> Edges;

// ???????????±??????????????? vector
// Graph??????????????????????????°?????????????????§??? V ??§?????????
typedef vector<Edges> Graph;

bool find_negative_loop_whole(Graph &G) {
    int V = G.size();
    vector<int> d(V);
    fill(d.begin(), d.end(), 0);
    for(int i=0; i<V; i++) {
        for(int j=0; j<V; j++) {
            for(int k=0; k<G[j].size(); k++) {
                Edge e = G[j][k];
                if(d[e.to] > d[j] + e.cost) {
                    d[e.to] = d[j] + e.cost;

                    // n ??????????????´??°???????????????????????????????????¨??????
                    if(i == V - 1) return true;
                }
            }
        }
    }
    return false;
}

// ???????????°V, ????????°E?????°????????????????????? s ???????????????????????????????????????????????¢????±???????
vector<int> bellman(Graph &G, int s) {
    // ???????????°??¨????????°?????°??????
    int V = G.size();
    vector<int> d(V);
    for(int i=0; i<V; i++) d[i] = INF;
    d[s] = 0;

    while(true) {
        bool update = false;
        for(int i=0; i<V; i++) {
            for(int j=0; j<G[i].size(); j++) {
                Edge e = G[i][j];
                if(d[i] != INF && d[e.to] > d[i] + e.cost) {
                    d[e.to] = d[i] + e.cost;
                    update = true;
                }
            }
        }
        if(!update) break;
    }
    return d;
}

namespace std {
    bool operator<(const P a, const P b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

int main() {
    int V, E;
    cin >> V >> E;
    Graph G(V);
    int s, t, d;
    rep(i,0,E) {
        cin >> s >> t >> d;
        G[s].push_back(Edge(t,d));
    }
    if(find_negative_loop_whole(G)) cout << "NEGATIVE CYCLE" << endl;
    else {
        rep(k,0,V) {
            vector<int> ans = bellman(G, k);
            rep(i,0,ans.size()) {
                if(i == 0) {
                    if(ans[i] == INF) cout << "INF";
                    else cout << ans[i];
                }
                else {
                    if(ans[i] == INF) cout << " INF";
                    else cout << " " << ans[i];
                }
            }
            cout << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 1308