#!/usr/bin/perl -w

use v5.38;
use experimental 'multidimensional';
$; = ';';

my @map = map { chomp; [ split // ] } <>;
my $xmax = $#{ $map[0] };
my $ymax = $#map;

sub get($x, $y) {
	return $x < 0 || $x > $xmax || $y < 0 || $y > $ymax
		? '.' : $map[$y][$x];
}

my $sum;
my %gear;

for my $y (0 .. $ymax) {
	my $x = 0;
	while ($x <= $xmax) {
		my $n;
		if (($n = get($x, $y)) !~ /\d/) {
			$x++; next;
		}
		my $dx;
		while ((my $n1 = get($x + ++$dx, $y)) =~ /\d/) {
			$n .= $n1;
		}
		for my $x1 ($x-1 .. $x+$dx) {
			for my $y1 ($y-1 .. $y+1) {
				next if (my $s = get($x1, $y1)) ne '*';
				push @{ $gear{$x1,$y1} }, $n;
				# say "star at $x1,$y1 for $n at $x+$dx,$y";
			}
		}
		$x += $dx;
	}
}

for my $l (values %gear) {
	next if @$l != 2;
	$sum += $l->[0] * $l->[1];
}

say $sum;
