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++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1193&lang=jp
//連鎖消滅パズル
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll=long long;
const ll LINF = 0x1fffffffffffffff;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
template<class T>bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1; } return 0;}
template<class T>bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1; } return 0;}
bool solve(){
  ll h; cin>>h;
  if(h<=0) return false;
  vector<vector<ll>> tbl(h,vector<ll>(5));
  for(auto&& l:tbl){
    for(auto&& v:l) cin>>v;
  }

  auto calc = [](vector<ll>& tbl){
    ll result=0;
    REP(i,tbl.size()){
      ll num=0;
      if(tbl.at(i)==0)continue;
      FOR(j,i+1,tbl.size()){
        if(tbl.at(i)!=tbl.at(j)) break;
        ++num;
      }
      ++num;
      if(num>=3){//先頭も含む
        FOR(j,i,i+num){
          result += tbl.at(j);
          tbl.at(j)=0; //消す
        }
        break;
      }
    }
    return result;
  };
  auto otosu =[&](){
    FOR(x,0,5){
      for(ll y=h-1;y>=0;--y){
        if(tbl[y][x]==0){
          for(ll y2=y-1;y2>=0;--y2){
            if(tbl[y2][x]!=0){
              swap(tbl[y][x],tbl[y2][x]);
              break;
            }
          }
        }
      }
    }
  };

  ll ans=0;
  do{
    dump(tbl); dump(ans);
    ll tmp=0;
    FOR(i,0,tbl.size()){
      tmp += calc(tbl.at(i));//消す
    }
    if(tmp==0)break;
    ans += tmp;
    //落す
    otosu();
  }while(true);
  cout<<ans<<endl;
  return true;
}
int main() {
#if LOCAL&01
  //std::ifstream in("./test/sample-1.in");
  std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  while(solve()){
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
signed main() {
  int H, W = 5; 
  while (cin >> H, H!=0) {
    // v[W][H]
    vector<vector<int>> v(W, vector<int>(H, 0));
    for (int i = H-1; i>=0; i--) {
      for (int j = 0; j < W; j++) {
        int t;
        cin >> t;
        v[j][i]=t;
      }
    }

    int ans = 0;
    for (int i = 0; i < W; i++) {
      for (int j = H-1; j >= 0; j--) {
        int last = -1, cnt = 0;
        for (int k = 0; k < W; k++) {
          if (v[k].size() <= j) {
            cnt = 1;
            last = -1;
          } else if (last == v[k][j]) {
            cnt++;
          } else {
            if (cnt >= 3) {
              ans += v[k-1][j]*cnt;
              for (int l = k-cnt; l < k;  l++) {
                v[l].erase(v[l].begin()+j);
              }
            }
            cnt = 1;
            last = v[k][j];
          }
        }
        if (cnt >= 3) {
          ans +=v[W-1][j]*cnt;
          for (int l = W-cnt; l < W; l++) {
            v[l].erase(v[l].begin()+j);
          }
        }
      }
    }
    
    cout << ans << endl;
  }
}

C++: #include <iostream>
#define EMP -1000
 
using namespace std;

int stage[11][11]={};
int h;


void outStage(){
	for(int i=0;i<h;i++){
		for(int j=0;j<5;j++){
			cout<<stage[i][j];
		}
		cout<<endl;
	}
}

bool delStage(){
	bool conti=false;
	for(int i=0;i<h;i++){
		for(int j=0;j<3;j++){
			int samecount=1;
			int now=-1;
			for(int k=0;;k++){
				if(now==-1){					
					now=stage[i][j+k];
				}
				else if(now==stage[i][j+k] && now!=EMP){
					samecount++;
				}
				if( (now!=-1 && now!=stage[i][j+k]) || k==4){
					if(samecount>=3){
						conti=true;
						for(int l=0;l<samecount;l++){
							stage[i][j+l]=EMP;
						}
						samecount=1;
					}
					goto end;
				}
			}
			end:;
		}
	}
	return conti;
}


void downNum(){
	for(int t=0;t<h;t++){
		for(int i=1;i<h-t;i++){
			for(int j=0;j<5;j++){
				if( i-1>=0 && stage[i][j]==EMP){
					swap(stage[i][j],stage[i-1][j]);
				}
			}
		}
	}
}

int countScore(){
	int sum=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<5;j++){
			if(stage[i][j]!=EMP)
				sum+=stage[i][j];
		}
	}
	return sum;
}

int main(){
	while(1){
		int sum=0;
		cin>>h;
		if(h==0)break;
		for(int i=0;i<h;i++){
			for(int j=0;j<5;j++){
				cin>>stage[i][j];
				sum+=stage[i][j];
			}
		}
//end input;

		while(1){
			if(delStage()==false)break;
			downNum();
		}
		cout<<sum-countScore()<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int board[10][5];

int main(){ _;
  int h;
  while(cin>>h,h!=0){
    REP(i,10)REP(j,5)board[i][j]=0;
    REP(i,h){
      REP(j,5){
	cin>>board[h-i-1][j];
      }
    }
    int sum=0;
    bool chain=false;
    do{
      chain=false;
      REP(i,h){
	REP(j,3){
	  if(board[i][j]==0)continue;
	  int k=j+1;
	  for(;k<5;k++){
	    if(board[i][j]!=board[i][k])break;
	  }
	  if(k-j>=3){
	    sum+=board[i][j]*(k-j);
	    for(;j<k;j++)
	      board[i][j]=0;
	    break;
	  }
	}
      }
      REP(j,5){
	REP(i,h-1){
	  if(board[i][j]==0){
	    int k=i+1;
	    for(;board[k][j]==0;k++){}
	    swap(board[i][j],board[k][j]);
	    chain=true;
	  }
	}
      }
    }while(chain);
    cout<<sum<<endl;
  }
}