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 <algorithm>
#include <queue>
#include <cstring>

using namespace std;

int h;
int map[10][5];
bool checked[10][5];

int check(int x,int y,int num,int count){
  if(x > 4)
    return count;
  
  if(num != map[y][x])
    return count;

  if(count > 5)
    return count;

  if(num == 0)
    return count;
  
  checked[y][x] = true;
  return check(x+1,y,map[y][x],count+1);
}

int main(){

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

    memset(map,0,sizeof(map));
    memset(checked,false,sizeof(checked));

    
    for(int i = 10-h;i<10;i++){
      for(int j = 0;j<5;j++){
	cin>>map[i][j];
      }
    }

    int point = 0;
    bool update = true;
    /*
    for(int i = 0;i<10;i++){
      for(int j = 0;j<5;j++){
	cout<<map[i][j]<<"\t";
      }
      cout<<endl;
    }
    */
    while(update){
      update = false;
      memset(checked,false,sizeof(checked));
      for(int i = 10-h;i < 10;i++){
	for(int j = 0;j<5;j++){
	  if(checked[i][j]||map[i][j]==0)
	    continue;
	  int tmp =  check(j,i,map[i][j],1) - 1;
	  point += (tmp >= 3)?tmp * map[i][j]: 0;
	  if(tmp >= 3){
	    update = true;
	    for(int k = j;k<j+tmp;k++){
	      for(int l = i;l > 0;l--){
		map[l][k] = map[l-1][k];
	      }
	      
	    }
	    
	  }
	  
	}
      }
      /*
      cout<<"--------------------------"<<endl;

    for(int i = 0;i<10;i++){
      for(int j = 0;j<5;j++){
	cout<<map[i][j]<<"\t";
      }
      cout<<endl;
      }
*/
      
    }
    cout<<point<<endl;
  }
    
	
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000

int mat[10][5];

int main(){
  int h;
  while(scanf("%d", &h), h){
    rep(i,h)
      rep(j,5)
      scanf("%d", &mat[i][j]);

    int res=0;
    while(true){
      // remove stones
      bool changed=false;
      rep(i,h){
	int base=mat[i][2];
	if(base<=0) continue;
	int idx=0;
	while(idx<5){
	  if(mat[i][idx]!=base){idx++; continue;}
	  int cnt=0;
	  while(idx<5 && mat[i][idx]==base){ idx++; cnt++; }
	  if(cnt>=3){
	    changed=true;
	    res += base*cnt;
	    rep(k,cnt) mat[i][idx-k-1]=0;
	    break;
	  }
	}
      }

      if(!changed) break;

      // drop stones
      rep(i,5){
	int height=h-1;
	while(height>0){
	  bool flg=false;
	  if(mat[height][i]<=0){
	    for(int tmp=height; tmp>0; tmp--){
	      mat[tmp][i]=mat[tmp-1][i];
	      if(mat[tmp-1][i]>0) flg=true;
	    }
	    mat[0][i]=0;
	  }
	  if(mat[height][i]!=0 || !flg) height--;
	}// for
      }// rep
    }

    cout<<res<<endl;

  }
  return 0;
}

C++: #include<iostream>
#include<string.h>

using namespace std;

int main(){
	while(1){
		int h;
		cin >> h;
		if(h == 0) break;
		
		int puzle[10][5];
		int reffer [10][5];
		memset(puzle,0,sizeof(puzle));
		
		for(int i=0;i < h;i++){
			for(int j = 0;j < 5;j++){
				cin >> puzle[i][j];
				reffer[i][j] = i;
			}
		}
		
		int point = 0;
		while(true){
			int lastpoint = point;
			for(int i=0;i < h;i++){
				for(int j = 0;j < 5;j++){
					if(reffer[i][j] == -1) continue;
					int k;
					int left = puzle[reffer[i][j]][j];
					for(k = 1;j+k < 5 && left == puzle[reffer[i][j+k]][j+k];k++){
						
					}
					if(k >= 3){
						point += k*left;
						for(int p = i;p >= 0;p--){
							for(int q = j;q < j+k;q++){
								if(p == 0){
									reffer[p][q] = -1;
								}
								else{
									reffer[p][q] = reffer[p-1][q];
								}
							}
						}
						j += k;
					}
				}
			}
			if(lastpoint == point) break;
		}
		cout << point << endl;
	}
	return 0;
}