Farmer John has always been known as tech-savvy, and he is testing his new automated drone-mounted cow locator camera. The camera is supposed to take a picture of his field and work out the location of cows on its own. Unfortunately, the camera does not come with a very good algorithm for finding cows, so FJ needs your help to develop a better one.
The overhead image of his farm taken by the camera is an N×N grid of characters. Each character is in the range A to Z and represents one of 26 possible colors. Farmer John defines a potential cow location (PCL) as follows. A PCL is a rectangular sub-grid (possibly the entire image) with sides parallel to the image sides, and it is not contained within any other PCL. In other words, no smaller sub-rectangle of a PCL is also a PCL. Furthermore, a PCL must satisfy the following property: looking only at the contents of the rectangle and ignoring the rest of the image, exactly two colors are present, one color forms a single contiguous region, and the other color forms two or more contiguous regions.
For example, a rectangle with contents
AAAAA
ABABA
AAABB
is a PCL, since the A cells form a single contiguous region and the B cells form more than one contiguous region. The interpretation is a cow of color A with spots of color B.
A region is "contiguous" if you can visit the entire region by repeatedly stepping from one cell of the region to another cell of the region, moving up, down, left, or right.
Given the image returned by FJ's camera, count the number of PCLs.