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;
int main(){
  int H;
  char mas[10][5];
  while( cin >> H, H){
    for(int i = 0; i < H; i++){
      for(int j = 0; j < 5; j++){
        cin >> mas[i][j];
      }
    }

    int ret = 0;
    bool changed = true;

    for(int foo = 0; foo < 7; foo++){
      
      for(int q = 0; q < 7; q++){
        for(int i = H - 1; i > 0; i--){
          for(int j = 0; j < 5; j++){
            if(mas[i][j] == '*'){
              swap(mas[i][j],mas[i - 1][j]); //上から持ってくる
            }
          }
        }
      }

      for(int i = 0; i < H; i++){
        char prev = mas[i][0];
        int cnt = 0;
        for(int j = 0; j < 5; j++){
          if(mas[i][j] == '*'){
            cnt = 0;
            continue;
          }
          if(mas[i][j] != prev){
            prev = mas[i][j];
            cnt = 0;
          }
          cnt++;
          if(cnt == 3){
            ret += (mas[i][j] - '0') * 3;
            for(int k = j, l = 0; l < cnt; l++, k--){
              mas[i][k] = '*';
            }
          } else if(cnt > 3){
            ret += (mas[i][j] - '0');
            mas[i][j] = '*';
          }
        }
      }
    }
    cout << ret << endl;
  }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<math.h>
#include<vector>

using namespace std;

 
int main(){

	int H;
	while(1){int X[11][11]={};
		cin>>H;if(H==0)break;
		
		int sum=0;

		for(int i=H-1;i>=0;i--)
			for(int j=0;j<5;j++)
				cin>>X[i][j];


		bool f=false;

		while(1){
			int Y=sum;
				for(int i=H-1;i>=0;i--){int p=0,r=1;
					for(int j=0;j<5;j++)
					{
						if(p!=X[i][j]){
							p=X[i][j];r=1;
						}

						else if(p==X[i][j]){
							r++;
							if(r==3){sum+=3*p;//cout<<p<<X[i][j];
								X[i][j-2]=0;X[i][j-1]=0;X[i][j]=0;
							}
							if(r>3){X[i][j]=0;sum+=p;}
						}
					}
				}
				for(int j=0;j<5;j++)
					for(int i=0;i<H;i++)
					{
						if(X[i][j]==0){
							for(int I=i+1;I<H;I++)
								if(X[I][j]>0){X[i][j]=X[I][j];X[I][j]=0;break;
							}
						}
					}
					/*
				for(int i=H-1;i>=0;i--){cout<<endl;
				for(int j=0;j<5;j++)cout<<X[i][j];
				}
			*/

					if(Y==sum)break;
		}
		cout<<sum<<endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
while(1){
int h;
cin>>h;
if(h==0)
break;
int x[h][5];
for(int i=0;i<h;i++){
for(int j=0;j<5;j++){
cin>>x[i][j];
}
}
int sum=0;
while(1){
int flag=0;

for(int i=0;i<h;i++){
if(x[i][0]==x[i][1]&&x[i][1]==x[i][2]&&x[i][2]==x[i][3]&&x[i][3]==x[i][4]&&x[i][0]!=0){
flag=1;
sum+=x[i][0]*5;
x[i][0]=0;
x[i][1]=0;
x[i][2]=0;
x[i][3]=0;
x[i][4]=0;
}
else if(x[i][0]==x[i][1]&&x[i][1]==x[i][2]&&x[i][2]==x[i][3]&&x[i][0]!=0){
flag=1;
sum+=x[i][0]*4;
x[i][0]=0;
x[i][1]=0;
x[i][2]=0;
x[i][3]=0;
}
else if(x[i][3]==x[i][4]&&x[i][1]==x[i][2]&&x[i][2]==x[i][3]&&x[i][3]!=0){
flag=1;
sum+=x[i][1]*4;
x[i][4]=0;
x[i][1]=0;
x[i][2]=0;
x[i][3]=0;
}
else if(x[i][0]==x[i][1]&&x[i][1]==x[i][2]&&x[i][0]!=0){
flag=1;
sum+=x[i][0]*3;
x[i][0]=0;
x[i][1]=0;
x[i][2]=0;
}else if(x[i][2]==x[i][3]&&x[i][1]==x[i][2]&&x[i][1]!=0){
flag=1;
sum+=x[i][1]*3;
x[i][3]=0;
x[i][1]=0;
x[i][2]=0;
}else if(x[i][2]==x[i][3]&&x[i][3]==x[i][4]&&x[i][4]!=0){
flag=1;
sum+=x[i][2]*3;
x[i][3]=0;
x[i][4]=0;
x[i][2]=0;
}

}

if(flag==0)break;

int x1[h];
int x2[h];
int x3[h];
int x4[h];
int x5[h];
int len1=0;
int len2=0;
int len3=0;
int len4=0;
int len5=0;
for(int i=h-1;i>=0;i--){
if(x[i][0]!=0){
x1[len1]=x[i][0];
len1++;
}
if(x[i][1]!=0){
x2[len2]=x[i][1];
len2++;
}
if(x[i][2]!=0){
x3[len3]=x[i][2];
len3++;
}
if(x[i][3]!=0){
x4[len4]=x[i][3];
len4++;
}
if(x[i][4]!=0){
x5[len5]=x[i][4];
len5++;
}
}
for(int j=len1;j<h;j++){
x1[j]=0;
}
for(int j=len2;j<h;j++){
x2[j]=0;
}
for(int j=len3;j<h;j++){
x3[j]=0;
}
for(int j=len4;j<h;j++){
x4[j]=0;
}
for(int j=len5;j<h;j++){
x5[j]=0;
}
for(int i=0;i<h;i++){
x[i][0]=x1[h-i-1];
x[i][1]=x2[h-i-1];
x[i][2]=x3[h-i-1];
x[i][3]=x4[h-i-1];
x[i][4]=x5[h-i-1];
}
}
cout<<sum<<endl;
}
return 0;
}