import sys

sys.setrecursionlimit(10000)

def find_basin(m, x, y, vis):
    # print(f"{x}, {y}")

    vis.append([x, y])

    s = 0

    if not (m[x - 1][y] == 9 or m[x - 1][y] == float('inf') or [x - 1, y] in vis):
        s += find_basin(m, x - 1, y, vis)

    if not (m[x + 1][y] == 9 or m[x + 1][y] == float('inf') or [x + 1, y] in vis):
        s += find_basin(m, x + 1, y, vis)

    if not (m[x][y - 1] == 9 or m[x][y - 1] == float('inf') or [x, y - 1] in vis):
        s += find_basin(m, x, y - 1, vis)

    if not (m[x][y + 1] == 9 or m[x][y + 1] == float('inf') or [x, y + 1] in vis):
        s += find_basin(m, x, y + 1, vis)
    
    return 1 + s

with open("input.txt") as f:
    m = [[float('inf')] + [int(n) for n in l.strip()] + [float('inf')] for l in f.readlines()]

    m = [[float('inf')] * len(m[0])] + m +  [[float('inf')] * len(m[0])]

    # print(m)

    lps = []
    bass = []

    for i in range(1, len(m) - 1):
        for j in range (1, len(m[0]) - 1):
            if m[i][j] < m[i - 1][j] and m[i][j] < m[i + 1][j] and m[i][j] < m[i][j - 1] and m[i][j] < m[i][j + 1]:
                print(f"{i}, {j}: {m[i][j]}")
                lps.append([i, j])

    for lp in lps:
        print(lp)
        bass.append(find_basin(m, *lp, []))

        bass = sorted(bass)

    print(bass)

    print(bass[-1] * bass[-2] * bass[-3])