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 <cstdio>
#include <cstdlib>
#include <utility>
#include <typeinfo>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() 
{
	int n;
	while(cin>>n&&n!=0)
	{
		int a[10][5]={};
		REP(i,n){REP(j,5){cin>>a[i][j];}}
		
		int ans(0);
		
		while(true)
		{
			int count(0);
			bool b[10]={};//i列目を消すならフラグ
			int c[10]={};//連続する一つ目の位置
			int d[10]={};//連続した個数
			int e[10]={};//連続した数字
			
			//一行毎に判定。○なら数字を10に変えて、ポイントを足す
			REP(i,n){
				c[i]=0;
				d[i]=1;	
				e[i]=a[i][0];
				REP(j,4)
				{
					if(a[i][j+1]==a[i][j])
					{
						d[i]++;
						if(d[i]>=3&&e[i]!=10){b[i]=true;}
					}
					else
					{
						if(b[i]){break;}
						else
						{
							c[i]=j+1;
							d[i]=1;
							e[i]=a[i][j+1];
						}
					}
				}
			}
		
			for(int i=0;i<n;i++)
			{
				if(b[i])
				{
					count++;
					ans+=e[i]*d[i];
					for(int j=c[i];j<c[i]+d[i];j++)
					{
						a[i][j]=10;
					}
				}
			}
		
			//消した行が1以上なら、タテに下詰する
			if(count>0)
			{
				for(int i=0;i<5;i++)
				{
					for(int j=0;j<n;j++)
					{
						if(a[j][i]==10)
						{
							for(int k=j;k>0;k--)
							{
								int tmp = a[k][i];
								a[k][i]=a[k-1][i];
								a[k-1][i]=tmp;
							}
						}
					}
				}
				
			}
			//消した行が0なら終わり
			if(count==0){break;}
			
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

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

int main(){
  while(1){
    int h; cin >> h; if(h == 0) break;
    
    vector<deque<int>> stone(5, deque<int>(h));
    rep(j, h) rep(i, 5) cin >> stone[i][j]; // j = 0が天井, h-1が底
    
    int ans = 0;
    while(1){
      int tmp = ans;
      rep(j, h){ // 上から見る
	int c = 1, n = -1; // c : いくつ連続か, n = 連続である数字
	rep(i,4){
	  if(stone[i][j] == stone[i+1][j]){
	    c++;
	    if(c >= 3){
	      n = stone[i][j];
	      break;
	    }
	  }else c = 1;
	}
	
	if(n != -1){
	  rep(i, 5){
	    if(stone[i][j] == n){
	      ans += n;
	      stone[i].erase(stone[i].begin() + j); // 消して
	      stone[i].push_front(-1); // 上に詰める
	    }
	  }
	}
      }
      if(tmp == ans) break;
    }
    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>
#define REP(a,b,c) for(int a=b; a<(int)c; a++)
#define REPD(a,b,c) for(int a=b; a>=(int)c; a--)
#define rep(a,b) REP(a,0,b)
#define repd(a,b) REPD(a,b,0)
typedef long long int ll;
using namespace std;

void show(int data[][5], int H){
  rep(i,H){
    rep(j,5){
      cout << data[i][j];
    }
    cout << endl;
  }  
}

int main(){
  int H;
  while(true){
    cin >> H;
    if(H == 0) break;
    int data[H][5] = {-1};
    rep(i,H) rep(j,5){
      cin >> data[i][j];
    }
    int ans = 0;

    while(true){
      bool finish_flag = true;
      // show(data,H);
      //cout << endl;
      rep(i,H){
        int cnt = 1, idx = 0, eidx = 0;
        REP(j,1,5){
          eidx = j;
          if(data[i][j] == data[i][idx] && data[i][j] != -1){
            cnt++;
          }else{
            if(cnt >= 3){
              break;
            }
            cnt = 1; idx = j;
          }
        }
        if(cnt >= 3){
          finish_flag = false;
          ans += cnt*data[i][idx];
          REP(j,idx,eidx+1){
            data[i][j] = -1;
          }
        }
      }
      //      show(data,H);
      //cout << endl;
      for(int i=H-1; i>=0; i--){
        rep(j,5){
          if(data[i][j] == -1){
            for(int k=i-1; k>=0; k--){
              if(data[k][j] != -1){
                data[i][j] = data[k][j];
                data[k][j] = -1;
                break;
              }
            }
          }
        }
      }
      if(finish_flag) break;
    }
    cout << ans << endl;
  }
  return 0;
}