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++: # define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <map>
# include <stack>
using namespace std;
//# define M_PI 3.141592
# define INF ((int)(1<<30))
# define FOR(i,n) for(int i=0;i<(int)n;i++)
# define FORI(i,k,n) for(int i=k;i<(int)n;i++)
# define toRad 2.0*M_PI/360.0
# define inin(x) int x;cin>>x;
# define all(x) x.begin(),x.end()
# define debug(x) cout<<#x<<" :"<<x<<endl;
# define rep(i,n) for(int i=0;i<(int)n;i++)
# define EPS 1e-12
# define CHECK(i,a) FOR(i,a.size())cout<<#a<<"["<<i<<"] : "<<a[i]<<endl; 
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = { 0,-1,1,0 }, dy[4] = { 1,0,0,-1 };


int main()
{
	int h;
	while (cin >> h&&h)
	{
		vi a[11];
		FOR(i, h)
		{
			FOR(j, 5)
			{
				inin(x); a[i].push_back(x);
			}
		}
		bool ok = true;
		int ans = 0;
		while (ok)
		{
			ok = false;
			FOR(i, h)
			{
				for (int j = 5; j >= 3;j--)
				{
					for (int k = 0; k <= 5 - j; k++)
					{
						bool okk = true;
						for (int l = k; l < k + j; l++)
						{
							if (a[i][k] == 0)okk = false;
							if (a[i][l] != a[i][k])okk = false;
						}
						if (okk)
						{
							ok = true;
							for (int l = k; l < k + j; l++)
							{
								ans += a[i][l];
								a[i][l] = 0;
							}
						}
					}
				}
			}
			for (int c = 0; c < h;c++) for (int i = 0; i < h - 1; i++)
			{
				for (int j = 0; j < 5; j++)
				{
					if (a[i+1][j] == 0)
					{
						swap(a[i][j], a[i + 1][j]);
					}
				}
			}
		}
		cout << ans << endl;
	}
}

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

#define INF 1e9
#define llINF 1e18
#define MOD 1e9+7
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
using namespace std;
int main(){
  int h;
  while(cin>>h,h){
    int num[h][5]={};
    for(int i=0;i<h;i++)
      for(int j=0;j<5;j++)
	cin>>num[i][j];
    int ans=0;
    while(1){
      bool flag=true;
      vector<pair<pair<int,int>,int> >kesi;
      for(int i=0;i<h;i++){
	int cnt=1;
	pair<int,int> doko=mp(INF,0);
	for(int j=0;j<4;j++){
	  if(num[i][j]!=0&&num[i][j]==num[i][j+1]){
	    if(doko.S==0)
	      doko=mp(min(doko.F,j),num[i][j]);
	    
	      cnt++;
	  }else{
	    if(cnt>=3){
	      flag=false;
	      kesi.pb(mp(mp(doko.F,doko.F+cnt-1),i));
	      ans+=cnt*doko.S;
	      //   cout<<ans<<endl;
	    }
	    doko=mp(INF,0);
	    cnt=1;
	  }
	}
	if(cnt>=3){
	  flag=false;
	  kesi.pb(mp(mp(doko.F,doko.F+cnt-1),i));
	  ans+=cnt*doko.S;
	  // cout<<cnt<<" "<<doko.S<<" "<<ans<<endl;
	}
      }
      if(flag)break;
      for(int i=kesi.size()-1;i>=0;i--){
	  for(int k=kesi[i].F.F;k<=kesi[i].F.S;k++){
	    num[kesi[i].S][k]=0;
	  }
	}
      

      for(int i=h-1;i>=0;i--){
	for(int j=0;j<5;j++){
	  int hei=i;
	  while(num[hei][j]==0&&hei>0){
	    hei--;
	  }
	  if(num[i][j]==0){
	  num[i][j]=num[hei][j];
	  num[hei][j]=0;
	  }
	}
      }
  
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <assert.h>


int main(){

  int x = 1, y = 1;
  double s;
  double no_tax;

  std::vector<int> ans(0);

  while(1){
	
	scanf("%d %d %lf", &x, &y, &s);
	//	printf("s = %f\n", s);
	//	printf("%d %d %f\n", x, y, s);
	if(x == 0){
	  // printf("OK1\n");
	  if(y == 0){
		//printf("OK2\n");
		if(s == 0){
		  //printf("OK3\n");
		  break;
		}
	  }
	}
	
	// assert(x != 0);
	// assert(y != 0);
	// assert(s != 0);
	
	no_tax = s * ((double)100 / (100 + x));
	//printf("%f\n", no_tax);
	//	after = no_tax * (double)((double)((double)100 + (double)y) / 100);

	int max = 0;
	
	for(int i = 1; i < s/2; i++){
	  int sum = (int)((double)i * ((double)100+y) / ((double)100+x) + 0.5);
	  sum += (int)((double)(s-i) * ((double)100+y) / ((double)100+x) + 0.5);

	  if(i == 1){
		max = sum;
	  }
	  else{
		if(max < sum){
		  max = sum;
		}
		
	  }
	}
	ans.push_back((int)max);
	//printf("%f\n", after);
  }

  for(int i = 0; i < ans.size(); i++){
	printf("%d\n", ans.at(i));
  }
  
  return 0;
}