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;
  while (cin >> H, H > 0) {
    vector<vector<int>> stones(H, vector<int>(5, 0));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < 5; j++) {
        cin >> stones[i][j];
      }
    }
    int ans = 0;
    while (true) {
      vector<vector<bool>> used(H, vector<bool>(5, false));
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < 5; j++) {
          int cnt = 0;
          int pv = stones[i][j];
          if (pv == 0) continue;
          for (int k = j; k < 5; k++) {
            if (stones[i][k] != pv) break;
            cnt++;
          }
          if (cnt < 3) continue;
          ans += pv * cnt;
          for (int k = j; k < 5; k++) {
            if (stones[i][k] != pv) break;
            used[i][k] = true;
            stones[i][k] = 0;
          }
        }
      }
      bool flag = false;
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < 5; j++) {
          if (used[i][j]) flag = true;
        }
      }
      if (!flag) break;
      for (int j = 0; j < 5; j++) {
        for (int i = H - 1; i >= 0; i--) {
          if (used[i][j]) continue;
          for (int k = i + 1; k < H; k++) {
            if (stones[k][j] == 0) swap(stones[k - 1][j], stones[k][j]);
          }
        }
      }
    }
    cout << ans << endl;
  }
}

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

using namespace std;
int res;
vector<string> del(vector<string> s){
  for(int i = 0 ; i < s.size() ; i ++){
    if(s[i][0] != 'A' && s[i][0] == s[i][1] && s[i][0] == s[i][3] && s[i][0] == s[i][4] && s[i][0] == s[i][2]){
      res+=5*(s[i][0]-'0');s[i][0] = s[i][1] = s[i][2] = s[i][3] = s[i][4] = 'A';
    }
    if(s[i][0] != 'A' && s[i][0] == s[i][1] && s[i][0] == s[i][3] && s[i][0] == s[i][2]){
      res+=4*(s[i][0]-'0');s[i][0] = s[i][1] = s[i][2] = s[i][3] = 'A';
    }
    if(s[i][1] != 'A' && s[i][1] == s[i][3] && s[i][1] == s[i][4] && s[i][1] == s[i][2]){
      res+=4*(s[i][1]-'0');s[i][1] = s[i][2] = s[i][3] = s[i][4] = 'A';
    }
    if(s[i][0] != 'A' && s[i][0] == s[i][1] && s[i][0] == s[i][2]){res+=3*(s[i][0]-'0');s[i][0] = s[i][1] = s[i][2] = 'A';}
    if(s[i][1] != 'A' && s[i][3] == s[i][1] && s[i][3] == s[i][2]){res+=3*(s[i][1]-'0');s[i][3] = s[i][1] = s[i][2] = 'A';}
    if(s[i][2] != 'A' && s[i][4] == s[i][3] && s[i][4] == s[i][2]){res+=3*(s[i][2]-'0');s[i][4] = s[i][3] = s[i][2] = 'A';}
  }
  return s;
}

vector<string> move(vector<string> s){
  for(int i = s.size()-1; i > 0 ; i --){
    for(int j = 0 ; j < 5 ; j ++){
      //cout<<s[i][j];
      if(s[i][j] == 'A'){
	//cout<<"c";
	swap(s[i][j],s[i-1][j]);
      }
    } 
    //cout<<endl;
  }
  return s;
  
}

int main(void){
  int n;
  while(cin>>n,n){

    res = 0;
    vector<string> s(n);
    for(int i = 0 ; i < n ;  i++){
      for(int j = 0 ; j < 5 ; j ++){
	char c;
	cin>>c;
	s[i]+=c;
      }
    }
    vector<string> last;
    while(last!=s){
      last=s;
      s = del(s);
      /*
      cout<<"del "<<res<<endl;
      for(int i= 0 ; i< s.size() ; i ++){
	cout<<s[i]<<endl;;
      }
      cout<<"move"<<endl;
      //*/
      s = move(s);
      s = move(s);
      /*
      for(int i= 0 ; i< s.size() ; i ++){
	cout<<s[i]<<endl;;
      }
      //*/
    }
    /*
    cout<<"hellO"<<endl;
    for(int i = 0 ; i < last.size() ; i ++)cout<<last[i]<<endl;
    //*/
    cout<<res<<endl;
  }  
}

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;
            }else{
              break;
            }
          }
        }
        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;
}