Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		h := io.NextInt()
		if h == 0 {
			break
		}
		p := make([][]int, h)
		for i := 0; i < h; i++ {
			p[i] = make([]int, 5)
			for j := 0; j < 5; j++ {
				p[i][j] = io.NextInt()
			}
		}
		ans := 0
		for {
			sc := puyo(p, h)
			if sc == 0 {
				break
			}
			fall(p, h)
			ans += sc
		}
		io.Println(ans)
	}
}

func puyo(p [][]int, h int) int {
	sc := 0
	for i := 0; i < h; i++ {
		for j := 0; j < 3; j++ {
			var cnt int
			for cnt = 1; j+cnt < 5; cnt++ {
				if p[i][j] != p[i][j+cnt] {
					break
				}
			}
			if cnt > 2 {
				sc += p[i][j]*cnt
				for k := 0; k < cnt; k++ {
					p[i][j+k] = 0
				}
			}
		}
	}
	return sc
}

func fall(p [][]int, h int) {
	for j := 0; j < 5; j++ {
		for k := 1; k < h; k++ {
			for i := h - 1; i >= k; i-- {
				if p[i][j] == 0 {
					p[i][j] = p[i-1][j]
					p[i-1][j] = 0
				}
			}
		}
	}
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <bits/stdc++.h>

#ifdef DEBUG
#define debug(x) cout << #x << ": " << x << endl
#else
#define debug(x)
#endif

#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPS(i, n) for(int i = 1; i <= (n); i++)
#define ALL(v) (v).begin(), (v).end()

#define pb push_back
#define mp make_pair
#define mt make_tuple

using namespace std;

using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;

const ll INF = 1e18;
const ll MOD = 1e9+7;

template<class T> inline bool chmax(T &a, const T &b) { if (a<b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, const T &b) { if (a>b) { a = b; return true;} return false; }


signed main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    const int W = 5;

    while(true)
    {
        int H; cin >> H;
        if(H == 0) break;
        vector<vi> block(H, vi(W)); REP(i, H) REP(j, W) cin >> block[i][j];

        int score = 0;
        bool update = true;

        while(update)
        {
            update = false;
            // blockを消す
            REP(i, H)
            {
                int count = 0, mc = 0, index = 0;
                REP(j, W)
                {
                    if(j > 0 && block[i][j] != block[i][j-1]) count = 0;
                    if(block[i][j] != -1) count++;
                    if(chmax(mc, count)) index = j;
                }
                if(mc >= 3)
                {
                    update = true;
                    score += mc * block[i][index];
                    for(int j = index-mc+1; j <= index; j++)
                    {
                        block[i][j] = -1;
                    }
                }
            }

            // blockを移動
            REP(j, W)
            {
                int k = H-1;
                while(k >= 0 && block[k][j] > 0) k--;
                if(k > 0)
                {
                    for(int n = k-1; n >= 0; n--)
                    {
                        if(block[n][j] > 0)
                        {
                            swap(block[k][j], block[n][j]);
                            k--;
                        }
                        if(k < 0) break;
                    }
                }
            }

            /*
            cout << endl;
            REP(i, H)
            {
                REP(j, W) cout << block[i][j] << " ";
                cout << endl;
            }
            cout << endl;
            */
        }
        cout << score << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAX_H=21;

int H,ans,ok,rev;
vector<vector<int>> stones(MAX_H,vector<int>(5));
vector<int> seq(5);

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
	while(cin >> H,H){
		for (int i=0;i<H;++i)
			for (int j=0;j<5;++j)
				cin >> stones[H-1-i][j];
		ans=0;
		while(1){
			ok=0;
			for (int i=0;i<H;++i){
				for (int j=0;j<5;++j) seq[j]=0;
				seq[2]=(stones[i][2]>0); rev=0;
				for (int j=1;j<=2;++j){
					if (seq[1+j]&&stones[i][2+j]==stones[i][2]) seq[2+j]=1;
					if (seq[3-j]&&stones[i][2-j]==stones[i][2]) seq[2-j]=1;
				}
				for (int j=0;j<5;++j) rev+=seq[j];
				if (rev>=3){
					ok=1; ans+=rev*stones[i][2];
					for (int j=0;j<5;++j){
						if (seq[j]) stones[i][j]=0;
					}
				}
			}
			for (int i=0;i<H;++i){
				for (int j=0;j<5;++j){
					if (!stones[i][j]){
						for (int k=i+1;k<H;++k){
							if (stones[k][j]){
								stones[i][j]=stones[k][j];
								stones[k][j]=0;
								break;
							}
						}
					}
				}
			}
			if (!ok) break;
		}
		cout << ans << '\n';
	}
}