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)
}

Python: while True:
    H = int(input())
    if H == 0:
        exit()
    L = [input().split() for _ in range(H)]
    L1 = list(reversed([l[0] for l in L]))
    L2 = list(reversed([l[1] for l in L]))
    L3 = list(reversed([l[2] for l in L]))
    L4 = list(reversed([l[3] for l in L]))
    L5 = list(reversed([l[4] for l in L]))
    ans = 0
    while True:
        changed = False
        for i in range(H):
            if L1[i] != 'X' and L1[i] == L2[i] == L3[i] == L4[i] == L5[i]:
                changed = True
                ans += 5*int(L1[i])
                L1[i] = 'X'
                L2[i] = 'X'
                L3[i] = 'X'
                L4[i] = 'X'
                L5[i] = 'X'
            elif L1[i] != 'X' and L1[i] == L2[i] == L3[i] == L4[i]:
                changed = True
                ans += 4*int(L1[i])
                L1[i] = 'X'
                L2[i] = 'X'
                L3[i] = 'X'
                L4[i] = 'X'
            elif L2[i] != 'X' and L2[i] == L3[i] == L4[i] == L5[i]:
                changed = True
                ans += 4*int(L2[i])
                L2[i] = 'X'
                L3[i] = 'X'
                L4[i] = 'X'
                L5[i] = 'X'
            elif L1[i] != 'X' and L1[i] == L2[i] == L3[i]:
                changed = True
                ans += 3*int(L1[i])
                L1[i] = 'X'
                L2[i] = 'X'
                L3[i] = 'X'
            elif L2[i] != 'X' and L2[i] == L3[i] == L4[i]:
                changed = True
                ans += 3*int(L2[i])
                L2[i] = 'X'
                L3[i] = 'X'
                L4[i] = 'X'
            elif L3[i] != 'X' and L3[i] == L4[i] == L5[i]:
                changed = True
                ans += 3*int(L3[i])
                L3[i] = 'X'
                L4[i] = 'X'
                L5[i] = 'X'
        if not changed:
            break
        L1 = list(filter(lambda x: x!='X',L1)) + ['X']*L1.count('X')
        L2 = list(filter(lambda x: x!='X',L2)) + ['X']*L2.count('X')
        L3 = list(filter(lambda x: x!='X',L3)) + ['X']*L3.count('X')
        L4 = list(filter(lambda x: x!='X',L4)) + ['X']*L4.count('X')
        L5 = list(filter(lambda x: x!='X',L5)) + ['X']*L5.count('X')
    print(ans)

Python: while True:
  lst=[]
  pt=0
  H=int(input()) 
  for i in range(H):
   lst.append(list(map(int,input().split())))
  for s in lst:
    for s2 in range(0,3):
     if s[s2] != s and s[s2] == s[s2+1] == s[s2+2]:
       x=s[s2]
       pt += s[s2]*s.count(x)
       for h in range(0,s.count(x)):
         s[s.index(x)]=0
       for g in lst:
         for f in range(5):
           try:
             if g[f]==0:
                 lst.reverse()
                 g[f]=lst[lst.index(g)-1][f]
                 ls[ls.index(g)-1]
             else:
               pass
           except:
             break
  print(pt)

Python: a = int(input())
global s
s = 0

def de(ppp,h):
	re = False
	co  = 0
	global s
	for lo in ppp:
		for j ,cell in enumerate(lo):
			k = j
			p = j
			if k <= 2:
				while cell == lo[k+1] and cell != None:
					co += 1
					if k == 3:
						j = k
						break
					k += 1
					j = k
				if co >= 2:
					re = True
					s += (co+1)*cell
					for l in range(5):
						if l >= p and l <= k:
							lo[l] = None
				co = 0

	return re



def mo(ppp,h):
	for i ,roe in enumerate(ppp):
		for j , cell in enumerate(roe):
			kkk = i
			while i != 0 and ppp[kkk-1][j] == None:
				ppp[kkk-1][j] = ppp[kkk][j]
				ppp[kkk][j] = None
				if kkk == 1:
					break
				kkk -= 1


while a != 0:
	ppp = [list(map(int,input().split())) for i in range(a)]
	ppp.reverse()
	while(de(ppp,a) == True):
		mo(ppp,a)
	print(s)

	s = 0

	a = int(input())

Python: H = int(input())

while H:
  Ali = [list(map(int,input().split())) for _ in range(H)]
  score = 0
  while True:
    sc = score
    for i in range(H):
      Row = Ali[i]
      #5つ
      if Row[0] == Row[1] == Row[2] == Row[3] == Row[4]:
        score += Row[0]*5
        Ali[i][0] = Ali[i][1] = Ali[i][2] = Ali[i][3] = Ali[i][4] = 0
      
      #4つ
      if Row[0] == Row[1] == Row[2] == Row[3]:
        score += Row[0]*4
        Ali[i][0] = Ali[i][1] = Ali[i][2] = Ali[i][3] = 0
      
      if Row[1] == Row[2] == Row[3] == Row[4]:
        score += Row[1]*4
        Ali[i][1] = Ali[i][2] = Ali[i][3] = Ali[i][4] = 0
      
    #3つ
      if Row[0] == Row[1] == Row[2]:
        score += Row[0]*3
        Ali[i][0] = Ali[i][1] = Ali[i][2] = 0
      
      if Row[1] == Row[2] == Row[3]:
        score += Row[1]*3
        Ali[i][1] = Ali[i][2] = Ali[i][3] = 0
      
      if Row[2] == Row[3] == Row[4]:
        score += Row[2]*3
        Ali[i][2] = Ali[i][3] = Ali[i][4] = 0
        
    if sc == score:
      break
      
    #落としていく処理
    #下から上に見ていく
    for i in range(1,H+1):
      for j in range(0,5):
        if Ali[-i][j] == 0:
          for k in range(i+1,H+1):
            if Ali[-k][j] != 0:
              Ali[-i][j] = Ali[-k][j]
              Ali[-k][j] = 0
              break
              
  print(score)
  H = int(input())