#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

//頂点数がn,枝数がm
//bellman_ford bf(n,m);
//適宜add_edge
//bf.comp(出発点)でdに最短路長が入る
//負の有向閉路には3種類存在することに注意する

template<typename T> class bellman_ford {
public:
	struct edge{
		int from,to;
		T cost;
	};
	vector<int> d;
	vector<edge> es;
	int edge_id;
	int V,E;
	bellman_ford(int node_size){
		edge_id = 0, V = node_size;
		d.resize(node_size,numeric_limits<T>::max());
	}
	void add_edge(int from,int to,T cost){
		es.push_back((edge){from,to,cost});
	}
	//sからの最短路長およびsからたどり着ける負の閉路の検出(trueなら負の閉路が存在する)
	bool solve(int s){
		E = (int)es.size();
		fill(d.begin(),d.end(),numeric_limits<T>::max());
		int cnt = 0;
		d[s] = 0;
		while(cnt < V){
			bool update = false;
			rep(i,E){
				edge e = es[i];
				if(d[e.from] != numeric_limits<T>::max() && d[e.to] > d[e.from] + e.cost){
					d[e.to] = d[e.from] + e.cost;
					update = true;
				}
			}
			if(!update) break;
			cnt++;
		}
		return (cnt == V);
	}
	//すべての負の閉路の検出(trueなら負の閉路が存在する)
	bool find_negative_loop(){
		E = (int)es.size();
	    fill(d.begin(),d.end(),0);
		int cnt = 0;
		while(cnt < V){
 		   bool update = false;
 		   rep(i,E){
 			   edge e = es[i];
 			   if(d[e.to] > d[e.from] + e.cost){
 				   d[e.to] = d[e.from] + e.cost;
 				   update = true;
 			   }
 		   }
 		   if(!update) break;
 		   cnt++;
 	   }
 	   return (cnt == V);
	}
	//sからtへの最短路上に存在する負の閉路の検出(trueなら負の閉路が存在する)
	bool find_negative_loop(int s,int t){
		E = (int)es.size();
		d[s] = 0;
		rep(i,2*V){
			rep(j,E){
				edge e = es[j];
				if(d[e.from] != numeric_limits<T>::max() && d[e.to] > d[e.from] + e.cost){
					d[e.to] = d[e.from] + e.cost;
					if(i >= V-1 && e.to == t){
						return true;
					}
				}
			}
		}
		return false;
	}
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
	bellman_ford<int> bf(n);
	rep(i,m){
		int a,b,c;
		cin >> a >> b >> c;
		bf.add_edge(a,b,c);
	}
	if(bf.find_negative_loop()){
		cout << "NEGATIVE CYCLE\n";
	}else{
		rep(i,n){
			bf.solve(i);
			rep(j,n-1){
				if(bf.d[j] == INT_MAX){
					cout << "INF ";
				}else{
					cout << bf.d[j] << " ";
				}
			}
			if(bf.d[n-1] == INT_MAX){
				cout << "INF";
			}else{
				cout << bf.d[n-1];
			}
			cout << "\n";
		}
	}
    return 0;
}



Measured memory_bytes: 3308