You are missing a $
You have
width0 = 0
But you want
$width0 = 0
This has the effect of never resetting $width0 back to zero, so only the first row is output. It thinks the inner loop never has to run again, since $width0
is still at its maximum value in every iteration after the first one.
(I should probably also add that globals are not the best idea, as others have pointed out, but you did ask for the reason why the script only output the first row. :) )