The problem ... 

# for(my $up = 1; $up <= $height + 1; $up++ ) {
# for(my $y = 1; $y <= $height; $y++ ) {
foreach my $y (1..$height) {