using System;
using System.Linq;

namespace GRL1C{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var V = sr.Next<int>();
            var E = sr.Next<int>();

            var wf = new WarshallFloyd(V);
            for(var i = 0; i < E; i++){
                wf.AddEdge(sr.Next<int>(), sr.Next<int>(), sr.Next<int>());
            }

            var res = wf.Solve();
            if(res == null){
                Console.WriteLine("NEGATIVE CYCLE");
                return;
            }

            Array.ForEach(res, a => Console.WriteLine(string.Join(" ", a.Select(x => x == int.MaxValue ? "INF" : x.ToString()).ToArray())));
            //---------------------------------
        }
    }

    public class WarshallFloyd{
        private readonly int _v;
        private readonly int[][] _dist;

        public WarshallFloyd(int v){
            _v = v;
            _dist = new int[v][];
            for(var i = 0; i < v; i++){
                _dist[i] = new int[v];
            }

            for(var i = 0; i < v; i++){
                for(var j = 0; j < v; j++){
                    if(i != j) _dist[i][j] = int.MaxValue;
                }
            }
        }

        public void AddEdge(int from, int to, int cost){
            _dist[from][to] = cost;
        }

        public int[][] Solve(){
            for(var k = 0; k < _v; k++){
                for(var i = 0; i < _v; i++){
                    for(var j = 0; j < _v; j++){
                        if(_dist[i][k] != int.MaxValue && _dist[k][j] != int.MaxValue){
                            _dist[i][j] = Math.Min(_dist[i][j], _dist[i][k] + _dist[k][j]);
                        }
                    }
                }
            }

            for(var i = 0; i < _v; i++){
                if(_dist[i][i] < 0) return null;
            }

            return _dist;
        }
    }
   
    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr = new System.IO.StreamReader(Console.OpenStandardInput());

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var ret = new T[x];
            for(var i = 0; i < x; ++i) ret[i] = Next<T>();
            return ret;
        }

        public T[][] Next<T>(int y, int x){
            var ret = new T[y][];
            for(var i = 0; i < y; ++i) ret[i] = Next<T>(x);
            return ret;
        }
    }
}

Measured memory_bytes: 10460