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<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

int H;
int c[10][5];

int main() {

  while(true) {
    cin >> H;
    if(H == 0) break;
    rep(i, H) {
      rep(j, 5) {
        cin >> c[i][j];
      }
    }

    int ans = 0;

    while(true) {
      vector<pair<pair<int, int>, int>> mem;
      rep(i, H) {
        rep(j, 3) {
          if(c[i][j] == 0) continue;
          bool ff = false;
          for(int k = 5 - j; k >= 3; k--) {
            bool f = true;
            rep(l, k - 1) {
              if(c[i][j+l] != c[i][j+l+1]) {
                f = false;
              }
            }
            if(f) {
              mem.push_back({{i, j}, k});
              ans += k * c[i][j];
              ff = true;
            }
            if(ff) {
              j = j + k - 1;
              break;
            }
          }
          if(ff) break;
        }
      }

      if(mem.size() == 0) {
        cout << ans << endl;
        break;
      }

      rep(i, mem.size()) {
        // cout << mem[i].first.first << " " << mem[i].first.second << " " << mem[i].second << endl;
        int sx, sy, k;
        sx = mem[i].first.first;
        sy = mem[i].first.second;
        k = mem[i].second;
        // cout << sx << " " << sy << " " << k << endl;
        if(sx == 0) {
          rep(j, k) {
            c[sx][sy+j] = 0;
        }
        } else {
          for(int j = 1; j <= sx; j++) {
            rep(l, k) {
              swap(c[0][sy+l], c[j][sy+l]);
            }
          }
          rep(j, k) {
            c[0][sy+j] = 0;
          }
        }
      }

    }

    // rep(i, H) {
    //   rep(j, 5) {
    //     cout << c[i][j] << " ";
    //   }
    //   cout << endl;
    // }

  }

  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set;
using std::queue;
using std::priority_queue;
using std::stack;

typedef vector<int> VI;
typedef vector<vector<int> > VII;
typedef vector<string> VS;
typedef pair<int, int> PII;

int main(void) {
    int h;
    while (cin >> h) {
        if (h == 0) {
            break; 
        }
        VII table(h, VI(5));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < 5; j++) {
                cin >> table[i][j];
            }
        }

        int score = 0;
        while (1) {
            bool change = false;
            for (int i = 0; i < h; i++) {
                int j = 0;
                while (j < 3) {
                    int k;
                    for (k = j + 1; k < 5; k++) {
                        if (table[i][k] != table[i][j]) {
                            break;
                        }
                    }
                    if (k - j > 2 && table[i][j] != 0) {
                        score += (k - j) * table[i][j];
                        for (int l = j; l < k; l++) {
                            table[i][l] = 0;  
                        }
                        change = true;
                    }
                    j = k;
                }
            }

            if (! change) {
                break;
            }
            for (int k = 0; k < h; k++) {
                for (int i = h - 1; i > 0; i--) {
                    for (int j = 0; j < 5; j++) {
                        if (table[i][j] == 0) {
                            table[i][j] = table[i-1][j];
                            table[i-1][j] = 0;
                        }
                    }
                }
            }
        }
        cout << score << endl;
    }
    return 0;
}