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

namespace AizuOnline
{
    public class Node
    {
        public int id;
        public bool isPassed = false;
        public int groupId;
        public int cost = int.MaxValue;
        public List<Edge> edges = new List<Edge>();
    }

    public class Edge
    {
        public int id = int.MinValue;
        public int fromId;
        public int toId;
        public int cost;
        public bool isPassed = false;
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var nodes = createDirectionNodesByList();

            warchalFloid(nodes);
            Console.ReadLine();
        }

        public static List<Node> createDirectionNodesByList()
        {
            var nodes = new List<Node>();
            var x = Console.ReadLine().Split(' ');
            var nodeNum = Convert.ToInt32(x[0]);

            for (int i = 0; i < nodeNum; i++)
            {
                nodes.Add(new Node()
                {
                    id = i
                });
            }

            var edgeNum = Convert.ToInt32(x[1]);
            for (int i = 0; i < edgeNum; i++)
            {
                var p = Console.ReadLine().Split(' ');
                var id = Convert.ToInt32(p[0]);

                var node = nodes.Where(m => m.id == id).FirstOrDefault();

                if (node == null)
                {
                    node = new Node()
                    {
                        id = id
                    };
                    nodes.Add(node);
                }

                node.edges.Add(new Edge()
                {
                    fromId = id,
                    toId = Convert.ToInt32(p[1]),
                    cost = Convert.ToInt32(p[2]),
                });
            }

            return nodes;
        }

        public static void warchalFloid(List<Node> nodes)
        {
            var count = nodes.Count;
            var dp = new int[count, count];
            for (int i = 0; i < nodes.Count; i++)
            {
                for (int j = 0; j < nodes.Count; j++)
                {
                    dp[i, j] = i == j ? 0 : int.MaxValue;
                }
            }

            foreach (var e in nodes.SelectMany(n => n.edges))
            {
                dp[e.fromId, e.toId] = e.cost;
            }

            for (int k = 0; k < count; k++)
            {
                for (int i = 0; i < count; i++)
                {
                    if (dp[i, k] == int.MaxValue)
                    {
                        continue;
                    }

                    for (int j = 0; j < count; j++)
                    {
                        if (dp[k, j] == int.MaxValue)
                        {
                            continue;
                        }
                        dp[i, j] = Math.Min(dp[i, j], dp[i, k] + dp[k, j]);
                    }
                }
            }

            for (int i = 0; i < count; i++)
            {
                if (dp[i, i] < 0)
                {
                    Console.WriteLine("NEGATIVE CYCLE");
                    return;
                }
            }

            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < count; j++)
                {
                    Console.Write(dp[i, j] == int.MaxValue ? "INF" : dp[i, j].ToString());

                    if (j != count - 1)
                    {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
        }
    }
}

Measured memory_bytes: 10632