Red Rectangles
InterviewTime limit1sMemory limit512 MB
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 grid. Every cell is painted red or blue. The cell in row and column (, ) is written as .
Write a program that counts the rectangles made only of red cells. A rectangle is fixed by four integers with and , and it holds every cell with and . For example, if , , , , the six cells , , , , , all belong to one rectangle. Two rectangles are different when at least one of the four integers differs.
Input
The first line contains the number of rows () and the number of columns (), separated by a space.
Each of the next lines contains characters. Line gives the colors painted on row of the grid, and the -th character of that line is the color of . 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 .