import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int n, m;
static long matrix[][];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
m = Integer.parseInt(br.readLine());
matrix = new long[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j)
matrix[i][j] = 0;
else
matrix[i][j] = Integer.MAX_VALUE;
}
}
for (int i = 1; i <= m; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
if (matrix[s][e] > cost)
matrix[s][e] = cost;
}
for (int k = 1; k <= n; k++) {
for (int s = 1; s <= n; s++) {
for (int e = 1; e <= n; e++) {
matrix[s][e] = Math.min(matrix[s][e], matrix[s][k] + matrix[k][e]);
}
}
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++) {
if (matrix[r][c] == Integer.MAX_VALUE)
System.out.print("0 ");
else
System.out.print(matrix[r][c] + " ");
}
System.out.println();
}
}
}