lines = [ [int(j) for j in list(i)] for i in open("input.txt", 'r').read().splitlines()] summa = 0 lowList = [] for x in range(len(lines)): for y in range(len(lines[x])): v = lines[x][y] l = 9 if x == 0 else lines[x-1][y] r = 9 if x == len(lines)-1 else lines[x+1][y] u = 9 if y == 0 else lines[x][y-1] d = 9 if y == len(lines[x])-1 else lines[x][y+1] if v < l and v < r and v < u and v < d: summa += 1 + v lowList.append((x,y)) for p in lowList: size = 0 print(summa) explored = [] def explore(x,y): if lines[x][y] == 9 or (x,y) in explored: return 0 explored.append((x,y)) size = 1 if y != 0: size += explore(x,y-1) if y != len(lines[x]) - 1: size += explore(x,y+1) if x != 0: size += explore(x-1,y) if x != len(lines) - 1: size += explore(x+1,y) return size sizes = [0]*len(lowList) for i in range(len(lowList)): sizes[i] = explore(lowList[i][0],lowList[i][1]) sizes.sort() print(sizes[-1] * sizes[-2] * sizes[-3])