# make input into a huge string
$lines = join '', <>;

# change newlines to spaces
$lines =~ s/\n/ /g;

# strip leading whitespace
$lines =~ s/^\s+//;

# break the line into fields with whitespace as the delimiter
@pixels = split(/\s+/, $lines);

# print the first four fields verbatim
print shift(@pixels), "\n";
$cols = shift(@pixels);
$rows = shift(@pixels);
print $cols, ' ', $rows, "\n";
print shift(@pixels), "\n";

# flip the rows one at a time by unshifting row pixels into an
# array and then print them as a line using join
for ($i = 0; $i < $rows; $i++) {
  @row = ();
  for ($j = 0; $j < $cols; $j++) {
    unshift(@row, shift(@pixels));
  }
  $row = join(' ', @row);
  print $row . "\n";
}

