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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

void mainmain(){
	int h;
	while(cin>>h,h){
		vvint vv;
		initvv(vv,h,5);
		rep(i,h){
			rep(j,5){
				cin>>vv[i][j];
			}
		}
		int ans=0;
		while(1){
			// rep(i,h){
				// rep(j,5){
					// cout<<vv[i][j]<<" ";
				// }
				// cout<<endl;
			// }
			// cout<<endl;
			bool f=false;
			rep(i,h){
				bool ff=false;
				rep(j,1){
					if(vv[i][j]==vv[i][j+1]&&vv[i][j]==vv[i][j+2]&&vv[i][j]==vv[i][j+3]&&vv[i][j]==vv[i][j+4]&&vv[i][j]){
						ff=true;
						f=true;
						ans+=vv[i][j]*5;
						rep(k,5){
							vv[i][j+k]=0;
						}
						break;
					}
				}
				if(ff) continue;
				rep(j,2){
					if(vv[i][j]==vv[i][j+1]&&vv[i][j]==vv[i][j+2]&&vv[i][j]==vv[i][j+3]&&vv[i][j]){
						ff=true;
						f=true;
						ans+=vv[i][j]*4;
						rep(k,4){
							vv[i][j+k]=0;
						}
						break;
					}
				}
				if(ff) continue;
				rep(j,3){
					if(vv[i][j]==vv[i][j+1]&&vv[i][j]==vv[i][j+2]&&vv[i][j]){
						f=true;
						ff=true;
						ans+=vv[i][j]*3;
						rep(k,3){
							vv[i][j+k]=0;
						}
					}
				}
				if(ff) continue;
			}
			for(int i=h-2;i>=0;i--){
				rep(j,5){
					int cnt=i;
					while(vv[cnt+1][j]==0){
						swap(vv[cnt][j],vv[cnt+1][j]);
						cnt++;
						if(cnt+1==h) break;
						// cout<<cnt<<endl;
						// cnt++;
					}
				}
			}
			if(!f) break;
			// else break;
		}
		cout<<ans<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include<iostream>
#include<vector>
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;

const int w=5;
int puzzle[w][10];

int del(vector<pii> stone){
  int res=0;
  for(int i=0;i<stone.size();i++){
    int x,y;
    x=stone[i].first;
    y=stone[i].second;
    res+=puzzle[x][y];
    puzzle[x][y]=0;
  }
  return res;
}

vector<pii> check(int h){
  vector<pii> stone;
  for(int i=0;i<h;i++){
    int cnt=0;
    for(int j=0;j<w;j++){
      while(j<w && puzzle[j][i] == puzzle[j+1][i] && puzzle[j][i]!=0){
	cnt++;j++;
      }
      if(cnt>=2){
	for(int k=j-cnt;k<=j;k++){
	  stone.push_back(mp(k,i));
	}
      }
      cnt=0;
    }
  }
  return stone;
}

bool drop(int h){
  bool res=false;
  for(int i=0;i<w;i++){
    for(int j=0;j<h;j++){
      for(int k=h-1;k>j;k--){
	if(puzzle[i][k]==0){
	  res=true;
	  int tmp=puzzle[i][k];
	  puzzle[i][k]=puzzle[i][k-1];
	  puzzle[i][k-1]=tmp;
	}
      }
    }
  }
  return res;
}

void printPuzzle(int h){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cout << puzzle[j][i] << " ";
    }
    cout << endl;
  }
}

int main(){
  int h;
  while(cin>>h,h){
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>puzzle[j][i];
    int ans=0;
    vector<pii>stone=check(h);
    while(!stone.empty()){
      ans+=del(stone);
      //out << "del end" << endl;
      drop(h);
      //cout << "drop end" << endl;
      //printPuzzle(h);
      stone=check(h);
    }
    cout << ans << endl;
  }
  return 0;
}

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

const int H = 10, W = 5;

int board[H][W];

int main(){
	int h, score, inc;
	while(cin >> h, h){
		inc = 1;
		score = 0;
		for(int i = 0;i < h;++i){
			for(int j = 0;j < W;++j){
				cin >> board[i][j];
			}
		}
		while(inc){
			int c, k;
			inc = 0;
			for(int i = 0;i < h;++i){
				for(int j = 0;j < W;++j){
					c = 0;
					for(int k = j;k < W && board[i][j] == board[i][k];++k){
						++c;
					}
					if(c >= 3){
						inc += board[i][j] * c;
						for(int k = j;k < j + c;++k){
							board[i][k] = 0;
						}
					}
					j += (c - 1);
				}
			}
			for(int i = 1;i < h;++i){
				for(int j = 0;j < W;++j){
					if(!board[i][j]){
						for(int k = i - 1;0 <= k;--k){
							board[k + 1][j] = board[k][j];
						}
						board[0][j] = 0;
					}
				}
			}
			score += inc;
		}
		cout << score << endl;
	}
	return 0;
}