($H, $W) = split / /, <>; @masu;
for (0..$H-1) {
    push @{ $masu[$_] }, split //, <>;
}

use Data::Dumper;
warn Dumper \@masu;

for my $h (0..$H-1) {
    for my $w (0..$W-1) {
        next if $masu[$h][$w] eq '#';

        my $bakudan = 0;
        for my $diffh (qw/-1 0 1/) {
            my $dh = $h + $diffh;
            print "h: $dh ";
            next if ($dh < 0 || $dh > $#masu);

            my @masuh = $masu[$dh];
            print "masuh: $#masuh";
            for my $diffw (qw/-1 0 1/) {
                my $dw = $w + $diffw;
                print "w: $dw ";
                next if ($dw < 0 || $dw > $#masuh);
                print "masu: $masu[$dh][$dw] ";
                $bakudan++ if $masu[$dh][$dw] eq '#';
                print "bakudan: $bakudan ";
            }
            print "\n";
        }

        $masu[$h][$w] = $bakudan;
    }
}

for (0..$#masu) {
    for my $c (@{ $masu[$_] }) {
        print $c;
    }

    print "\n";
}

=pod
h-1, w-1
h-1, w
h-1, w+1
h,   w-1
h,   w
h,   w+1
h+1, w-1
h+1, w
h+1, w+1
=cut
