Puyo Puyo

Count how many chain reactions occur when groups of four or more same-color puyos pop and gravity settles the 12 by 6 field.

Medium4BFSSimulationInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

Puyo Puyo has the following rules.

You drop puyos of several colours onto the field. Gravity pulls each puyo straight down until it reaches the floor or another puyo.

After a puyo settles, any group of four or more puyos of the same colour connected up, down, left or right disappears all at once. That is when the first chain starts.

When puyos disappear, every puyo left above the emptied cells falls down under gravity as well.

If four or more puyos of the same colour end up connected again after that fall, they pop too. Each round of popping and falling raises the chain count by one.

If several groups can pop at the same time, they pop together, and the chain count still rises by only one.

Namgyu has been playing a lot of Puyo Puyo lately. It is a one on one game, so stacking well matters, but so does reading off how long the opponent's chain will run the moment it starts. Namgyu is still busy keeping his own field in order. Given the opponent's field, work out how many chains happen and help him out.

Input

The field is given on 12 lines, each holding 6 characters.

A . is an empty cell, and every character other than . is a puyo of one colour.

R is red, G is green, B is blue, P is purple and Y is yellow.

The field in the input is already settled, so no puyo has an empty cell below it.

Output

Print how many chains happen on the given field. If nothing pops at all, print 0.