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;
#define int long long
#define REP(i, s, n) for (signed i = (s); i < signed(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

const double EPS = 1e-9;

signed main() {
	int h;
	while (cin >> h && h) {
		VV board(h, V(5));
		rep(i, h) rep(j, 5) cin >> board[i][j];
		int score = 0;

		while (true) {
			int added = 0;
			VV next = board;
			rep(i, h) rep(j, 3) {
				if (board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2]) {
					added += next[i][j] + next[i][j + 1] + next[i][j + 2];
					next[i][j] = next[i][j + 1] = next[i][j + 2] = 0;
				}
			}
			score += added;
			board = next;

			rep(j, 5) {
				int shift = 0;
				for (int i = h - 1; i >= 0; i--) {
					int tmp = board[i][j];
					board[i][j] = 0;
					board[i + shift][j] = tmp;
					shift += tmp == 0;
				}
			}

			if (added == 0) break;
		}

		cout << score << endl;
	}
}

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

#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define ALL( a )        (a).begin(), (a).end()             

int main() {
	int n;
	array<int, 5> tmp;

	while( 1 ) {
		cin >> n; if( n == 0 ) break;

		vector<array<int, 5>> p( n );
		REP( i, n ) {
			REP( j, 5 ) {
				cin >> tmp[j];
			}
			p[i] = tmp;
		}

		int m = 0, score = 0;
		while( 1 ) {
			FOR( i, m, n ) {
				int cnt = 0;
				REP( j, 5 ) {

					int tmp = p[i][j];
					if( j != 4 && p[i][j + 1] == tmp ) {
						cnt++;
					} else {
						if( cnt >= 2 ) {
							score += ( cnt + 1 ) * p[i][j];
							REP( k, cnt + 1 ) {
								if( i == 0 ) {
									p[i][j - k] = 0;
								} else {
									REP( l, i ) {
										p[i - l][j - k] = p[i - l - 1][j - k];
									}
									p[0][j - k] = 0;

								}

							}

						}
						cnt = 0;
					}

				}

			}
			m++;
			if( m == n ) break;
		}


		cout << score << endl;
	}

}

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

int main(){
  while(1){
    int h;
    cin >> h;
    if(h==0) break;

    vector<vector<int> > board(h, vector<int>(5, 0));
    for(int i=0; i<h; i++){
      for(int j=0; j<5; j++){
        cin >> board[i][j];
      }
    }

    int point=0;
    bool flag = true;
    while(flag){
      flag = false;
      //disappear
      for(int i=0; i<h; i++){
        for(int j=0; j<3; j++){
          int col = board[i][j];
          if(col==0) continue;
          if(board[i][j+1]==col && board[i][j+2]==col){
            flag =true;
            point += col*3;
            board[i][j] = board[i][j+1] = board[i][j+2] = 0;
            if(j<=1 && board[i][j+3]==col){
              point += col;
              board[i][j+3] = 0;
            }
            if(j==0 && board[i][j+4]==col){
              point += col;
              board[i][j+4] = 0;
            }
          }
        }
      }

      //fall
      for(int i=0; i<5; i++){
        for(int j=0; j<h; j++){
          for(int k=h-1; k>j; k--){
            if(board[k][i]==0){
              swap(board[k-1][i], board[k][i]);
            }
          }
        }
      }

    }

    cout << point << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int b[15][6];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=a-1;i>=0;i--){
			for(int j=0;j<5;j++)scanf("%d",&b[i][j]);
		}
		bool chg=false;
		int ret=0;
		do{
			chg=false;
			for(int i=0;i<a;i++){
				int len=0;
				int now=0;
				for(int j=0;j<6;j++){
					if(now!=b[i][j]){
						if(~now&&len>=3){
							ret+=len*now;
							for(int k=j-len;k<j;k++)b[i][k]=-1;
							chg=true;
						}
						now=b[i][j];
						len=1;
					}else len++;
				}
			}
			for(int i=0;i<5;i++){
				int at=0;
				for(int j=0;j<a;j++){
					if(~b[j][i]){
						b[at++][i]=b[j][i];
					}
				}
				for(int j=at;j<a;j++)b[j][i]=-1;
			}
	//		for(int i=a-1;i>=0;i--){
	//			for(int j=0;j<5;j++)printf("%2d ",b[i][j]);
	//			printf("\n");
	//		}printf("\n");
		}while(chg);
		printf("%d\n",ret);
	}
}