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;

typedef long long ll;

int main()
{
    ll i,j;
    while(true){
        ll h;
        cin >> h;
        if(h == 0){
            return 0;
        }
        vector<vector<ll>> ban(h, vector<ll>(5,0));
        for(i = h-1;i >= 0;--i){
            for(j = 0;j < 5;++j){
                cin >> ban.at(i).at(j);
            }
        }
        ll ans = 0;
        while(true){
            ll tmp_ans = ans;
            for(i = 0;i < h;++i){
                vector<pair<ll,ll>> vp;
                ll tmp = ban.at(i).at(0);
                ll num = 1;
                for(j = 1;j < 5;++j){
                    if(tmp != ban.at(i).at(j)){
                        vp.emplace_back(tmp,num);
                        tmp = ban.at(i).at(j);
                        num = 1;
                    }else{
                        ++num;
                    }
                }
                vp.emplace_back(tmp,num);
                if(vp.size() >= 4){
                    continue;
                }
                if(vp.size() == 1){
                    ans += ban.at(i).at(0)*5;
                    for(j = 0;j < 5;++j){
                        ban.at(i).at(j) = 0;
                    }
                }else if(vp.size() == 2){
                    if(vp.at(0).second >= 3){
                        ans += vp.at(0).first*vp.at(0).second;
                        for(j = 0;j < vp.at(0).second;++j){
                            ban.at(i).at(j) = 0;
                        }
                    }else if(vp.at(1).second >= 3){
                        ans += vp.at(1).first*vp.at(1).second;
                        for(j = vp.at(0).second;j < 5;++j){
                            ban.at(i).at(j) = 0;
                        }
                    }
                }else if(vp.size() == 3){
                    if(vp.at(0).second == 3){
                        ans += vp.at(0).first*vp.at(0).second;
                        for(j = 0;j < vp.at(0).second;++j){
                            ban.at(i).at(j) = 0;
                        }
                    }else if(vp.at(1).second == 3){
                        ans += vp.at(1).first*vp.at(1).second;
                        for(j = vp.at(0).second;j < vp.at(0).second + vp.at(1).second;++j){
                            ban.at(i).at(j) = 0;
                        }
                    }
                    else if(vp.at(2).second == 3){
                        ans += vp.at(2).first*vp.at(2).second;
                        for(j = vp.at(0).second + vp.at(1).second;j < 5;++j){
                            ban.at(i).at(j) = 0;
                        }
                    }
                }
            }
            if(ans == tmp_ans){
                break;
            }
            for(i = 0;i < 5;++i){
                vector<ll> tmp;
                for(j = 0;j < h;++j){
                    if(ban.at(j).at(i) != 0){
                        tmp.push_back(ban.at(j).at(i));
                    }
                }
                for(j = 0;j < tmp.size();++j){
                    ban.at(j).at(i) = tmp.at(j);
                }
                for(j = tmp.size();j < h;++j){
                    ban.at(j).at(i) = 0;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h;
  while(cin>>h,h) {
    int in[h][5];
    FOR(i,0,h) {
      FOR(j,0,5) {
        cin >> in[i][j];
      }
    }
    vector<queue<int> > Q(5);
    for(int i = h - 1; i >= 0; i--) {
      FOR(j,0,5) {
        Q[j].push(in[i][j]);
      }
    }
    // ?¶??????????0
    bool end = true;
    int ans = 0;
    FOR(x,0,6) {
      // ?¶?????????????0
      int num[h][5];
      FOR(i,0,h) {
        FOR(j,0,5) {
          num[i][j] = Q[j].empty() ? 0 : Q[j].front();
          if(!Q[j].empty()) Q[j].pop();
        }
      }
      FOR(i,0,h) {
        int cnt = 1;
        FOR(j,0,4) {
          if(num[i][j] != 0 && num[i][j] == num[i][j+1]) cnt++;
          else {
            if(cnt >= 3) {
              end = false;
              int k = j;
              while(cnt--) {
                ans += num[i][k];
                num[i][k] = 0;
                k--;
              }
            }
            cnt = 1;
          }
        }
        if(cnt >= 3) {
          end = false;
          int k = 4;
          while(cnt--) {
            ans += num[i][k];
            num[i][k] = 0;
            k--;
          }
        }
      }
      if(end) break;
      // ????????????
      FOR(i,0,h) FOR(j,0,5) {
        if(num[i][j] != 0) Q[j].push(num[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}