import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;



void main() {
    immutable long inf = 1L << 59;

    auto s = readln.split.map!(to!int);
    auto v = s[0];
    auto e = s[1];

    auto g = new long[][](v, v);
    foreach (i; 0..v) g[i][] = inf;
    foreach (i; 0..v) g[i][i] = 0;

    foreach (_; 0..e) {
        s = readln.split.map!(to!int);
        auto x = s[0];
        auto y = s[1];
        auto cost = s[2];
        g[x][y] = cost;
    }

    auto dist = warshall_floyd!(long, inf)(g);

    if (v.iota.map!(i => dist[i][i] < 0).any) {
        writeln("NEGATIVE CYCLE");
    } else {
        dist.each!(a => a.map!(b => b == inf ? "INF" : to!string(b)).join(" ").writeln);
    }
}

T[][] warshall_floyd(T, T inf)(const T[][] graph) {
    import std.conv : to;

    int n = graph.length.to!int;
    auto dist = new T[][](n, n);
    foreach (i; 0..n) foreach (j; 0..n) dist[i][j] = graph[i][j];

    foreach (i; 0..n)
        foreach (j; 0..n)
            foreach (k; 0..n)
                if (dist[j][i] != inf && dist[i][k] != inf)
                    dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);

    return dist;
}

