Have you ever heard of the game Hex? Hex is a two-player game played on a hexagonal board. The rules are that the red/blue players take turns placing their colored pieces on empty spaces on the board until the red player connects the top and bottom of the board or the blue player connects the left and right of the board. The first player to achieve their goal wins. Here the connection condition is that there must be a path of the player's color connecting two adjacent hexagons with at least one common edge. For example, the following two Hex game boards are both victories for the blue player.
It can be proven that there is only one winning player in Hex.
Bobo also wants to play Hex, but he thinks that having hexagonal cells on the board is too complicated. So, he invented a new game called Square! The rules of Square are similar to Hex, where the red/blue players take turns placing their colored pieces on empty spaces on the board until the red player connects the top and bottom of the board or the blue player connects the left and right of the board. However, in Square, the board is a rectangle made up of many small squares, and the connection condition remains the same, which is to have a path of the player's color connecting two adjacent squares with at least one common edge (i.e., four-connected). The following two Square boards on a

board are victories for the red and blue players, respectively.
Bobo was excited to share this Square game with his friend, oboB. However, oboB immediately realized a problem: Bobo's Square game may result in a tie! For example, the following two Square game boards on a

board are both ties because both players have no moves left, and the red pieces are not connected to the top and bottom of the board, and the blue pieces are not connected to the left and right of the board.
"But...there shouldn't be many tie games, right?" Bobo said stubbornly. To refute Bobo, oboB drew a

-row by

-column matrix of red and blue cells. "I randomly select a submatrix from here as the game board, and there is a high probability that it will be a tie!" Bobo seemed to agree, but he suddenly thought of something else, "In order to make the rules of Square valid, the submatrix selected must have all red cells at the top and bottom and all blue cells on the left and right! (corresponding to the red/blue lines in the example matrix)" oboB had no choice but to agree with Bobo's argument, "Then could you please count how many game boards satisfy this condition?"
Formally, given an

-row by

-column matrix

, where each cell is either red ('R') or blue ('B'), you need to count the number of quadruples
)
satisfying the following conditions (where
)
and
)
are the coordinates of the top-left and bottom-right corners of the submatrix you select):
1.

,

(you must select a submatrix)
2.

,

'R' (the top and bottom of the submatrix must be all red)
3.

,

'B' (the left and right of the submatrix must be all blue)
4. There does not exist a sequence of cells
%2C(i_2%2Cj_2)%2C%5Cdots%2C(i_%7B%5Cell%7D%2Cj_%7B%5Cell%7D)))
satisfying:
(a)

,

,

(b)

,

(c)

,

'R'
(d)

,

(there is no connected submatrix with a red path between the top and the bottom)
5. There does not exist a sequence of cells
%2C(i_2%2Cj_2)%2C%5Cdots%2C(i_%7B%5Cell%7D%2Cj_%7B%5Cell%7D)))
satisfying:
(b)

,

(c)

,

'B'
(d)

,

(there is no connected submatrix with a blue path between the left and the right)