inputs: I found an interesting problem on https://codeforces.com/problemset/problem/1181/C:
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns. 

The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.

Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]

These subrectangles are flags.  [Image] [Image] [Image] [Image] [Image] [Image]

These subrectangles are not flags. 


-----Input-----

The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.

Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.


-----Output-----

In the only line print the number of subrectangles which form valid flags.


-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd

Output
6

Input
6 1
a
a
b
b
c
c

Output
1



-----Note----- [Image] [Image]

The selected subrectangles are flags in the first example.

I tried it in Python, but could not do it. Can you solve it?
targets: def countFlagNum(x):
  y = 0
  flagNum = 0
  while y < m:
    curColor = a[x][y]
    colorCountByX = 1
    isItFlag = True
    while x + colorCountByX < n and a[x + colorCountByX][y] == curColor:
      colorCountByX += 1
    if not(x + colorCountByX * 3 > n):
      color2 = a[x + colorCountByX][y]
      color3 = a[x + colorCountByX * 2][y]
      if color3 != color2 and color2 != curColor:
        offY = 0
        while y + offY < m and isItFlag:
          i = 0
          while i < colorCountByX and isItFlag:
            if (a[x + i][y + offY] != curColor or a[x + colorCountByX + i][y + offY] != color2 or a[x + colorCountByX * 2 + i][y + offY] != color3):
              isItFlag = False
              if offY == 0:
                offY = 1
            i += 1
          if isItFlag:
            flagNum = flagNum + 1 + offY
            offY += 1
        y += offY - 1
    y += 1
  return flagNum

n, m = list(map(int, input().split()))
a = []
totalFlagNum = 0

for i in range(n):
  row = input()
  a.append(row)

for i in range(n - 2):
    totalFlagNum += countFlagNum(i)

print(totalFlagNum)