#!/usr/bin/perl use v5.35.0; my (@horz, @vert, @diag1, @diag2); for (my $row = 0; <>; ++$row) { chomp; $horz[$row] = $_; my $col = 0; for my $ch (split //) { $vert[$col] .= $ch; $diag1[$row + $col] .= $ch; $diag2[$row + (length) - ++$col] .= $ch; } } $_ = join "\n", @horz, @vert, @diag1, @diag2; say s/XMAS/XS/g + s/SAMX//g;