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 <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <tuple>
#include <queue>
#include <set>
#include <cstdio>
#include <climits>
#include <cmath>
#include <array>
#include <functional>
#include <sstream>
#include <list>
#include <set>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;
struct Point
{
  double x,y;
  Point(){x=0;y=0;}
  Point(double d_x,double d_y){x=d_x,y=d_y;}
  double operator*(Point obj){return obj.x*x+obj.y*y;}
  double operator%(Point obj){return obj.y*x-obj.x*y;}
  Point operator*(double b){Point tmp;tmp.x=x*b;tmp.y=y*b;return tmp;}
  Point operator/(double b){Point tmp;tmp.x=x/b;tmp.y=y/b;return tmp;}
  Point operator+(Point obj){Point tmp;tmp.x=x+obj.x;tmp.y=y+obj.y;return tmp;}
  Point operator-(){Point tmp;tmp.x=-x;tmp.y=-y;return tmp;}
  Point operator-(Point obj){Point tmp;tmp.x=x-obj.x;tmp.y=y-obj.y;return tmp;}
  Point operator-=(Point obj){x-=obj.x;y-=obj.y;return *this;}
  Point operator+=(Point obj){x+=obj.x;y+=obj.y;return *this;}
  Point operator/=(double b){x=x/b;y=y/b;return *this;}
  Point operator*=(double b){x=x*b;y=y*b;return *this;}
  double size(){return hypot(x,y);}
  Point unit(){return Point(x/size(),y/size());}
  Point normal(){return Point(y,-x);}
  double atan(){return atan2(y,x);}
};

bool operator<(Point a,Point b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool operator>(Point a,Point b){return b<a;}
bool operator<=(Point a,Point b){return !(b<a);}
bool operator>=(Point a,Point b){return !(a<b);}
bool operator==(Point a,Point b){return (a-b).size()<eps;}
bool operator!=(Point a,Point b){return !(a==b);}

bool equal(double a,double b){return abs(a-b)<eps;}
double cross(Point a,Point b){return a%b;}
double dot(Point a,Point b){return a*b;}

int ccw(Point a,Point b,Point c)
{
  b=b-a;
  c=c-a;
  if(b%c>0) return +1;
  else if(b%c<0)return -1;
  else if(b*c<0) return +2;
  else if(b.size()<c.size()) return -2;
  else return 0;
}


int h;
int board[15][5];
int vanih[5];
int chain[10];


int main(int argc,char const* argv[])
{
  while(1)
    {
      memset(vanih,0,sizeof(vanih));
      int score=0;
      cin >> h;
      if(!h)break;
      bool end=false;
      for(int i=0;i<h;i++)
	{
	  for(int j=0;j<5;j++)
	    {
	      cin >> board[h-i-1][j];
	    }
	}
      while(!end)
	{
	  end=true;
	  memset(chain,0,sizeof(chain));
	  for(int i=0;i<h;i++)
	    {
	      for(int j=0;j<3;j++)
		{
		  if(board[i][j]!=-1 && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2])
		    {
		      end=false;
		      int d=board[i][j];
		      for(; board[i][j]==d && j<5;j++)
			{
			  board[i][j]=-1;
			  score+=d;
			}
		    }
		}
	    }

	  for(int j=0;j<5;j++)
	    {
	      for(int i=1;i<h;i++)
		{
		  if(board[i-1][j]==-1 && board[i][j]!=-1)
		    {
		      for(int row=i; row>0&&board[row-1][j]==-1;row--)
			{
			  swap(board[row-1][j],board[row][j]);
			}
		    }
		}
	    }
	}
      cout << score << endl;
    }
  return 0;
}

C++: #include<iostream>
using namespace std;
#define T t[i][j]
#define r(i,h) for(int i=0;i<h;i++)
int main(){
int h,a;
while(cin>>h,h){
a=0;
int t[200][5]={{0},{0}};
r(i,h)r(j,5)cin>>T;
while(1){
bool f=1;
r(i,h){
int c=0,q=-1;
r(j,3){
if(T==t[i][j+1]&&T==t[i][j+2]&&T!=0){
c++;
if(q==-1)q=j;
}
}
if(c!=0){a+=t[i][q]*(2+c),f=0;r(j,2+c)t[i][q+j]=0;}
}
if(f)break;
for(int i=h-2;i>=0;i--){
r(j,5){
int x=i;
while(true){
if(x==h-1||t[x+1][j]!=0)break;
t[x+1][j]=t[x][j];
t[x][j]=0;
x++;
}
}
}
}
cout<<a<<endl;
}
return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

int ban[10][5];

int turn(int score)
{
	int cpban[10][5];
	bool f=false;
	for(int i=0;i<10;i++)
	{
		for(int j=0;j<5;j++)
			cpban[i][j]=ban[i][j];
	}

	//消滅
	for(int i=0;i<10;i++)
	{
		int j=0;
		while(j<5)
		{
			if(ban[i][j]==0)
			{
				j++;
				continue;
			}else{
				int tmp=ban[i][j];
				int cnt=1;
				j++;
				while(j<5 && ban[i][j]==tmp)
				{
					cnt++;
					j++;
				}
				if(cnt>=3)
				{
					f=true;
					score+=cnt*tmp;
					for(int k=j-cnt;k<j;k++)
						ban[i][k]=0;
				}
			}
		}	
	}

	if(!f)
		return score;
	//落下
	for(int c=0;c<10;c++)
	{
		for(int j=0;j<5;j++)
		{
			for(int i=9;i>-1;i--)
			{
				if(ban[i][j]==0 && ban[i+1][j]>0)
				{
					ban[i][j]=ban[i+1][j];
					ban[i+1][j]=0;
				}
			}
		}
	}
	return turn(score);
}

int main()
{
	while(1)
	{
		fill(ban[0],ban[10],0);
		int h;
		cin>>h;
		if(h==0)
			break;
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<5;j++)
			{
				int t;
				cin>>t;
				ban[h-i-1][j]=t;
			}
		}

		int ans=turn(0);
		cout<<ans<<endl;
	}
	return 0;
}