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>

using namespace std;

int main() {

	while(1){
	int h;
	cin >> h;
	if (h == 0) { break; }
	vector<deque<int>> p(5, deque<int>());
	int score = 0;

	for (int i = 0; i < h; i++) {
		int in1;
		for (int j = 0; j < 5; j++) {
			cin >> in1;
			p[j].push_front(in1);
		}
	}

	while (1) {
		bool chgflg = false;

		for (int i = 0; i < h; i++) {

			int ernum, ernumcount;
			map<int, int> counter;

			//横に並んだ数字カウント
			for (int j = 0; j < 5; j++) {
				counter[p[j].at(i)]++;
			}
			//3つ以上を記憶する
			ernum = -1;
			for (int j = 1; j <= 9; j++) {
				if (counter[j] >= 3) {
					ernum = j;
					ernumcount = counter[j];
					break;
				}
			}
			if (ernum == -1) { continue; }

			//繋がってるか判定&ポイント追加
			if (ernumcount == 3) {
				for (int j = 0; j < 3; j++) {
					if (p[j].at(i) == ernum
						&& p[j + 1].at(i) == ernum
						&& p[j + 2].at(i) == ernum) {
						score += ernum * 3;
						p[j].at(i) = -1;
						p[j + 1].at(i) = -1;
						p[j + 2].at(i) = -1;
						chgflg = true;
						break;
					}
				}

			}
			else if (ernumcount == 4) {
				int chigauposi = -1;
				for (int j = 0; j < 5; j++) {
					if (p[j].at(i) != ernum) {
						chigauposi = j;
					}
				}

				if (chigauposi == 0) {
					chgflg = true;
					score += ernum * 4;
					p[1].at(i) = -1;
					p[2].at(i) = -1;
					p[3].at(i) = -1;
					p[4].at(i) = -1;
				}
				else if (chigauposi == 4) {
					chgflg = true;
					score += ernum * 4;
					p[0].at(i) = -1;
					p[1].at(i) = -1;
					p[2].at(i) = -1;
					p[3].at(i) = -1;
				}
				else if (chigauposi == 1) {
					chgflg = true;
					score += ernum * 3;
					p[2].at(i) = -1;
					p[3].at(i) = -1;
					p[4].at(i) = -1;
				}
				else if (chigauposi == 3) {
					chgflg = true;
					score += ernum * 3;
					p[0].at(i) = -1;
					p[1].at(i) = -1;
					p[2].at(i) = -1;
				}

			}
			else if (ernumcount == 5) {
				chgflg = true;
				score += ernum * 5;
				p[0].at(i) = -1;
				p[1].at(i) = -1;
				p[2].at(i) = -1;
				p[3].at(i) = -1;
				p[4].at(i) = -1;
			}

		}

		if (chgflg == false) { break; }

		//消去
		for (int i = h - 1; i >= 0; i--) {
			for (int j = 0; j < 5; j++) {
				if (p[j].at(i) == -1) {
					p[j].erase(p[j].begin() + i);
					p[j].push_back(0);
				}
			}
		}
	}

	cout << score << endl;
}
	return 0;

}

C++: 1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0

C++: #include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={-1,0,1,0,1,1,-1,-1};
const int INF = 1<<30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define rep(i,j) reps(i,0,j)
#define MOD 1000000007
typedef pair<int,int> Pii;
typedef pair<int,Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

vector < Pii > deleteList;
int H,W;
int stage[12][6];

bool check(){
	deleteList.clear();
	bool flg = false;
	rep(i,H){
		reps(j,1,W-1){
			if(stage[i][j-1] != 0 && stage[i][j-1] == stage[i][j] && stage[i][j] == stage[i][j+1]){
				deleteList.PB(Pii(i,j-1));
				flg = true;
			}
		}
	}
	return flg;
}

int deleteBlock(){
	int ret = 0;
	rep(i,deleteList.size()){
		reps(j,deleteList[i].se,deleteList[i].se+3){
			if(stage[deleteList[i].fi][j] > 0)ret += stage[deleteList[i].fi][j];
			stage[deleteList[i].fi][j] = -1;
		}
	}
	return ret;
}

void dropBlock(){
	rep(i,H){
		rep(j,W){
			if(stage[i][j] == -1){
				while(stage[i][j] == -1){
					reps(k,i+1,H){
						stage[k-1][j] = stage[k][j];
					}
					stage[H-1][j] = 0;
				}
			}
		}
	}
	return ;
}

void DEBUGPRINT(){
	
	rep(i,H){
		rep(j,W){
			printf("%2d%c",stage[i][j],j==W-1?'\n':' ');
		}
	}
	puts("######################");
}

int main(){
	W = 5;
	while(scanf("%d",&H),H){
		memset(stage,-1,sizeof(stage));
		rep(i,H){
			rep(j,W){
				scanf("%d",&stage[H-i-1][j]);
			}
		}
		int score = 0;
		while(check()){
			//DEBUGPRINT();
			score += deleteBlock();
			dropBlock();
		}
		
		printf("%d\n",score);
	}
	return 0;
}