#!/usr/bin/perl -w

use v5.16;
use strict;

my @m = map { chomp; [ split // ] } <>;

for (1 .. 100) {
	my @nm;
	for my $y (0 .. 99) {
	for my $x (0 .. 99) {
		my $sum = 0;
		for my $dy (-1 .. 1) {
			next if $y + $dy < 0 || $y + $dy > 99;
			for my $dx (-1 .. 1) {
				next if $x + $dx < 0 || $x + $dx > 99;
				next if $dx == 0 && $dy == 0;
				$sum++ if $m[$y+$dy][$x+$dx] eq '#';
			}
		}
		if ($m[$y][$x] eq '#') {
			$nm[$y][$x] = $sum == 2 || $sum == 3 ? '#' : '.';
		} else {
			$nm[$y][$x] = $sum == 3 ? '#' : '.';
		}
	} }
	@m = @nm;
}

my $sum;
for my $y (0 .. 99) {
for my $x (0 .. 99) {
	$sum++ if $m[$y][$x] eq '#';
} }

say $sum;
