Rarity and New Dress
Contest: Codeforces Round 662 (Div. 2)
Difficulty rating: 2100
Tags: dfs and similar, dp, implementation, shortest paths

Carousel Boutique is busy again! Rarity has decided to visit the pony ball and she surely needs a new dress, because going out in the same dress several times is a sign of bad manners. First of all, she needs a dress pattern, which she is going to cut out from the rectangular piece of the multicolored fabric.

The piece of the multicolored fabric consists of $$$n \times m$$$ separate square scraps. Since Rarity likes dresses in style, a dress pattern must only include scraps sharing the same color. A dress pattern must be the square, and since Rarity is fond of rhombuses, the sides of a pattern must form a $$$45^{\circ}$$$ angle with sides of a piece of fabric (that way it will be resembling the traditional picture of a rhombus).

Examples of proper dress patterns:  Examples of improper dress patterns:  The first one consists of multi-colored scraps, the second one goes beyond the bounds of the piece of fabric, the third one is not a square with sides forming a $$$45^{\circ}$$$ angle with sides of the piece of fabric.

Rarity wonders how many ways to cut out a dress pattern that satisfies all the conditions that do exist. Please help her and satisfy her curiosity so she can continue working on her new masterpiece!

Input format:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2000$$$). Each of the next $$$n$$$ lines contains $$$m$$$ characters: lowercase English letters, the $$$j$$$-th of which corresponds to scrap in the current line and in the $$$j$$$-th column. Scraps having the same letter share the same color, scraps having different letters have different colors.

Output format:
Print a single integer: the number of ways to cut out a dress pattern to satisfy all of Rarity's conditions.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, all the dress patterns of size $$$1$$$ and one of size $$$2$$$ are satisfactory.

In the second example, only the dress patterns of size $$$1$$$ are satisfactory.

Example input:
3 3
aaa
aaa
aaa

Example output:
10

Example input:
3 4
abab
baba
abab

Example output:
12

Editorial:
Let's note that if there is a rhombus of size $$$X$$$ in the cell $$$(i, j)$$$, then there are also rhombuses with the smaller sizes.
Let's divide the rhombus into the left part and the right part. Let's solve the problem separately for both of them and then the answer for the cell is going to be equal to the minimum of these values.
Note that if the maximum size for the cell $$$(i, j)$$$ is $$$X$$$, then the maximum size for the cell $$$(i, j+1)$$$ is at most $$$X+1$$$ (and at most $$$1$$$, if these two cells are not equal). Also the maximums size for the left part is at most minimum of the number of consecutive cells to the up and to left from the fixed cell.
To find the number of consective equal cells to the up, we will use the dynamic programming. If the cells $$$(i, j)$$$ and $$$(i-1, j)$$$ are equal, then the answer for the cell $$$(i, j)$$$ is equal to the answer for the cell $$$(i-1, j)$$$ plus $$$1$$$, otherwise it is equal to $$$1$$$. Similarly we can find the answer for the cells to the left.
Now we need to find the maximum size of the left part. We can use dynamic programming and calculate it accordingly to our observations.
Similarly for the right part.
The total complexity is $$$O(nm)$$$.