#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int graph[n][n];
  int edge[n][n];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      graph[i][j] = (1 << 30);
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      edge[i][j] = -1;
    }
  }
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    graph[a][b] = graph[b][a] = c;
    edge[a][b] = edge[b][a] = 1;
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j || j == k || k == i) continue;
        if (graph[i][j] > graph[i][k] + graph[k][j]) {
          graph[i][j] = graph[i][k] + graph[k][j];
          ++edge[i][k];
          ++edge[k][i];
          ++edge[j][k];
          ++edge[k][j];
          if (edge[i][j] > 0) --edge[i][j];
          if (edge[j][i] > 0) --edge[j][i];
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cout << edge[i][j] << ' ';
      if (i != j && edge[i][j] == 0) {
        ++ans;
      }
    }
    cout << endl;
  }
  cout << ans / 2 << endl;
  return 0;
}
