#! /usr/bin/perl
# fb.pl
# Solve fencebuilder problem.
# Start: Sat Sep  6 14:40:33 EDT 1997

$state=0 ; # 0=new prog;

%struts = ();
# list of coords of all lower-left hand square metres that are fenced in.



sub min { # 2 arg min
	local($a,$b)=@_;
	$a < $b ? $a : $b;
}

sub init {
	$x=0;
	$y=0;
	%struts=();
}

sub mark {
	local($a,$b,$c,$d)=@_;
	local($mx)=$a; # == $c;
	local($my)=&min($b,$d);
	if ( length($struts{$my}) <= 0 ) { $struts{$my} = $mx; }
	else { $struts{$my} .= ";$mx"; }
}

sub bynumber { $a <=> $b; }
sub answer {
	local($y,@xl,$total,$p, @xln);
	$total = 0;
	foreach $i ( sort keys(%struts) ) {
		$p = $struts{$i};
# print "$i: $p\n";
	
		@xl=sort bynumber split(/;/,$p);
		while ( $#xl > 0 ) {
			local($l,$r);
			$l = shift(@xl);
			$r = shift(@xl);
			$total += $r - $l;
		}
	}
	print "$total\n";
}

sub n { &mark($x,$y,$x,$y+1); $y++; }
sub s { &mark($x,$y,$x,$y-1); $y--; }
sub e {                       $x++; }
sub w {                       $x--; }

&init;
while($line = <>) {
	if ( $line=~ m/^\./ ) {
		&answer;
		&init;
	} elsif ( $line=~ m/^n/ ) { &n; }
	  elsif ( $line=~ m/^e/ ) { &e; }
	  elsif ( $line=~ m/^w/ ) { &w; }
	  elsif ( $line=~ m/^s/ ) { &s; }
}
