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 <bits/stdc++.h>
using namespace std;
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={-1,0,1,0,1,1,-1,-1};
const int INF = 1<<30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define rep(i,j) reps(i,0,j)
#define MOD 1000000007
typedef pair<int,int> Pii;
typedef pair<int,Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

vector < Pii > deleteList;
int H,W;
int stage[12][6];

bool check(){
	deleteList.clear();
	bool flg = false;
	rep(i,H){
		reps(j,1,W-1){
			if(stage[i][j-1] != 0 && stage[i][j-1] == stage[i][j] && stage[i][j] == stage[i][j+1]){
				deleteList.PB(Pii(i,j-1));
				flg = true;
			}
		}
	}
	return flg;
}

int deleteBlock(){
	int ret = 0;
	rep(i,deleteList.size()){
		reps(j,deleteList[i].se,deleteList[i].se+3){
			if(stage[deleteList[i].fi][j] > 0)ret += stage[deleteList[i].fi][j];
			stage[deleteList[i].fi][j] = -1;
		}
	}
	return ret;
}

void dropBlock(){
	rep(i,H){
		rep(j,W){
			if(stage[i][j] == -1){
				while(stage[i][j] == -1){
					reps(k,i+1,H){
						stage[k-1][j] = stage[k][j];
					}
					stage[H-1][j] = 0;
				}
			}
		}
	}
	return ;
}

void DEBUGPRINT(){
	
	rep(i,H){
		rep(j,W){
			printf("%2d%c",stage[i][j],j==W-1?'\n':' ');
		}
	}
	puts("######################");
}

int main(){
	W = 5;
	while(scanf("%d",&H),H){
		memset(stage,-1,sizeof(stage));
		rep(i,H){
			rep(j,W){
				scanf("%d",&stage[H-i-1][j]);
			}
		}
		int score = 0;
		while(check()){
			//DEBUGPRINT();
			score += deleteBlock();
			dropBlock();
		}
		
		printf("%d\n",score);
	}
	return 0;
}

C++: #include<stdio.h>
int main(){
  int a[11][5];
  int h,i,j,k,c,po,score,sw,sw2;
  while(1){
    score=0;
    sw=0;
    sw2=0;
    po=0;
    c=0;

    scanf("%d",&h);
    if(h==0)break;
    for(i=0;i<h;i++){
      for(j=0;j<5;j++){
	scanf("%d",&a[i][j]);
      }
    }
    //  printf("check0\n");
    while(1){
      sw=score;
      //del
      for(i=0;i<h;i++){
	for(j=0;j<3;j++){
	  c=1;
	  po=a[i][j];
	  for(k=1;j+k<5;k++){
	    if(a[i][j+k]==po){
	      c++;
	    }
	    else break;
	  }
	  if(c>=3){
	    for(k=0;k<c;k++){
	      score+=a[i][j+k];
	      a[i][j+k]=0;
	    }
	  }
	}
      }
      if(sw==score)break;
      //del end
      //  printf("check1\n");
      //fall
      while(1){
	sw2=0;
	if(h!=0){
	  for(i=h-2;i>=0;i--){
	    for(j=0;j<5;j++){
	      if(a[i][j]!=0&&a[i+1][j]==0){
		a[i+1][j]=a[i][j];
		a[i][j]=0;
		sw2=1;
	      }
	    }
	  }
	}
	if(sw2==0)break;
      }
      //fall end
      //  printf("check2\n");
    }
    printf("%d\n",score);
  }
  return 0;
}

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

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

typedef long long ll;
using namespace std;
int mod = 1000000007;

int main(void){
    //消えるたびに上から落としていけばいい
    int H;
    while(true){
        cin >> H;
        if(H == 0) break;
        vector<vector<int>> grid(H);
        rep(i, H) grid[i].resize(5);
        rep(i, H) rep(j, 5) cin >> grid[i][j];
        bool cont = true;
        int score = 0;
        while(cont){
            cont = false;
            rep(i, H){
                rep(j, 3){
                    if(grid[i][j] != -1 && grid[i][j] == grid[i][j+1] && grid[i][j] == grid[i][j+2]){
                        if(j <= 1 && grid[i][j] == grid[i][j+3]){
                            if(j == 0 && grid[i][j] == grid[i][j+4]){
                                score += grid[i][j]*5;
                                for(int k = i; k >= 0; k--){
                                    if(k == 0) rep(l, 5) grid[0][l] = -1;
                                    else rep(l, 5) grid[k][l] = grid[k-1][l];
                                }
                            }
                            else{
                                score += grid[i][j]*4;
                                for(int k = i; k >= 0; k--){
                                    if(k == 0) for(int l = j; l < j+4; l++) grid[0][l] = -1;
                                    else for(int l = j; l < j+4; l++) grid[k][l] = grid[k-1][l];
                                }
                            }
                        }
                        else{
                            score += grid[i][j]*3;
                            for(int k = i; k >= 0; k--){
                                if(k == 0) for(int l = j; l < j+3; l++) grid[0][l] = -1;
                                else for(int l = j; l < j+3; l++) grid[k][l] = grid[k-1][l];
                            }
                        }
                        cont = true;
                        break; //同じ行で二個以上の消滅は無い
                    }
                }
            }
        }
        cout << score << endl;
    }
    return 0;
}