#include<iostream>
#include<cstring>
#include<algorithm>
#define re register
using namespace std;
const int N = 1e3;
int n,m;
int dp[N][N];
inline void cs(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(dp,0x3f,sizeof(dp));
return ;
}
int main(){
cs();
cin>>n>>m;
for(re int i = 1;i <= m;i ++){
int u,v,w;
cin>>u>>v>>w;
dp[u][v] = w;
dp[v][u] = w;
}
for(re int i = 1;i <= n;i ++){
dp[i][i] = 0;
}
for(re int k = 1;k <= n;k ++){
for(re int i = 1;i <= n;i ++){
for(re int j = 1;j <= n;j ++){
dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);
}
}
}
for(re int i = 1;i <= n;i ++){
for(re int j = 1;j <= n;j ++){
cout<<dp[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}