This page is still under construction.

Parts of this page are still being built. What you see may change.

Red Rectangles

Interview

Time limit1sMemory limit512 MB

Summary
Count the subrectangles of an N by M red and blue grid that contain only red cells.
Level

Medium6 of 10

Topics
Stack, Dynamic programming, Matrix
Solved
No attempts yet

Problem

There is an N×MN \times M grid. Every cell is painted red or blue. The cell in row ii and column jj (1≤i≤N1 \le i \le N, 1≤j≤M1 \le j \le M) is written as (i,j)(i, j).

Write a program that counts the rectangles made only of red cells. A rectangle is fixed by four integers x1,y1,x2,y2x_1, y_1, x_2, y_2 with 1≤x1≤x2≤N1 \le x_1 \le x_2 \le N and 1≤y1≤y2≤M1 \le y_1 \le y_2 \le M, and it holds every cell (x,y)(x, y) with x1≤x≤x2x_1 \le x \le x_2 and y1≤y≤y2y_1 \le y \le y_2. For example, if x1=2x_1 = 2, y1=3y_1 = 3, x2=4x_2 = 4, y2=4y_2 = 4, the six cells (2,3)(2, 3), (2,4)(2, 4), (3,3)(3, 3), (3,4)(3, 4), (4,3)(4, 3), (4,4)(4, 4) all belong to one rectangle. Two rectangles are different when at least one of the four integers x1,y1,x2,y2x_1, y_1, x_2, y_2 differs.

Input

The first line contains the number of rows NN (1≤N≤30001 \le N \le 3000) and the number of columns MM (1≤M≤30001 \le M \le 3000), separated by a space.

Each of the next NN lines contains MM characters. Line ii gives the colors painted on row ii of the grid, and the jj-th character of that line is the color of (i,j)(i, j). Every character is 'R' or 'B'. 'R' means the cell is red, and 'B' means it is blue.

Output

Print the number of rectangles made only of red cells on the first line.

Hint

In the first example the 5 rectangles that satisfy the condition are listed below, written as [x1,y1,x2,y2][x_1, y_1, x_2, y_2].

  • [1,1,1,1][1, 1, 1, 1]
  • [1,1,1,2][1, 1, 1, 2]
  • [1,2,1,2][1, 2, 1, 2]
  • [1,1,2,1][1, 1, 2, 1]
  • [2,1,2,1][2, 1, 2, 1]

Examples1

  1. Example 1

    Input
    2 2
    RR
    RB
    
    Expected output
    5