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 <iostream>
using namespace std;
int main(){
  int h;
  while (cin>>h,h|h!=0){
    string temp;
    int board[h][6];
    for (int i=0;i<h;i++){
      cin>>board[i][0]>>board[i][1]>>board[i][2]>>board[i][3]>>board[i][4];
    }
    int num,count;
    int check=0;
    int score=0;
    while (1){
      check=score;
      for (int i=h-2;i>=0;i--){
	for (int j=0;j<5;j++){
	  for (int k=h-1;k>i;k--){
	    if (board[k][j]==0){
	      board[k][j]=board[i][j];
	      board[i][j]=0;
	      break;
	    }
	  }
	}
      }
      for (int i=0;i<h;i++){
	count=1;
	num=0;
	for (int j=0;j<5;j++){
	  if (board[i][j]==num){
	    count++;
	  }
	  else{
	    if (count>2)
	      break;
	    count=1;
	  }
	  num=board[i][j];
	}
	if (count<=2)
	  num=0;
	for (int j=0;j<5;j++){
	  if (board[i][j]==num){
	    board[i][j]=0;
	  }
	}
	score=score+num*count;
      }
      if (check==score){
	break;
      }
    }
    cout<<score<<endl; 
  }
}

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

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

int in[100][5];
int ans;

void solve(){
  rep(i, n) rep(j, 5) cin >> in[n - i - 1][j];
  ans = 0;
  while(true){
    bool end = true;
    rep(i, n){
      rep(j, 5){
        if(in[i][j] == 0) continue;
        int k = j;
        while(k < 5){
          if(in[i][j] == in[i][k]) ++k;
          else break;
        }
        if(k - j >= 3){
          end = false;
          ans += (k - j) * in[i][j];
          for(int l = j; l < k; ++l)
            in[i][l] = 0;
        }
      }
    }
    if(end) break;
    while(true){
      bool end = true;
      for(int i = 0; i + 1 < n; ++i){
        rep(j, 5){
          if(in[i][j]) continue;
          if(in[i + 1][j]){
            end = false;
            swap(in[i + 1][j], in[i][j]);
          }
        }
      }
      if(end) break;
    }
  }
  
  cout << ans << endl;
}

int main(int argc, char *argv[])
{

  while(cin >> n && n) solve();
  return 0;
}