#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for (ll i=0;i<(n);++i)
const ll MOD=1e9+7;

template<typename T>
void Warshall_Floyd(vector<vector<T>> &d,T id){
    for (int k=0;k<d.size();++k){
        for (int i=0;i<d.size();++i){
            for (int j=0;j<d.size();++j){
                if (d[i][k]==id||d[k][j]==id) continue;
                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }
}

const ll IINF=1e18;

int main(){
    int V,E; cin >> V >> E;
    vector<vector<ll>> g(V,vector<ll>(V,IINF));
    rep(i,V) g[i][i]=0;
    rep(i,E){
        int s,t; ll d; cin >> s >> t >> d;
        g[s][t]=d;
    }
    Warshall_Floyd(g,IINF);
    rep(i,V) if (g[i][i]<0){
        cout << "NEGATIVE CYCLE" << endl;
        return 0;
    }
    rep(i,V) rep(j,V){
        if (g[i][j]==IINF) cout << "INF";
        else cout << g[i][j];
        if (j!=V-1) cout << ' ';
        else cout << endl;
    }
}
