#P6551. [COCI 2010/2011 #2] CRNI
[COCI 2010/2011 #2] CRNI
Description
Although Mirko has found all the most interesting rides, his enthusiasm still has not faded. He opens a notebook with squared paper and starts coloring the squares. A new and even harder problem comes to his mind.
You are given an grid. Each cell is either black or white.
If all cells of some rectangular shape inside the grid are black, and the rectangle consists of at least two cells, then this rectangle is called a black rectangle.

The top-left picture shows two examples that are not black rectangles. Rectangle is not a black rectangle because it contains one white cell; rectangle is not a black rectangle because it contains only one cell. The top-right picture shows three examples of black rectangles.
Compute the number of ways to choose two black rectangles that do not share any cells. Since the answer may be very large, you should output the remainder of this number modulo .
Input Format
The first line contains an integer , whose meaning is described above.
The next lines each contain an uppercase string of length , consisting only of C (meaning the cell is black) and B (meaning the cell is white), describing the grid.
Output Format
Print one integer: the number of ways to choose two black rectangles with no common cells, modulo .
2
CC
CC
2
3
CCB
CCB
CBB
5
5
BCCBB
BBCBB
BCCBB
BBBBB
CCBBB
8
Hint
Constraints
For of the data, it is guaranteed that . Each character in the input strings is either C or B. Let be the length of each string; then .
Notes
- This problem is worth points in total.
- Translated from COCI2010-2011 CONTEST #2 CRNI, translator https://www.luogu.com.cn/user/115711
Translated by ChatGPT 5
京公网安备 11011102002149号