Where's Bessie?

Given an N x N grid of colors (N up to 20), count the rectangular sub-grids where exactly two colors appear, one forming one connected region and the other forming two or more, and that are not contained in any other such rectangle.

Medium6ImplementationBrute forceDFSNo attempts yetTime limit2sMemory limit512 MB

Problem

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×NN \times 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.

Input

The first line contains NN, the size of the grid (1N201 \leq N \leq 20). Each of the next NN lines describes one row of the image and consists of NN characters, each an uppercase letter from A to Z.

Output

Print the number of PCLs in the image.

Hint

In the sample, the two PCLs are the rectangles with contents

ABB
BBB
AAB
ABB

and

BC
BC
BB
BC