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(){
  for(int H;cin>>H,H!=0;){
    int sum = 0;
    vector<vector<int>> stone(5,vector<int> (H));

    // input
    for(int i=0;i<H;++i)
      for(int j=0;j<5;++j)
        cin>>stone[j][i];

    while(1){
      int count=0;

     /* for(int i=0;i<H;++i){
        for(int j=0;j<5;++j){
          cout<<stone[j][i];
        }
        cout<<"\n";
      }
      cout<<"\n";*/
      // count, and make stones 0
      for(int i=0;i<H;++i){ // Rows
        for(int j=1,num=1;j<5;++j){ // Columns
          if(stone[j-1][i]==stone[j][i]){ ++num; }
          else{
            if(num>=3){
              count += num * stone[j-1][i];
              // make stones 0
              for(int k=j-1;k>=j-num;--k) { stone[k][i]=0; }
            }
            num=1;
            if(j>3) { break; }
          }

          if(j==4&&num>=3){
            count += num * stone[j][i];
            for(int k=j;k>=j-num+1;--k) { stone[k][i]=0; }
            num=1;
          }
        }
      }

      if(count==0) { break; }

      sum += count;
     /* for(int i=0;i<H;++i){
        for(int j=0;j<5;++j){
          cout<<stone[j][i];
        }
        cout<<"\n";
      }
      cout<<"\n";
*/
      // fall
      for(int j=0;j<5;++j){
        for(int i=H-1;i>=0;--i){
          if(stone[j][i]==0) { 
            for(int k=i-1;k>=0;--k){
              if(stone[j][k]!=0){
                swap(stone[j][k], stone[j][i]); 
                break;
              }
            }
          }
        }
      }


 /*     for(int i=0;i<H;++i){
        for(int j=0;j<5;++j){
          cout<<stone[j][i];
        }
        cout<<"\n";
      }
      cout<<"\n";
      cout<<"sum = "<<sum<<"\n";*/
    }
    printf("%d\n",sum);
  }
  return EXIT_SUCCESS;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a)
{
    return is >> a.first >> a.second;
}
ostream &operator<<(ostream &os, const P &a) { return os << "(" << a.first << "," << a.second << ")"; }

template <typename T>
void view(const std::vector<T> &v)
{
    for (const auto &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        view(v);
    }
}
#pragma endregion

int popRow(vvint &g, int y, int w = 5)
{
    vint row = g[y];
    int score = 0;

    int li = 0, l = row[0];
    rep(i, w)
    {
        if (row[i] != l)
        {
            l = row[i];
            li = i;
            continue;
        }

        if (i - li >= 2)
        {
            for (int j = i; j >= li && g[y][j]; j--)
            {
                score += g[y][j];
                g[y][j] = 0;
            }
        }
    }

    return score;
}

int pop(vvint &g, int h, int w = 5)
{
    int score = 0;
    rep(y, h)
    {
        score += popRow(g, y);
    }
    return score;
}

void shift(vvint &g, int h, int w = 5)
{
    for (int y = h - 2; y >= 0; y--)
        rep(x, 5)
        {
            int d = 1;
            while (y + d < h && g[y + d][x] == 0)
            {
                g[y + d][x] = g[y + d - 1][x];
                g[y + d - 1][x] = 0;
                d++;
            }
        }
}

int main()
{
    int h, w = 5;
    while (cin >> h, h)
    {
        vvint g(h, vint(w));
        rep(i, h) rep(j, w) cin >> g[i][j];

        int ans = 0;
        while (true)
        {
            int newScore = pop(g, h, w);
            ans += newScore;
            shift(g, h);

            if (newScore == 0)
                break;
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int h;
  while(cin>>h,h){
    int a[10][5],ans=0;
    for(int i=0;i<h;i++)
      for(int j=0;j<5;j++)cin>>a[i][j];
    bool f=1;
    while(f){
      f=0;
      for(int i=0;i<h;i++){
	int c=1,p=0;
	for(int j=1;j<5;j++)
	  if(a[i][p]==a[i][j])c++;
	  else if(c<3)c=1,p=j;
	  else {
	    ans+=c*a[i][p];
	    for(int k=0;k<c;k++)a[i][p+k]=0;
	    break;
	  }
      }
      for(int i=0;i<5;i++)
	for(int j=1;j<h;j++)
	  for(int k=j;k>0;k--)
	    if(!a[k][i]&&a[k-1][i])swap(a[k][i],a[k-1][i]),f=1;
    }
    cout<<ans<<endl;
  }
  return 0;
}