foreach my $x1 (0 .. 2) { next if $x - $x1 < 1; foreach my $y1 (0 .. 2) { next if $y - $y1 < 1; $total += $grid[$x - $x1][$y - $y1]; } };