#!/usr/bin/perl -w

use strict;

my %tiles;

my ($min_x, $max_x, $min_y, $max_y);

while (<>) {
	chomp;
	my ($x, $y) = (0, 0);
	while (length) {
		s/\A(se|ne|sw|nw|e|w)//;
		if ($1 eq 'ne') {
			$y++;
		} elsif ($1 eq 'e') {
			$x++;
		} elsif ($1 eq 'se') {
			$x++; $y--;
		} elsif ($1 eq 'sw') {
			$y--;
		} elsif ($1 eq 'w') {
			$x--;
		} elsif ($1 eq 'nw') {
			$x--; $y++;
		}
	}
	$tiles{"$x|$y"} ^= 1;
	$min_x = $x if !defined $min_x || $min_x > $x;
	$max_x = $x if !defined $max_x || $max_x < $x;
	$min_y = $y if !defined $min_y || $min_y > $y;
	$max_y = $y if !defined $max_y || $max_y < $y;
}


for (1 .. 100) {
	$min_x--; $min_y--; $max_x++, $max_y++;
	my %newtiles;
	for my $x ($min_x .. $max_x) {
	for my $y ($min_y .. $max_y) {
		my $count = 0;
		$count++ if $tiles{($x+1).'|'.($y)}; # E
		$count++ if $tiles{($x).'|'.($y+1)}; # NE
		$count++ if $tiles{($x+1).'|'.($y-1)}; # SE
		$count++ if $tiles{($x).'|'.($y-1)}; # SW
		$count++ if $tiles{($x-1).'|'.($y)}; # W
		$count++ if $tiles{($x-1).'|'.($y+1)}; # NW
		if ($tiles{"$x|$y"} && ($count == 0 || $count > 2)) {
			$newtiles{"$x|$y"} = 0;
		} elsif (!$tiles{"$x|$y"} && $count == 2) {
			$newtiles{"$x|$y"} = 1;
		} else {
			$newtiles{"$x|$y"} = $tiles{"$x|$y"};
		}
	}
	}
	%tiles = %newtiles;
	print "Tiles=" . (grep { $tiles{$_} } keys %tiles)."\n";
}

