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>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;


int point_check(vvi &p) {
	int ret = 0;	
	rep(i,0,p.size()) {
		int old = p[i][0];
		int num = 1;
		vector<bool> kesu(5,false);;
		kesu[0] = true;
		rep(j,1,5) {
			if (p[i][j] == old) {
				num++;	
				kesu[j] = true;
				kesu[j-1] = true;
			}else {
				if(num >= 3) continue;
				old = p[i][j];
				num = 1;
				kesu[j-1] = false;
				if (j >= 2) {
					kesu[j-2] = false;
				}
			}
		}
		if(num >= 3) {
			ret += old * num;
			rep(k,0,5) {
				if(kesu[k]) {
					p[i][k] = 0;
				}
			}
		}
	}
	return ret;
}

void next(vvi &p) {
	rep(i,0,5) {
		for (int j = p.size() - 1; j >= 0; j--) {
			if (p[j][i] == 0) {
				for (int k = j; k >= 0;k--) {
					if (p[k][i] != 0) {
						int tmp = p[j][i];
						p[j][i] = p[k][i];
						p[k][i] = tmp;
						break;
					}
				}
			}
		}
	}				
	return ;	
}

void printv(vvi v){
	for(int i = 0; i < v.size(); i++){
	for(int j = 0; j < 5; j++){
		cout << v[i][j] << " ";
	}cout << endl;
	}cout << endl;
		return ;
		}

signed main() {

	while(1) {
		int ans = 0;
		cin1(n);
		if (n == 0) break;

		vvi puzzle(n,vi(5,0));
		rep(i,0,n) {
			rep(j,0,5) {
				cin1(puzzle[i][j]);	
			}
		}
		
		int old_ans = 0;
		while(1){
			ans += point_check(puzzle);
			if(old_ans == ans) break;
			old_ans = ans;
			next(puzzle);
		}
	
		cout(ans);
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
int h;
int ans;
int a[11][6];
bool fall_and_count()
{
	bool did=0;
	for(int i=1;i<=h;i++)
	{
		
		int val=-1,sum=0;
		int j;
		for(j=1;j<=5;j++)
		{
			if(a[i][j]!=a[i][j-1])
			{
				if(sum>=3 && val!=-1)
				{
					for(int k=j-1;k>=j-sum;k--) a[i][k]=-1;
					ans+=val*sum;
					did=1;
				}
				val=a[i][j];
				sum=1;
			} else sum++;
		}
		if(sum>=3 && val!=-1)
		{
			for(int k=5;k>=j-sum;k--) a[i][k]=-1;
			ans+=val*sum;
			did=1;
		}
	}
	return did;
}
void summarize()
{
	for(int i=1;i<=5;i++)
	{
		int pos=0;
		for(int j=1;j<=h;j++)
		{
			if(a[j][i]!=-1)
			{
				a[++pos][i]=a[j][i];
			}
		}
		for(int j=pos+1;j<=h;j++) a[j][i]=-1;
	}
}
int main()
{
	while(~scanf("%d",&h) && h)
	{
		ans=0;
		for(int i=h;i>=1;i--)
		{
			for(int j=1;j<=5;j++)
			{
				scanf("%d",&a[i][j]);
			}
		}
		while(fall_and_count())
		{	
			summarize();
			// for(int i=h;i>=1;i--)
			// {
			// 	for(int j=1;j<=5;j++)
			// 	{
			// 		printf("%d\t",a[i][j]);
			// 	}
			// 	printf("\n");
			// }
			// printf("\n\n");
		}
		printf("%d\n",ans);
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int field[5][10];
int H;

int erase(int h) {
	int cnt = 1,max_cnt = 0,n = -1;
	for(int i = 1;i < 5;i++) {
		if(field[i][h] == field[i - 1][h]) cnt++;
		else cnt = 1;
		if(cnt > max_cnt) {
			max_cnt = cnt;
			n = field[i][h];
		}
	}
	if(max_cnt >= 3) {
		for(int i = 0;i < 5;i++) {
			if(field[i][h] == n) field[i][h] = 0;
		}
		return max_cnt * n;
	} else return 0;
}

int main() {
	
	while(cin >> H && !(H == 0)) {
		for(int i = H - 1;i >= 0;i--) {
			cin >> field[0][i] >> field[1][i] >> field[2][i] >> field[3][i] >> field[4][i];
		}

		int score = 0,old_score = 0;
		do {
			old_score = score;
			for(int i = 0;i < H;i++) score += erase(i);

			int new_field[5][10];
			for(int i = 0;i < 5;i++) {
				for(int j = 0;j < H;j++) {
					new_field[i][j] = 0;
				}
			}
			for(int i = 0;i < 5;i++) {
				int j = 0,k = 0;
				for(int j = 0;j < H;j++) {
					if(field[i][j] != 0) new_field[i][k++] = field[i][j];
				}
			}
			
			for(int i = 0;i < 5;i++) {
				for(int j = 0;j < H;j++) {
					field[i][j] = new_field[i][j];
				}
			}
		} while(score != old_score);

		cout << score << endl;

	}
}