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 _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
 
const static int tx[] = {0,1,0,-1};
const static int ty[] = {-1,0,1,0};

const static double EPS = 1e-8;

int stage[10][5];
bool marked[10][5];


void disp(int W,int H){
  for(int y = 0; y < H; y++){
    for(int x = 0; x < W; x++){
      printf("%d ",stage[y][x]);
    }
    printf("\n");
  }
  printf("\n");
}

bool mark(int W,int H){
  bool res = false;
  memset(marked,false,sizeof(marked));
  for(int sy = 0; sy < H; sy++){
    for(int sx = 0; sx < W; sx++){
      int count = 1;
      for(int x = sx; x + 1 < W; x++){
        if(stage[sy][x] != -1 
           && stage[sy][x] == stage[sy][x+1]){
          count++;
        }
        else {
          break;
        }
      }
      if(count >= 3){
        for(int x = sx; count > 0; x++,count--){
          marked[sy][x] = true;
          res = true;
        }
      }
    }
  }
  return res;
}

int compute_score(int W,int H){
  int freq[10] = {};
  for(int y = 0; y < H; y++){
    for(int x = 0; x < W; x++){
      if(marked[y][x]){
        freq[stage[y][x]]++;
      }
    }
  }

  int res = 0;
  for(int i = 1; i <= 9; i++){
    res += freq[i] * i;
  }
  return res;
}

void erase(int W,int H){
  int next[10][5];
  memset(next,-1,sizeof(next));

  for(int x = 0; x < W; x++){
    int pos = H - 1;
    for(int y = H - 1; y >= 0; y--){
      if(!marked[y][x]){
        next[pos][x] = stage[y][x];
        pos--;
      }
    }
  }
  memcpy(stage,next,sizeof(int)*10*5);
}

int main(){
  int H;
  const int W = 5;
  while(~scanf("%d",&H)){
    if(H == 0) break;
    memset(stage,-1,sizeof(stage));
    for(int y = 0; y < H; y++){
      for(int x = 0; x < W; x++){
        scanf("%d",&stage[y][x]);
      }
    }

    int res = 0;
    while(mark(W,H)){
      res += compute_score(W,H);
      erase(W,H);
    }
    printf("%d\n",res);
  }
}

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

int slide(vector<vector<int>>& v, int retu) {
    int rc = 0;
    vector<int> remove;
    for (int i = 0; i < 3; i++) {
        int right = i;
        while (v[retu][i] == v[retu][right + 1] && v[retu][i] != 0 && right < 4) {
            right++;
        }
        if (right - i + 1 >= 3) {
            rc += (right - i + 1) * v[retu][i];
            for (int rRetu = retu; rRetu >= 0; rRetu--) {
                for (int j = i; j <= right; j++) {
                    if (rRetu == 0) {
                        v[0][j] = 0;
                    } else {
                        v[rRetu][j] = v[rRetu - 1][j];
                    }
                }
            }
            return rc;
        }
    }
    return 0;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (1) {
        int H;
        cin >> H;
        if (!H) break;
        vector<vector<int>> field(H, vector<int>(5, 0));
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < 5; j++) {
                int res;
                cin >> res;
                field[i][j] = res;
            }
        }
        int ans = 0;
        for (int i = 0; i < H * 3 * 3; i++) {
            for (int i = 0; i < H; i++) {
                int res = slide(field, i);
                ans += res;
            }
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
#define PB push_back
#define all(v) (v).begin(),(v).end()
const int INF = 10000;

int main()
{
	int h;
	while (scanf("%d", &h), h){
		VI v[5];
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < 5; j++){
				int a;
				scanf("%d", &a);
				v[j].PB(a);
			}
		}
		
		for (int i = 0; i < 5; i++){
			reverse(all(v[i]));
		}
		
		int res = 0;
		while (true){
			bool update = false;
			
			for (int i = 0; i < h; i++){
				int chain[5] = {1, 1, 1, 1, 1};
				for (int j = 1; j < 5; j++){
					if (v[j][i] == v[j - 1][i] && v[j][i] != INF) chain[j] = chain[j - 1] + 1;
				}
				for (int j = 4; j >= 2; j--){
					if (chain[j] >= 3){
						for (int k = j; k > j - chain[j]; k--){
							res += v[k][i];
							v[k][i] = INF;
						}
						update = true;
						break;
					}
				}
			}
			
			if (!update) break;
			
			for (int i = h - 1; i >= 0; i--){
				for (int j = 0; j < 5; j++){
					if (v[j][i] == INF){
						for (int k = i; k < h - 1; k++){
							swap(v[j][k], v[j][k + 1]);
						}
					}
				}
			}
			
			/*
			for (int i = h - 1; i >= 0; i--){
				for (int j = 0; j < 5; j++){
					if (v[j][i] == INF) printf("-1 ");
					else printf("%2d ", v[j][i]);
				}
				puts("");
			}
			puts("");
			*/
			
		}
		
		cout << res << endl;
	}
	
	return 0;
}