#include<iostream>
//#include<conio.h>
#include<algorithm>
#include<vector>
#include<list>
using namespace std;
struct edge
{
int u;
int v;
int w;
}arr[500000];
inline int comp(struct edge a,struct edge b)
{
return (a.w>b.w);
}
int p[1005];
int rank[1005];
inline int makeset(int x)
{
p[x]=x;
rank[x]=0;
}
int root;
inline int findset(int x)
{
if(x!=p[x])
{ p[x]=findset(p[x]);p[x];}
return (p[x]);
}
inline void linkset(int x,int y)
{
if(rank[x]>rank[y])
p[y]=x;
else
p[x]=y;
if(rank[x]==rank[y])
rank[y]=rank[y]+1;
}
inline void unionset(int x,int y)
{
linkset(findset(x),findset(y));
}
vector<int> d;
int sum=100000019;
vector<int> sum1[500000];
inline void dfs(int a,int b)
{
while(b!=p[b])
{
if(sum>sum1[a][p[b]])
{sum=sum1[a][p[b]];sum1[a][p[b]]=sum;sum1[p[b]][a]=sum;
}
b=p[b];
}
}
int main()
{
int n,e,t,a,b,c,i,j,k,l;
scanf("%d%d",&n,&e);
for(i=0;i<e;i++)
scanf("%d%d%d",&arr[i].u,&arr[i].v,&arr[i].w);
sort(arr,arr+e,comp);
int visited[n+1];
int visit[n+1];
for(i=0;i<n;i++){p[i]=i;visited[i]=0;visit[i]=0; makeset(i);}
// for(i=0;i<e;i++)
// printf("%d%d%d\n",arr[i].u,arr[i].v,arr[i].w);
int x,y;
for(i=0;i<e;i++)
{
x=findset(arr[i].u);
y=findset(arr[i].v);
if(x!=y)
{
if(arr[i].u>arr[i].v)
{ for(l=0;l<=arr[i].u;l++)
{ sum1[x].push_back(100000009);
sum1[y].push_back(100000009);
sum1[arr[i].u].push_back(100000009);
sum1[arr[i].v].push_back(100000009);
}
}
else
{
for(l=0;l<=arr[i].v;l++)
{
sum1[x].push_back(100000009);
sum1[y].push_back(100000009);
sum1[arr[i].u].push_back(100000009);
sum1[arr[i].v].push_back(100000009);
}
}
unionset(x,y);
sum1[x][y]=arr[i].w;
sum1[y][x]=arr[i].w;
// printf("x %d y %d sum[x][y] %d\n",x,y,sum1[x][y]);
if(visit[arr[i].u]==0)
d.push_back(arr[i].u);
if(visit[arr[i].v]==0)
d.push_back(arr[i].v);
visit[arr[i].u]=1;
visit[arr[i].v]=1;
}
}
// for(i=0;i<e;i++)printf("hi %d\n",d[i]) ;
for(i=0;i<n;i++)
dfs(i,i);
int root=findset(0);
//printf("root %d \n",findset(0));
//printf("root %d \n",findset(2));
//printf("root %d \n",findset(3));
//printf("asdas %d\n",sum1[0][2]);
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{
int sum2=100000019;
int sum3=100000019;
if(i==j)printf("0 ");
else if(sum1[i][j]==100000009)
{ //printf("viaksh %d %d\n",i,j);
int r=i,s=j;
while(r!=p[r])
{
if(sum2>sum1[r][p[r]])
sum2=sum1[r][p[r]];
// printf("mayank r %d p[r] %d sum1[r][p[r]]%d\n",r,p[r],sum1[r][p[r]]);
r=p[r];
}
while(s!=p[s])
{
if(sum3>sum1[p[s]][s])
{ sum3=sum1[p[s]][s];
} //printf("churu p[s] %d s %d sum %d\n",p[s],s,sum1[p[s]][s]);
s=p[s];
}
if(sum2>sum3)
printf("%d ",sum3);
else
printf("%d ",sum2);
}
else
printf("%d ",sum1[i][j]);
}
printf("\n");
}
//system("pause");
return 0;
// getch();
}
I2luY2x1ZGU8aW9zdHJlYW0+Ci8vI2luY2x1ZGU8Y29uaW8uaD4KI2luY2x1ZGU8YWxnb3JpdGhtPgojaW5jbHVkZTx2ZWN0b3I+CiNpbmNsdWRlPGxpc3Q+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnN0cnVjdCBlZGdlCnsKICAgIGludCB1OwogICAgaW50IHY7CiAgICBpbnQgdzsKfWFycls1MDAwMDBdOwoKaW5saW5lIGludCBjb21wKHN0cnVjdCBlZGdlIGEsc3RydWN0IGVkZ2UgYikKewogICAgcmV0dXJuIChhLnc+Yi53KTsKfSAgCmludCBwWzEwMDVdOwppbnQgcmFua1sxMDA1XTsgICAgICAKCmlubGluZSBpbnQgIG1ha2VzZXQoaW50IHgpCnsKICAgIHBbeF09eDsKICAgIHJhbmtbeF09MDsKfSAgICAKaW50IHJvb3Q7CmlubGluZSBpbnQgZmluZHNldChpbnQgeCkKewogICAgaWYoeCE9cFt4XSkKICAgeyBwW3hdPWZpbmRzZXQocFt4XSk7cFt4XTt9CiAgICByZXR1cm4gKHBbeF0pOwp9ICAgCgppbmxpbmUgdm9pZCBsaW5rc2V0KGludCB4LGludCB5KQp7CiAgICBpZihyYW5rW3hdPnJhbmtbeV0pCiAgICBwW3ldPXg7CiAgICBlbHNlCiAgICBwW3hdPXk7CiAgICBpZihyYW5rW3hdPT1yYW5rW3ldKQogICAgcmFua1t5XT1yYW5rW3ldKzE7Cn0gIAoKaW5saW5lIHZvaWQgdW5pb25zZXQoaW50IHgsaW50IHkpCnsKICAgIGxpbmtzZXQoZmluZHNldCh4KSxmaW5kc2V0KHkpKTsKfSAgICAKCiAgdmVjdG9yPGludD4gZDsKCmludCBzdW09MTAwMDAwMDE5Owp2ZWN0b3I8aW50PiBzdW0xWzUwMDAwMF07CmlubGluZSB2b2lkIGRmcyhpbnQgYSxpbnQgYikKewogICAgd2hpbGUoYiE9cFtiXSkKICAgIHsKICAgICAgICBpZihzdW0+c3VtMVthXVtwW2JdXSkKICAgICAgICB7c3VtPXN1bTFbYV1bcFtiXV07c3VtMVthXVtwW2JdXT1zdW07c3VtMVtwW2JdXVthXT1zdW07CiAgICAgCiAgICAgICAgfQoKICAgICAgICAgICAKICAgICAgICBiPXBbYl07CiAgICB9Cn0gICAgICAgIAogCiAgIAoKCgppbnQgbWFpbigpCnsKICAgIGludCBuLGUsdCxhLGIsYyxpLGosayxsOwogICAgc2NhbmYoIiVkJWQiLCZuLCZlKTsKICAgIGZvcihpPTA7aTxlO2krKykKICAgIHNjYW5mKCIlZCVkJWQiLCZhcnJbaV0udSwmYXJyW2ldLnYsJmFycltpXS53KTsKICAgIAogICAgc29ydChhcnIsYXJyK2UsY29tcCk7CiAgICAgaW50IHZpc2l0ZWRbbisxXTsKICAgIGludCB2aXNpdFtuKzFdOwogICAgCiAgICAgZm9yKGk9MDtpPG47aSsrKXtwW2ldPWk7dmlzaXRlZFtpXT0wO3Zpc2l0W2ldPTA7IG1ha2VzZXQoaSk7fQogICAgCiAvLyAgIGZvcihpPTA7aTxlO2krKykKICAvLyAgcHJpbnRmKCIlZCVkJWRcbiIsYXJyW2ldLnUsYXJyW2ldLnYsYXJyW2ldLncpOwogICAKICAKCiAgIGludCB4LHk7CiAgIGZvcihpPTA7aTxlO2krKykKICAgewogICAgCiAgICAgICB4PWZpbmRzZXQoYXJyW2ldLnUpOwogICAgICAgeT1maW5kc2V0KGFycltpXS52KTsKICAgICAgIGlmKHghPXkpCiAgICAgICB7CiAgICAgICAgICAgaWYoYXJyW2ldLnU+YXJyW2ldLnYpCiAgICAgICAgCiAgICAgICB7ICBmb3IobD0wO2w8PWFycltpXS51O2wrKykKICAgICAgICAgeyBzdW0xW3hdLnB1c2hfYmFjaygxMDAwMDAwMDkpOwogICAgICAgICAgIHN1bTFbeV0ucHVzaF9iYWNrKDEwMDAwMDAwOSk7CiAgICAgICAgICAgc3VtMVthcnJbaV0udV0ucHVzaF9iYWNrKDEwMDAwMDAwOSk7CiAgICAgICAgICAgICBzdW0xW2FycltpXS52XS5wdXNoX2JhY2soMTAwMDAwMDA5KTsKICAgICAgICAgfQogICAgICAgfSAgIAogICAgICAgIGVsc2UgIAogICAgICAgIHsKICAgICAgICAgICAgICBmb3IobD0wO2w8PWFycltpXS52O2wrKykKICAgICAgICAgIHsgIAogICAgICAgICAgIHN1bTFbeF0ucHVzaF9iYWNrKDEwMDAwMDAwOSk7CiAgICAgICAgICAgc3VtMVt5XS5wdXNoX2JhY2soMTAwMDAwMDA5KTsgCiAgICAgICAgICAgc3VtMVthcnJbaV0udV0ucHVzaF9iYWNrKDEwMDAwMDAwOSk7CiAgICAgICAgICAgICBzdW0xW2FycltpXS52XS5wdXNoX2JhY2soMTAwMDAwMDA5KTsKICAgICAgICAgICB9CiAgICAgICAgICB9IAogICAgICAgICAgICB1bmlvbnNldCh4LHkpOwogICAgICAgIAogICAgICAgICAgc3VtMVt4XVt5XT1hcnJbaV0udzsKICAgICAgICAgIHN1bTFbeV1beF09YXJyW2ldLnc7CiAgICAgICAgIC8vICBwcmludGYoInggICAlZCAgeSAlZCAgICBzdW1beF1beV0gICAgJWRcbiIseCx5LHN1bTFbeF1beV0pOwogICAgIAogICAgICAgICAgaWYodmlzaXRbYXJyW2ldLnVdPT0wKQogICAgICAgICAgIGQucHVzaF9iYWNrKGFycltpXS51KTsKICAgICAgICAgICBpZih2aXNpdFthcnJbaV0udl09PTApCiAgICAgICAgICAgZC5wdXNoX2JhY2soYXJyW2ldLnYpOwogICAgICAgICAgIHZpc2l0W2FycltpXS51XT0xOwogICAgICAgICAgIHZpc2l0W2FycltpXS52XT0xOwogIAogICAgICAgICAKICAgICAgICAgICAgCiAgICAgICAgICAgICAKICAgICAgIH0gICAgIAogICAgICAgCiAgICAgfSAgICAgCi8vIGZvcihpPTA7aTxlO2krKylwcmludGYoImhpICAgJWRcbiIsZFtpXSkgOyAgIAogICAgIApmb3IoaT0wO2k8bjtpKyspCmRmcyhpLGkpOyAgIAogCiBpbnQgcm9vdD1maW5kc2V0KDApOyAgIAogICAgCgovL3ByaW50Zigicm9vdCAgICVkICAgXG4iLGZpbmRzZXQoMCkpOyAKLy9wcmludGYoInJvb3QgICAlZCAgIFxuIixmaW5kc2V0KDIpKTsKLy9wcmludGYoInJvb3QgICAlZCAgIFxuIixmaW5kc2V0KDMpKTsgIAoKCi8vcHJpbnRmKCJhc2RhcyAgJWRcbiIsc3VtMVswXVsyXSk7CiAgICAgICAKZm9yKGk9MDtpPG47aSsrKQp7Zm9yKGo9MDtqPG47aisrKQp7ICAgICAgIAppbnQgc3VtMj0xMDAwMDAwMTk7ICAgCmludCBzdW0zPTEwMDAwMDAxOTsKICAgaWYoaT09ailwcmludGYoIjAgIik7CiAgICAgICAgIAogICAgICAgZWxzZSBpZihzdW0xW2ldW2pdPT0xMDAwMDAwMDkpCiAgICAgICAgICB7IC8vcHJpbnRmKCJ2aWFrc2ggICAlZCAgJWRcbiIsaSxqKTsKICAgICAgICAgICAgICBpbnQgcj1pLHM9ajsKICAgICAgICAgICAgICAgICAgd2hpbGUociE9cFtyXSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZihzdW0yPnN1bTFbcl1bcFtyXV0pCiAgICAgICAgICAgICAgICAgICAgc3VtMj1zdW0xW3JdW3Bbcl1dOwogICAgICAgICAgICAgICAgICAvLyAgIHByaW50ZigibWF5YW5rICByICAgICVkICBwW3JdICAgJWQgICAgc3VtMVtyXVtwW3JdXSVkXG4iLHIscFtyXSxzdW0xW3JdW3Bbcl1dKTsKICAgICAgICAgICAgICAgICAgICByPXBbcl07ICAKICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgIHdoaWxlKHMhPXBbc10pCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYoc3VtMz5zdW0xW3Bbc11dW3NdKQogICAgICAgICAgICAgICAgICB7ICBzdW0zPXN1bTFbcFtzXV1bc107CiAgICAgICAgICAgICAgICAgIH0gIC8vcHJpbnRmKCJjaHVydSBwW3NdICAlZCAgcyAgJWQgc3VtICAgJWRcbiIscFtzXSxzLHN1bTFbcFtzXV1bc10pOwogICAgICAgICAgICAgICAgICAgIHM9cFtzXTsgIAogICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYoc3VtMj5zdW0zKQogICAgICAgICAgICAgICAgcHJpbnRmKCIlZCAiLHN1bTMpOwogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgcHJpbnRmKCIlZCAiLHN1bTIpOyAgCiAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgfQogICAgICAgIGVsc2UgICAgICAgICAgCiAgICAgIHByaW50ZigiJWQgIixzdW0xW2ldW2pdKTsgICAgCiAgICAgICAgICAgICAgCiAgICAgICAgIH0gIAogICAgICAgCiAgICAgICAgIAogICAgICAgICBwcmludGYoIlxuIik7CiB9ICAgIAovL3N5c3RlbSgicGF1c2UiKTsKICAgcmV0dXJuIDA7CiAgICAvLyBnZXRjaCgpOwogfSAgICAgICAgICAgIAo=