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 <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);

	while(1) {

		int h, field[5][10];	//フィールドの高さ、フィールド
		int fall[5][10];
		int score = 0;			//スコア
		bool flg = true;		//消去可能フラグ

		cin >> h;
		if( h == 0 ) break;

		//データ取得
		for(int i = 0; i < h; i++)
			for(int j = 0; j < 5; j++)
				cin >> field[j][i];

		while( flg ) {
			flg = false;

			//一度に一行で二種類以上の数字が同時には消えないことを利用する
			for(int i = 0; i < h; i++) {
				int cntnum = 0, cnt = 0;
				int max = 0, st = 0;
				for(int j = 0; j < 5; j++) {

					if( field[j][i] > 0 && cntnum != field[j][i] ) {
						cnt = 1;
						cntnum = field[j][i];
					} else if( field[j][i] > 0 ){
						cnt ++;
						if( cnt > max ) {
							max = cnt;
							st = j - cnt + 1;
						}
					}

				}

				//消去
				if( max >= 3 ) {
					score += max * field[st][i];
					for(int j = st; j < st + max; j++)
						field[j][i] = 0;
					flg = true;
				}
			}

			//落下
			for(int i = 0; i < 5; i++) {
				int cnt = h - 1;
				for(int j = h - 1; j >= 0; j-- ) {
					if( field[i][j] > 0 ) {
						fall[i][cnt] = field[i][j]; 
						cnt --;
					}
				}
				for(int j = cnt; j >= 0; j--) {
					fall[i][j] = -1;
				}
			}

			for(int i = 0; i < 5; i++)
				for(int j = 0; j < h; j++)
					field[i][j] = fall[i][j];

		}
		
		cout << score << endl;
		

	}

	//while(1){}
    return 0;
}

C++: #include <cstdio>
using namespace std;

int main(){
	
	int h,tmp,filled,combo;
	int last_score, score;
	int a[10][5];
	
	while(1){
		score = 0;
		last_score = -1;
		scanf(" %d", &h);
		if(h == 0){
			break;
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<5;j++){
				scanf(" %d", (a[i]+j));
			}
		}
		
		
		while(last_score != score){	//while2 begin
			last_score = score;

			//erasing and scoring
			for(int i=0;i<h;i++){
				combo = 0;
				for(int j=1;j<5;j++){
					if(a[i][j] == a[i][j-1]){
						combo ++;
					}else{
						if(combo >= 2){
							for(int k=combo;k>=0;k--){
								score += a[i][j-1-k];
								a[i][j-1-k] = 0;
							}
						}
						combo = 0;
					}
				}
				//not DRY...
				if(combo >= 2){
					for(int k=combo;k>=0;k--){
						score += a[i][4-k];
						a[i][4-k] = 0;
					}
				}
			}
			
			//dropping
			for(int j=0;j<5;j++){
				filled = 0;
				for(int i=h-1;i>=0;i--){
					if(a[i][j] != 0){
						tmp = a[i][j];
						a[i][j] = 0;
						a[h-1 - filled][j] = tmp;

						filled ++;
					}
				}
			}
		//debug
/*		printf("score:%d\n",score);
		for(int i=0;i<h;i++){
			for(int j=0;j<5;j++){
				printf("%d ",a[i][j]);
			}
			printf("\n");
		}
*/			
		}		//while2 end	
		printf("%d\n",score);
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 1145141919
#define MOD 1000000007
#define ALL(a) begin((a)),end((a))
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main()
{
	int h;
	while (cin >> h, h)
	{
		int tmp[11][6];
		REP(i, h + 1)REP(j, 6) tmp[i][j] = 0;
		REP(i, h)REP(j, 5) cin >> tmp[i][j];
		int sum = 0;
		while (1)
		{
			int res = 0;
			REP(i, h)
			{
				if (count(tmp[i],tmp[i]+5, tmp[i][2]) == 3)
				{
					if ((tmp[i][0] == tmp[i][1] && tmp[i][1] == tmp[i][2]) || (tmp[i][1] == tmp[i][2] && tmp[i][2] == tmp[i][3]) || (tmp[i][2] == tmp[i][3] && tmp[i][3] == tmp[i][4]))
					{
						int k = tmp[i][2];
						REP(j, 5)
						{
							if (tmp[i][j] == k) tmp[i][j] = 0;
						}
						res += 3 * k;
					}
				}
				else if (count(tmp[i], tmp[i] + 5, tmp[i][2]) == 4)
				{
					int k = tmp[i][2];
					if (tmp[i][1] != tmp[i][3])
					{
						if (tmp[i][1] != k) FOR(j, 2, 5) tmp[i][j] = 0;
						else REP(j, 3) tmp[i][j] = 0;
						res += k * 3;
					}
					else
					{
						REP(j, 5) if (tmp[i][j] == k) tmp[i][j] = 0;
						res += k * 4;
					}
				}
				else if (count(tmp[i], tmp[i] + 5, tmp[i][2]) == 5)
				{
					int k = tmp[i][2];
					REP(j, 5) tmp[i][j] = 0;
					res += k * 5;
				}
			}

			if (res == 0) break;
			sum += res;
			REP(s, 11)
			{
				for (int i = h - 1; i > 0; i--)REP(j, 5)
				{
					if (tmp[i][j] == 0)
					{
						tmp[i][j] = tmp[i - 1][j];
						tmp[i - 1][j] = 0;
					}
				}
			}

		}
		cout << sum << endl;

	}
	return 0;
}