#!/usr/bin/perl -w

use v5.16;

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

my $step = 0;
while (++$step) {
	my (%f, @q);
	for my $x (0 .. 9) {
	for my $y (0 .. 9) {
		next if ++$m[$y][$x] < 10;
		$f{$x,$y} = 1;
		push @q, [$x, $y];
	} }
	while (my $p = shift @q) {
		my ($x, $y) = @$p;

		for my $dx (-1 .. 1) {
		for my $dy (-1 .. 1) {
			my ($x2, $y2) = ($x+$dx, $y+$dy);
			next if $x2 < 0 || $x2 > 9;
			next if $y2 < 0 || $y2 > 9;
			next if $f{$x2,$y2};
			next if ++$m[$y2][$x2] < 10;
			$f{$x2,$y2} = 1;
			push @q, [$x2, $y2];
		} }
	}
	for my $p (keys %f) {
		my ($x, $y) = split /$;/, $p;
		$m[$y][$x] = 0;
	}
	say "Step $step flashes ", scalar keys %f;
	say join("\n", map { join('', @$_) } @m);
	say '';
	last if keys %f == 100;
}

say $step;

