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 "random"
#include "ctime"
#include "chrono"
#include "string"
#include "bitset"
#include "algorithm"
#include "map"
#include "queue"
#include "list"

using namespace std;


int main() {
	
	int H;
	const int W = 5;
	cin >> H;
	list<int>ret;
	while (H) {
		vector<vector<int>>v(H, vector<int>(W));
		int ans = 0;
		for (int i = H - 1; i >= 0; i--) {
			for (int j = 0; j < W; j++) {
				cin >> v[i][j];
			}
		}
		bool flag = true;
		while (flag) {
			//for (auto i : v) {
			//	for (auto j : i) {
			//		cout << j;
			//	}
			//	cout << endl;
			//}
			//cout << endl;
			flag = false;
			for (int i = 0; i < H; i++) {
				vector<int>num(10,0);
				for (int j = 0; j < W; j++) {
					num[v[i][j]]++;
				}
				for (int j = 1; j < 10; j++) {
					while (num[j] >= 3) {
						for (int k = 0; k <= W - num[j]; k++) {
							int cnt = 0;
							for (int l = k; l < k + num[j]; l++) {
								cnt += j == v[i][l];
							}
							if (cnt == num[j]) {
								flag = true;
								ans += j*num[j];
								for (int l = k; l < k + num[j]; l ++) {
									if (v[i][l] == j) {
										v[i][l] = 0;
									}
								}
							}
						}
						num[j]--;
					}
				}
			}
			for (int i = 1; i < H; i++) {
				for (int j = 0; j < W; j++){
					if (v[i][j]) {
						for (int k = i; k > 0; k--) {
							if (!v[k - 1][j]) {
								swap(v[k][j], v[k - 1][j]);
							}
						}
					}
				}
			}
		}
		ret.push_back(ans);
		cin >> H;
	}
	for (auto i : ret) {
		cout << i << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

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

    vector<vector<int> > board(h, vector<int>(5, 0));
    for(int i=0; i<h; i++){
      for(int j=0; j<5; j++){
        cin >> board[i][j];
      }
    }

    int point=0;
    bool flag = true;
    while(flag){
      flag = false;
      //disappear
      for(int i=0; i<h; i++){
        for(int j=0; j<3; j++){
          int col = board[i][j];
          if(col==0) continue;
          if(board[i][j+1]==col && board[i][j+2]==col){
            flag =true;
            point += col*3;
            board[i][j] = board[i][j+1] = board[i][j+2] = 0;
            if(j<2 && board[i][j+3]==col){
              point += col;
              board[i][j+3] = 0;
            }
            if(j==0 && board[i][j+4]==col){
              point += col;
              board[i][j+4] = 0;
            }
          }
        }
      }

      //fall
      for(int i=0; i<5; i++){
        for(int j=h-1; j>0; j--){
          for(int k=0; k<j; k++){
            if(board[k+1][i]==0){
              swap(board[k+1][i], board[k][i]);
            }
          }
        }
      }

    }

    cout << point << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

int H;
int field[10][6]={};

ll solve(){
	ll res = 0;
	rep(i,H){
		bool f[3] = {};
		ll cnt = 2;
		ll num = -1;
		for(int j = 0;j < 3;j++){
			if(field[i][j] == 0)continue;
			for(int k = 1; k < 3;k++){
				if(field[i][j+k] == 0)break;
				if(field[i][j] != field[i][j+k])break;
				if(k == 2){
					f[j] = 1;
					num = field[i][j];
				}
			}
			cnt += f[j];
		}
		if(num == -1)continue;
		res += num*cnt;
		for(int j = 0;j < 3;j++){
			if(!f[j])continue;
			for(int k = 0; k < 3;k++)field[i][j+k] = 0;
		}
	}
	return res;
}

void drop(){
	rep(j,5){
		vector<ll> v;
		rep(i,H){
			if(field[i][j] != 0)v.push_back(field[i][j]);
		}
		reverse(ALL(v));
		while(v.size()<H)v.push_back(0);
		reverse(ALL(v));
		rep(i,H)field[i][j] = v[i];
	}
}

void print(){
	for(int i = H - 1;i >= 0;i--){
		rep(j,5)cout << field[i][j] << " ";
		cout << endl;
	}
}

int main(){
	while(1){
		cin	>> H;
		if(H == 0)break;
		for(int i = 0 ;i < H;i++)for(int j = 0;j < 5;j++)cin >> field[i][j];
		int ans = 0;
		while(1){
			// print();
			int score = solve();
			// cout << score << endl;
			if(score == 0)break;
			ans += score;
			drop();
		}
		cout<<ans<<endl;
	}

}