#!/usr/bin/env python3 import sys from collections import defaultdict G = [] for l in sys.stdin: chars = [c for c in l.strip()] G.append(chars) def safeget(i, j): if i > 0 and j > 0 and i < len(G) and j < len(G[0]): return G[i][j] return None def getgear(i, j): locs = [ (i-1,j-1), (i-1,j), (i-1,j+1), (i,j-1), (i,j), (i,j+1), (i+1,j-1), (i+1,j), (i+1,j+1), ] for loc in locs: if safeget(loc[0], loc[1]) == '*': return loc return None gearnums = defaultdict(list) for i in range(len(G)): numstr = '' sym = None for j in range(len(G[0])): if G[i][j].isdigit(): numstr += G[i][j] if not sym: sym = getgear(i, j) else: if numstr and sym: gearnums[sym].append(int(numstr)) numstr = '' sym = None if numstr and sym: gearnums[sym].append(int(numstr)) tot = 0 for gear in gearnums: if len(gearnums[gear]) == 2: tot += gearnums[gear][0] * gearnums[gear][1] print(tot)