when i'm not using the 'k' for loop the result is wrong.

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
//sum=0;
//for(k=0;k<=2;k++)
sum=sum+(a1[i][j])*(a2[j][j]);
res[i][j]=sum;
}
}


but when i'm doing this, the result is correct ...
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum=0;
for(k=0;k<=2;k++)
sum=sum+(a1[i][k])*(a2[k][j]);
res[i][j]=sum;
}
}

What is the difference in both the codes ????