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() {
	while(1){
		int h, w{10}, cnt{}; cin >> h;
		if(!h) break;
		vector<vector<int>> f(2*h,vector<int>(5));
		for(int i=h-1; i>=0; i--)for(int j=0; j<5; j++) cin >> f.at(i).at(j);
		for(int i=h; i<2*h; i++) {
			for(int j=0; j<5; j++){
				f.at(i).at(j) = w;
				w++;
			}
		}
		vector<vector<int>> a1(h,vector<int>(5));
		for(int i=0; i<h; i++)for(int j=0; j<5; j++) a1.at(i).at(j) = i;
		bool flg{1};
		while(flg){
			auto a = a1;
			flg=0;
			for(int i=h-1; i>=0; i--){
				int f0 = f.at(a.at(i).at(0)).at(0), f1 = f.at(a.at(i).at(1)).at(1), f2 = f.at(a.at(i).at(2)).at(2), f3 = f.at(a.at(i).at(3)).at(3), f4 = f.at(a.at(i).at(4)).at(4);
				if(f2!=f1&&f2==f3){
					if(f3==f4){
						flg=1;
						cnt += f2*3;
						for(int k=i; k<h; k++) for(int l=2; l<5; l++) {
							if(k==h-1)a1.at(k).at(l)++;
							else a1.at(k).at(l) = a1.at(k+1).at(l);
						}
					}
				}
				if(f2==f1&&f2!=f3){
					if(f1==f0){
						flg=1;
						cnt += f2*3;
						for(int k=i; k<h; k++) for(int l=0; l<3; l++) {
							if(k==h-1)a1.at(k).at(l)++;
							else a1.at(k).at(l) = a1.at(k+1).at(l);
						}
					}
				}
				if(f2==f1&&f2==f3){
					flg=1;
					int b{3}, left{1}, right{3};
					if(f0==f1) {
						b++;
						left--;
					}
					if(f3==f4){
						b++;
						right++;
					}
					cnt += f2*b;
					for(int k=i; k<h; k++) for(int l=left; l<=right; l++) {
						if(k==h-1)a1.at(k).at(l)++;
						else a1.at(k).at(l) = a1.at(k+1).at(l);
					}
				}
			}
		}
		cout << cnt << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int h,ans;
  int map[10][7];
  bool b;
  while(1){
    cin>>h;
  if(h==0) break;
  b=true;
  ans=0;
  for(int i=0;i<10;i++){
    for(int j=0;j<5;j++){
      map[i][j]=0;
    }
  }
  for(int i=0;i<h;i++){
    for(int j=0;j<5;j++){
      cin>>map[i][j];
    }
  }
  while(b){
    b=false;
  for(int i=0;i<h;i++){
    for(int j=0;j<3;j++){
      if(map[i][j]==0) continue;
      if(map[i][j]==map[i][j+1] && map[i][j]==map[i][j+2]){
	b=true;
	if(map[i][j]==map[i][j+3]){
	  if(map[i][j]==map[i][j+4]){
	    for(int k=j;k<j+5;k++){
	       ans+=map[i][k];
	       map[i][k]=0;
	    }
	  }
	  else for(int k=j;k<j+4;k++){
	      ans+=map[i][k];
	      map[i][k]=0;
	    }
	}
	else for(int k=j;k<j+3;k++){
	    ans+=map[i][k];
	      map[i][k]=0;
	  }
      }
    }
  }
  for(int i=0;i<h;i++){
    for(int j=0;j<5;j++){
      if(map[i][j]==0){
	for(int k=i;k>=0;k--){
	  if(k==0) map[k][j]=0;
	  else map[k][j]=map[k-1][j];
	}
      }
    }
  }
  /*  for(int i=0;i<h;i++){
    for(int j=0;j<5;j++){
      cout<<map[i][j]<<" ";
    }
    cout<<endl;
    }*/
  }
  cout<<ans<<endl;
  }
  return 0;
}

C++: // 2016/04/27 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int H;
		cin >> H;

		if(H==0)
			break;

		int S[10][5];
		for(int i=0; i<H; i++){
			for(int j=0; j<5; j++){
				cin >> S[i][j];
			}
		}

		int sco = 0;

	while(true){
		bool flg = false;

		for(int i=0; i<H; i++){
			if(S[i][2]==0)
				continue;

			if(S[i][2]==S[i][0] && S[i][2]==S[i][1] && S[i][2]==S[i][3] && S[i][2]==S[i][4]){
				sco += (S[i][2]*5);
				S[i][2] = S[i][0] = S[i][1] = S[i][3] = S[i][4] = 0;
				flg = true;
			}
			else if(S[i][2]==S[i][0] && S[i][2]==S[i][1] && S[i][2]==S[i][3]){
				sco += (S[i][2]*4);
				S[i][2] = S[i][0] = S[i][1] = S[i][3] = 0;
				flg = true;
			}
			else if(S[i][2]==S[i][1] && S[i][2]==S[i][3] && S[i][2]==S[i][4]){
				sco += (S[i][2]*4);
				S[i][2] = S[i][1] = S[i][3] = S[i][4] = 0;
				flg = true;
			}
			else if(S[i][2]==S[i][0] && S[i][2]==S[i][1]){
				sco += (S[i][2]*3);
				S[i][2] = S[i][0] = S[i][1] = 0;
				flg = true;
			}
			else if(S[i][2]==S[i][1] && S[i][2]==S[i][3]){
				sco += (S[i][2]*3);
				S[i][2] = S[i][1] = S[i][3] = 0;
				flg = true;
			}
			else if(S[i][2]==S[i][3] && S[i][2]==S[i][4]){
				sco += (S[i][2]*3);
				S[i][2] = S[i][3] = S[i][4] = 0;
				flg = true;
			}
		}

		if(!flg)
			break;

		for(int j=0; j<5; j++){
			for(int i=H-1; i>=0; i--){
				if(S[i][j]!=0)
					continue;

				for(int k=i-1; k>=0; k--){
					if(S[k][j]!=0){
						S[i][j] = S[k][j];
						S[k][j] = 0;
						break;
					}
				}
			}
		}
/*
		for(int i=0; i<H; i++){
			for(int j=0; j<5; j++){
				cout << S[i][j];
			}
			cout << endl;
		}
*/
	}

		cout << sco << endl;
	}

	return 0;
}