v, e = gets.split.map &:to_i
a = Array.new(v).map { Array.new(v, Float::INFINITY) }
e.times do
  s, t, d = gets.split.map &:to_i
  a[s][t] = d
end

(0..(v-1)).each do |i|
  a[i][i] = 0
end

(0..(v-1)).each do |k|
  (0..(v-1)).each do |i|
    (0..(v-1)).each do |j|
      a[i][j] = [ a[i][j], a[i][k] + a[k][j] ].min
    end
  end
end

is_nega = false
(0..(v-1)).each do |i|
  (0..v-1).each do |j|
    if i == j && a[i][j] < 0
      is_nega = true
      break
    end
    if a[i][j] == Float::INFINITY
      a[i][j] = "INF"
    end
  end
end

if is_nega
  puts "NEGATIVE CYCLE"
else
  (0..v-1).each do |i|
    puts a[i] * ' '
  end
end