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<bits/stdc++.h>
using namespace std;




int main(){
  while(1){


  
  int h;
  cin>>h;
  if(h==0){break;}
  int a[20][10]={};
  for(int i=5;i<h+5;i++){
    for(int t=0;t<5;t++){
      cin>>a[i][t];
    }
  }
  int count=0;
  bool j=true;

  while(j){

    j=false;
 for(int i=5;i<h+5;i++){
    for(int t=0;t<3;t++){
      for(int x=t+1;t<5;x++){
	if(a[i][t]!=a[i][x] || a[i][x]==0){break;}
	if(x-t>=2 && a[i][x+1]!=a[i][x]){
	  j=true;
	  for(int y=t;y<=x;y++){count+=a[i][y]; a[i][y]=0;}
	}
      }
    }
  }

 bool k=true;
 while(k){
   k=false;
 for(int i=h+4;i>=5;i--){
   for(int t=0;t<5;t++){
     if(a[i][t]==0 && a[i-1][t]!=0){k=true; a[i][t]=a[i-1][t]; a[i-1][t]=0;}
   }
 }
 }
 
 
  }

  cout<<count<<endl;




  }
  
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <list>
#include <math.h>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007 // 1e9+7(prime number)
#define INF 1000000000 // 1e9

using namespace std;
typedef long long ll;

int main() {
	while (1) {
		int h, r, score, count, num;
		bool erased;
		vector<list<int> > mp(5);
		vector<list<int>::iterator> itrs(5);

		score = 0;
		count = 0;
		cin >> h;
		if (h==0) break;
		vector<vector<int>> input(h);
		REP(j, h) {
			REP(k, 5) {
				cin >> r;
				input[j].push_back(r);
			}
		}

		REP(k, 5) {
			REP(j, h) { mp[k].push_front(input[j][k]); }
		}

		while (1) {
			// 各行を見て点数を計算
			REP(i, 5) itrs[i] = mp[i].begin();
			REP(j, h) {
				num = -1;
				REP(k, 5) {
					/*debug(j);
					debug(k);
					debug(*itrs[k]);*/
					if (j < mp[k].size()) {
						if (*itrs[k] == num && num>0)
							count++;
						else {
							num = *itrs[k];
							count = 1;
						}
						if (count >= 3) {
							*itrs[k] = -1;
							if (count == 3) {
								score += 3 * num;
								itrs[k - 1]--;
								itrs[k - 2]--;
								*itrs[k - 1] = -1;
								*itrs[k - 2] = -1;
								itrs[k - 1]++;
								itrs[k - 2]++;
							} else {
								score += num;
							}
							/*debug(j);
							debug(num);
							debug(count);
							debug(score);*/
						}
						if (itrs[k] != mp[k].end()) itrs[k]++;
					}
				}
			}

			erased = false;
			REP(i, 5) itrs[i] = mp[i].begin();
			REP(j, h) {
				REP(k, 5) {
					//debug(j);
					//debug(k);
					if (itrs[k] != mp[k].end()) {
						if (*itrs[k] < 0) {
							erased = true;
							//debug(*itrs[k]);
							itrs[k] = mp[k].erase(itrs[k]);
							mp[k].push_back(0);
						} else {
							//debug(*itrs[k]);
							++itrs[k];
						}
					}
					//				if (*itrs[k] != NULL)debug(*itrs[k]);
				}
				//cout << "--------------------------" << endl;
			}
			//cout << score << endl;
			//cout << "--------------------------" << endl;
			if (!erased) break;
			REP(i, 5) if (mp[i].empty()) break;
		}

		cout << score << endl;
		/*if (score==90){
			debug(po);
			debug(score);
			cout << h << endl;
			REP(j,h){
				REP(k,5) printf("%d ", input[j][k]);
				cout << endl;
			}
		}
		po++;*/
		
	}
	return 0;
}