Cash Cow

No attempts yetTime limit1sMemory limit128 MB

Problem

Cash Cow is a single player puzzle game from the Webkinz site. You clear the board by clicking on blobs of same colored circles.

The board has 12 rows and 10 columns. Rows are numbered 1 at the bottom through 12 at the top, and columns are named a at the left through j at the right. Every cell either holds one colored circle or is empty. Colors are written as uppercase letters, for example B for blue, R for red, and Y for yellow.

On each turn the player clicks one cell. The cluster of that cell is the set you get by starting at its circle and repeatedly taking every horizontally or vertically adjacent circle of the same color. Diagonals do not connect.

If the clicked cell is empty, or if its cluster holds only 1 or 2 circles, the turn is wasted and the board does not change. If the cluster holds 3 or more circles, every circle in the cluster is removed and the board is then compacted:

  1. Within each column, circles fall down to fill the holes.
  2. If any column has become empty, the remaining columns slide left, each keeping its order and its contents, until they are packed against the left edge of the board.

Coordinates always refer to the board as it stands at that moment. After a slide, c 5 can be a different circle than c 5 was before it.

Input

The input holds several games. Each game starts with a line containing the number of turns TT that the player will make. (1T201 \le T \le 20)

The next 12 lines give the initial board, each line holding 10 uppercase letters. The first of those lines is row 12 and the last one is row 1. The initial board never has an empty cell.

Then come TT lines, one click per line. Each line holds a lowercase column letter from a to j and a row number from 1 to 12, separated by a space. Clicking a cell that currently holds no circle wastes the turn.

The input ends with a line containing 0.

Output

For each game, print on one line the number of circles left on the board after all of the player's turns have been processed.