#include <iostream>
#include <fstream>
#define infinit 1e9
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int n, m;
int a[105][105], c[105][105];
void citire()
{
int x, y, cost;
fin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j)
c[i][j] = infinit;
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> cost;
a[x][y] = 1;
c[x][y] = cost;
}
}
void RoyFloyd()
{
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
c[i][j] = c[i][k] + c[k][j] < c[i][j] ? c[i][k] + c[k][j] : c[i][j];
}
int main()
{
citire();
RoyFloyd();
for (int i = 1; i <= n; i++, fout << '\n')
for (int j = 1; j <= n; j++)
if (c[i][j] != infinit)
fout << c[i][j] << ' ';
else fout << -1 << ' ';
return 0;
}