function Main(input) {
	input = input.trim().split("\n").map(function(x) { return x.split(" ")});
	let l = 0;
	let count = 0;
	while (input[l][0] != '0'){
		let H = parseInt(input[l][0], 10);
		let arr = [];
		for (let i = 0; i < H; i++){
			arr.push(input[i + l + 1].map(e => parseInt(e, 10)));
		}

		//vanish
		let ENDflag;
		let ans = 0;
		while (true){
			ENDflag = true;
			let flag;
			let vanishList = Array.from({length: 5}, () => []);
			for (let i = 0; i < H; i++){
				flag = false;
				let tmpN = -1;
				let tmpIndex = [];
				this_loop:
				for (let j = 0; j < 5; j++){
					if (arr[i][j] === 0) {
						tmpN = -1;
						tmpIndex = [];
						continue;
					}
					else if	(arr[i][j] === tmpN) {
						tmpIndex.push(j);
						if (tmpIndex.length >= 3) {flag = true; ENDflag = false;}
					}
					else {
						if (flag) break this_loop;
						tmpN = arr[i][j];
						tmpIndex = [j];
					}
				}
				if (flag){
					for (let x of tmpIndex){
						// console.log(tmpIndex);
						ans += arr[i][x];
						arr[i][x] = -1;
						vanishList[x].push(i);
					}
				}
			}
			//fall
			let tmpFallDistance;
			for (let j = 0; j < 5; j ++){
				tmpFallDistance = 0;
				for (let i = H - 1; i >= 0; i--){
					while (i - tmpFallDistance > 0 && arr[i - tmpFallDistance][j] === -1){
						tmpFallDistance ++;
					}
					if (i - tmpFallDistance >= 0) arr[i][j] = Math.max(0, arr[i - tmpFallDistance][j]);
					else arr[i][j] = 0;
				}
			}
			if (ENDflag) break;
		}
		l += H + 1;
		console.log(ans);
		count ++;
	}
	
}


Main(require("fs").readFileSync("/dev/stdin", "utf8"));



