#!/usr/bin/perl use v5.35.0; my @grid = map {chomp; [split //]} <>; $_ = ''; for my $r (1 .. $#grid-1) { for my $c (1 .. $#{$grid[0]}-1) { $_ .= $grid[$r-1][$c-1] . $grid[$r-1][$c+1] . $grid[$r][$c] . $grid[$r+1][$c-1] . $grid[$r+1][$c+1] . "\n"; } } say s/MMASS|MSAMS|SMASM|SSAMM//g;