from functools import lru_cache





def DEBUG(*args): pass # print('@', *args)





@lru_cache(maxsize=None)

def pat(s, n):

    return ' '.join([s] * n)





DEBUG(pat('1', 3))





def removeAll(xs, s):

    while s in xs:

        xs.remove(s)

        xs.append('#')

    return xs





def lmap(f, s): return list(map(f, s))





digits = lmap(str, list(range(1, 10)))



def main(n):

    data = '\n'.join([eval(input()) for x in range(n)])

    score = 0

    DEBUG(data)

    while True:

        removed = False

        sum1 = sum([0 if x == '#' else int(x) for x in data.split()])

        for d in digits:

            if pat(d, 3) in data:

                data = data.replace(pat(d, 5), pat('0', 5))

                data = data.replace(pat(d, 4), pat('0', 4))

                data = data.replace(pat(d, 3), pat('0', 3))

                removed = True

        if removed == False:

            break

        DEBUG(data)

        score += (sum1 - sum([0 if x == '#' else int(x) for x in data.split()]))

        data = list(zip(*[x.split() for x in data.split('\n')]))

        data = [list(x[::-1]) for x in data]

        data = [removeAll(x, '0') for x in data]

        data = lmap(lambda s: ' '.join(s), list(zip(*data)))

        data = '\n'.join(data[::-1])

        DEBUG(data)

    DEBUG(data)

    print(score)



while True:

    n = int(eval(input()))

    if n == 0: break

    main(n)

from functools import lru_cache



digits = list([str(x) for x in range(1,10)])



@lru_cache(maxsize=None) 

def check(s,n):  

    return ' '.join([s]*n)



def down(x):

    x = list(x[::-1])   

    while '0' in x:

        x.remove('0')

        x.append('#')

    return x



def main(n):

    data = '\n'.join([eval(input()) for x in range(n)])  

    score = 0

    while True:

        removed = False

        for d in digits:

            if check(d, 3) in data:

                for i in range(5, 2, -1):

                    if check(d, i) in data:

                        score += int(d) * (data.count(check(d, i))*i)

                        data = data.replace(check(d, i), check('0', i))

                        removed = True

        if removed == False:

            break

        data = list(zip(*[x.split() for x in data.split('\n')]))

        data = [down(x) for x in data]

        data = list([' '.join(x) for x in zip(*data)])

        data = '\n'.join(data[::-1])

    print(score) 





while True:

    n = int(eval(input()))

    if n == 0: break

    main(n)



from functools import lru_cache


def DEBUG(*args):
    pass  # print('@', *args)


@lru_cache(maxsize=None)
def pat(s, n):
    return " ".join([s] * n)


DEBUG(pat("1", 3))


def removeAll(xs, s):
    while s in xs:
        xs.remove(s)
        xs.append("#")
    return xs


def lmap(f, s):
    return list(map(f, s))


digits = lmap(str, list(range(1, 10)))


def main(n):
    data = "\n".join([eval(input()) for x in range(n)])
    score = 0
    DEBUG(data)
    while True:
        removed = False
        sum1 = sum([0 if x == "#" else int(x) for x in data.split()])
        for d in digits:
            if pat(d, 3) in data:
                data = data.replace(pat(d, 5), pat("0", 5))
                data = data.replace(pat(d, 4), pat("0", 4))
                data = data.replace(pat(d, 3), pat("0", 3))
                removed = True
        if removed == False:
            break
        DEBUG(data)
        score += sum1 - sum([0 if x == "#" else int(x) for x in data.split()])
        data = list(zip(*[x.split() for x in data.split("\n")]))
        data = [list(x[::-1]) for x in data]
        data = [removeAll(x, "0") for x in data]
        data = lmap(lambda s: " ".join(s), list(zip(*data)))
        data = "\n".join(data[::-1])
        DEBUG(data)
    DEBUG(data)
    print(score)


while True:
    n = int(eval(input()))
    if n == 0:
        break
    main(n)

from functools import lru_cache

digits = list([str(x) for x in range(1, 10)])


@lru_cache(maxsize=None)
def check(s, n):
    return " ".join([s] * n)


def down(x):
    x = list(x[::-1])
    while "0" in x:
        x.remove("0")
        x.append("#")
    return x


def main(n):
    data = "\n".join([eval(input()) for x in range(n)])
    score = 0
    while True:
        removed = False
        for d in digits:
            if check(d, 3) in data:
                for i in range(5, 2, -1):
                    if check(d, i) in data:
                        score += int(d) * (data.count(check(d, i)) * i)
                        data = data.replace(check(d, i), check("0", i))
                        removed = True
        if removed == False:
            break
        data = list(zip(*[x.split() for x in data.split("\n")]))
        data = [down(x) for x in data]
        data = list([" ".join(x) for x in zip(*data)])
        data = "\n".join(data[::-1])
    print(score)


while True:
    n = int(eval(input()))
    if n == 0:
        break
    main(n)

- -def DEBUG(*args): -    pass  # print('@', *args) +digits = list([str(x) for x in range(1, 10)]) -def pat(s, n): +def check(s, n): -DEBUG(pat("1", 3)) - - -def removeAll(xs, s): -    while s in xs: -        xs.remove(s) -        xs.append("#") -    return xs - - -def lmap(f, s): -    return list(map(f, s)) - - -digits = lmap(str, list(range(1, 10))) +def down(x): +    x = list(x[::-1]) +    while "0" in x: +        x.remove("0") +        x.append("#") +    return x -    DEBUG(data) -        sum1 = sum([0 if x == "#" else int(x) for x in data.split()]) -            if pat(d, 3) in data: -                data = data.replace(pat(d, 5), pat("0", 5)) -                data = data.replace(pat(d, 4), pat("0", 4)) -                data = data.replace(pat(d, 3), pat("0", 3)) -                removed = True +            if check(d, 3) in data: +                for i in range(5, 2, -1): +                    if check(d, i) in data: +                        score += int(d) * (data.count(check(d, i)) * i) +                        data = data.replace(check(d, i), check("0", i)) +                        removed = True -        DEBUG(data) -        score += sum1 - sum([0 if x == "#" else int(x) for x in data.split()]) -        data = [list(x[::-1]) for x in data] -        data = [removeAll(x, "0") for x in data] -        data = lmap(lambda s: " ".join(s), list(zip(*data))) +        data = [down(x) for x in data] +        data = list([" ".join(x) for x in zip(*data)]) -        DEBUG(data) -    DEBUG(data)