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;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
//int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int h;
int a[11][5];
int d[11][5];

int main(){
	while(1){
		cin>>h;
		if(h==0)break;
		REP(i,11)
			REP(j,5)
			a[i][j]=-1;
		for(int i=h-1;i>=0;i--){
			for(int j=0;j<5;j++){
				cin>>a[i][j];
			}
		}
		int ans=0;
		int flag=1;
		for(int qq=0;qq<200;qq++){
			
			for(int i=0;i<h;i++){
				if(a[i][0]!=-1&&a[i][0]==a[i][1]&&a[i][1]==a[i][2]&&a[i][2]==a[i][3]&&a[i][3]==a[i][4]){
					ans+=a[i][0]*5;
					for(int j=0;j<5;j++)
						a[i][j]=-1;
				}else if(a[i][0]!=-1&&a[i][0]==a[i][1]&&a[i][1]==a[i][2]&&a[i][2]==a[i][3]){
					ans+=a[i][0]*4;
					for(int j=0;j<4;j++)
						a[i][j]=-1;
				}else if(a[i][1]!=-1&&a[i][1]==a[i][2]&&a[i][2]==a[i][3]&&a[i][3]==a[i][4]){
					ans+=a[i][1]*4;
					for(int j=1;j<5;j++)
						a[i][j]=-1;
				}else if(a[i][0]!=-1&&a[i][0]==a[i][1]&&a[i][1]==a[i][2]){
					ans+=a[i][0]*3;
					for(int j=0;j<3;j++)
						a[i][j]=-1;
				}else if(a[i][1]!=-1&&a[i][1]==a[i][2]&&a[i][2]==a[i][3]){
					ans+=a[i][1]*3;
					for(int j=1;j<4;j++)
						a[i][j]=-1;
				}else if(a[i][2]!=-1&&a[i][2]==a[i][3]&&a[i][3]==a[i][4]){
					ans+=a[i][2]*3;
					for(int j=2;j<5;j++)
						a[i][j]=-1;
				}
			}
			
			for(int i=0;i<h;i++){
				for(int j=0;j<h-1;j++){
					for(int k=0;k<5;k++){
						if(a[j][k]==-1){
							swap(a[j][k],a[j+1][k]);
						}
					}
				}
			}
			
			/*if(qq==0){
				cout<<endl;
				for(int i=h-1;i>=0;i--){
					for(int j=0;j<5;j++){
						cout<<a[i][j]<<" ";
					}
					cout<<endl;
				}
			}*/
			
			
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

#define REP(i,n) for(int i=(0); i<n; ++i)

double eps = 1e-10;
const int INF = 1e+9;
typedef pair<int, int> P;
typedef pair<int, P> pp;
const int MAX_N = 105000;

int H;
int maze[16][8];

int search(int x, int y, int c){
	int cnt = 0;
	while(1){
		if(x >= 5 || maze[y][x] != c)
			break;
		x += 1;
		cnt++;
	}
	if(cnt >= 3)
		return cnt;
	else
		return 0;
}

int main() {
	while(cin >> H && H){
		for(int i = 0; i < 16; ++i)
			for(int j = 0; j < 5; ++j)
				maze[i][j] = 0;
		for(int i = 0; i < H; ++i)
			for(int j = 0; j < 5; ++j)
				scanf("%d", &maze[i][j]);
				
		int ans = 0;
		vector<pp> vec;
		while(1){
			for(int y = 0; y < H; ++y){
				int cnt;
				for(int x = 0; x < 3; ++x){
					if(maze[y][x] == 0)	
						continue;
					if(cnt = search(x, y, maze[y][x])){
						ans += maze[y][x] * cnt;
						for(int i = x; i < x + cnt; ++i){
							maze[y][i] = 0;
						}
						vec.push_back(pp(y, P(x, x + cnt)));
						break;
					}
				}
			}
			if(vec.empty())
				break;
			sort(vec.begin(), vec.end());
			for(int i = 0; i < vec.size(); ++i){
				for(int y = vec[i].first; y > 0; --y){
					for(int x = vec[i].second.first; x < vec[i].second.second; ++x){
						maze[y][x] = maze[y - 1][x];
					}
				}
			}
			vec.clear();
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

int main(){
while(true){
 int H;
 cin>>H;

 if(H==0)break;

 int S[10][5];
 for(int y=0;y<H;y++)
	for(int x=0;x<5;x++)
		cin>>S[y][x];

 int sco=0;
 
 while(true){
	bool flg=false;

	for(int y=0;y<H;y++){
		int m =S[y][2];//真ん中覚える
		if(m==0)
			continue;

		if(S[y][0]==m && S[y][1]==m){//012で消える
			sco += m*3;
			S[y][0]=S[y][1]=S[y][2]=0;
			flg==true;
			if(S[y][3]==m){
				sco+=m;
				S[y][3]=0;
				if(S[y][4]==m){
					sco+=m;
					S[y][4]=0;
				}
			}
		}
		else if(S[y][1]==m&&S[y][3]==m){//1~3
			sco+=m*3;
			S[y][1]=S[y][2]=S[y][3]=0;
			flg=true;
			if(S[y][4]==m){
				sco+=m;
				S[y][4]=0;
			}
		}
		else if(S[y][3]==m&&S[y][4]==m){//2~4
			sco+=m*3;
			S[y][2]=S[y][3]=S[y][4]=0;
			flg=true;
		}
	}
	if(!flg)break;

	for(int x=0;x<5;x++){
		for(int y=H-1;y>=0;y--){
			if(S[y][x]!=0)
				continue;

			for(int y2=y-1;y2>=0;y2--){
				if(S[y2][x]!=0){
					S[y][x]=S[y2][x];
					S[y2][x]=0;
					break;
				}
			}
		}
	}
 }
 cout<<sco<<endl;
}
return 0;
}