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<stdio.h>

int block[10][5];
int p[10000];
void trace(int h);

void del(int h,int c) {
	int P2;
	while (1) {
		P2 = p[c];
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < 3; j++) {
				if (block[i][j] == block[i][j + 1] && block[i][j + 1] == block[i][j + 2]) {
					p[c] += block[i][j] * 3;
					if (j < 2) {
						if (block[i][j + 2] == block[i][j + 3]) {
							p[c] += block[i][j];
							if (j < 1) {
								if (block[i][j + 3] == block[i][j + 4]) {
									p[c] += block[i][j];
									for (int k = j; k < 5; k++) block[i][k] = 0;
									break;
								}
							}
							for (int k = j; k < j + 4; k++) block[i][k] = 0;
							break;
						}
					}
					for (int k = j; k < j + 3; k++) block[i][k] = 0;
					break;
				}
			}
		}
		if (P2 == p[c]) break;
		trace(h);
	}

}

void trace(int h) {
	
	for (int s = 0; s < h; s++) {
		for (int i = h - 1; i >= 0; i--) {
			for (int j = 0; j < 5; j++) {
				if (block[i][j] == 0) {
					block[i][j] = block[i - 1][j];
					block[i - 1][j] = 0;
				}
			}
		}
	}
	
}

int main() {
	int h;

	int n;
	int i, j;
	int c = 0;
	while (1) {
		for (i = 0; i < 10; i++) {
			for (j = 0; j < 5; j++) {
				block[i][j] = 0;
			}
		}
		
		scanf("%d", &h);
		if (h == 0) break;

		for (i = 0; i < h; i++) {
			for (j = 0; j < 5; j++) {
				scanf("%d", &n);
				block[i][j] = n;
			}
		}

		del(h,c);

		c++;
	}
	for (i = 0; i < c; i++) {
		printf("%d\n", p[i]);
	}
	return 0;
}

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

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

typedef long long LL;

int main(){
  int h;
  int count=0,ans=0;
  bool update=false;
  int now,num,pos;
  while(1){
    cin >> h;
    if(h==0){
      return 0;
    }
    vector<vector<int> > field = vector<vector<int> >(h,vector<int>(5,0));
    for(int i=0;i<h;i++){
      for(int j=0;j<5;j++){
        cin >> field[h-i-1][j];
      }
    }
    ans=0;
    pos=0;
    while(1){
      update=false;
      for(int i=0;i<h;i++){
        num=field[i][0];
        count=1;
        pos=0;
        for(int j=1;j<5;j++){
          if(num==field[i][j]){
            count++;
          }else{
            if(count<3){
              num=field[i][j];
              pos=j;
              count=1;
            }
          }
        }
        if(count>=3&&num!=0){
          ans+=num*count;
          for(int j=pos;j<pos+count;j++){
            field[i][j]=0;
          }
          update=true;
        }
      }
      if(!update){
        cout << ans << endl;
        break;
      }
      for(int j=0;j<5;j++){
        now=0;
        for(int i=0;i<h;i++){
          if(field[i][j]>0){
            field[now][j]=field[i][j];
            now++;
          }
        }
        for(int i=now;i<h;i++){
          field[i][j]=0;
        }
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int h;
    while(1){

        
        cin >> h;
        if(h == 0){
            break;
        }

        
        vector<vector<int>> stone;
        stone.resize(5);
        for(int i = 0; i < 5; i++){
            stone[i].resize(h);
        }
        for(int i = h-1; i >= 0; i--){
            for(int j = 0; j < 5; j++){
                cin >> stone[j][i];
            }
        }
        
        int old_score = 0;
        int score = 0;
        while(1){
            old_score = score;
            for(int i = 0; i < h; i++){
                for(int j = 0; j < 3; j++){
                    if(stone[j][i] == stone[j+1][i] && stone[j][i] == stone[j+2][i] && stone[j][i] != 0){
                        int old_stone_num = stone[j][i];
                        for(int k = 0; j + k < 5 && stone[j+k][i] == old_stone_num; k++){
                            score += stone[j+k][i];
                            stone[j+k][i] = 0;
                        }
                        break;
                    }
                }
            }
            
            if(old_score == score){
                break;
            }
        
            
            for(int i = 0; i < 5; i++){
                stone[i].erase(remove(stone[i].begin(), stone[i].end(), 0), stone[i].end());
                for(int j = stone[i].size(); j < h; j++){
                    stone[i][j] = 0;
                }
            }
            

            for(int i = h-1; i >= 0; i--){
                cout << endl;
                for(int j = 0; j < 5; j++){
                    cout << stone[j][i];
                }
            }
            cout << endl;

        }
        cout << score << endl;
    }
    return 0;
}