using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace aoj_graph2
{
    class Program
    {
        static void Main()
        {
            string[] ve = Console.ReadLine().Split(' ');
            int v = int.Parse(ve[0]);
            int e = int.Parse(ve[1]);
            bool nega = false;
            Rinf[,] ans = new Rinf[v, v];
            for (int i = 0; i < v; ++i)
            {
                for (int j = 0; j < v; ++j)
                {
                    ans[i, j].isInf = true;
                    ans[i, j].value = 0;
                }
                ans[i, i].isInf = false;
            }
            for (int i = 0; i < e; ++i)
            {
                string[] std = Console.ReadLine().Split(' ');
                int s = int.Parse(std[0]);
                int t = int.Parse(std[1]);
                int d = int.Parse(std[2]);
                ans[s, t].isInf = false;
                ans[s, t].value = d;
            }
            for (int k = 0; k < v; ++k)
            {
                for (int i = 0; i < v; ++i)
                {
                    for (int j = 0; j < v; ++j)
                    {
                        if (ans[i, k] + ans[k, j] < ans[i, j])
                        {
                            ans[i, j] = ans[i, k] + ans[k, j];
                        }
                    }
                    if (ans[i, i].value < 0)
                    {
                        nega = true;
                        break;
                    }
                }
            }
            if (nega)
                Console.WriteLine("NEGATIVE CYCLE");
            else
            {
                for (int i = 0; i < v; ++i)
                {
                    string s = "";
                    for (int j = 0; j < v; ++j)
                    {
                        if (!ans[i, j].isInf)
                            s += ans[i, j].value.ToString();
                        else
                            s += "INF";

                        if (j < v - 1)
                            s += " ";
                    }
                    Console.WriteLine(s);
                }
            }

        }
        struct Rinf
        {
            public bool isInf;
            public int value;

            public static Rinf operator +(Rinf x, Rinf y)
            {
                Rinf n = new Rinf();
                if (!x.isInf && !y.isInf)
                {
                    n.isInf = false;
                    n.value = x.value + y.value;
                    return n;
                }
                else 
                {
                    n.isInf = true;
                    n.value = 0;
                    return n;
                }
            }
            public static bool operator <(Rinf x, Rinf y) 
            {
                if (x.isInf)
                    return false;
                else if (y.isInf)
                    return true;
                else
                {
                    if (x.value < y.value)
                        return true;
                    else
                        return false;
                }
            }
            public static bool operator >(Rinf x, Rinf y)
            {
                return y < x;
            }

        }

    }
}

Measured memory_bytes: 20612