#P7532. [USACO21OPEN] Balanced Subsets P

[USACO21OPEN] Balanced Subsets P

Description

Farmer John's pasture can be viewed as a huge two-dimensional square grid made of square cells (imagine a huge chessboard). For each 1iN1 \le i \le N and 1jN1 \le j \le N, a cell can be represented by the ordered pair (i,j)(i,j). Some cells contain grass.

A non-empty subset of cells is called "balanced" if the following conditions hold:

  1. All cells in the subset contain grass.
  2. The subset is 4-connected. In other words, from any cell in the subset to any other cell in the subset, there exists a path such that every pair of adjacent cells on the path are adjacent horizontally or vertically.
  3. If cells (x1,y)(x_1,y) and (x2,y)(x_2,y) (x1x2x_1 \le x_2) are in the subset, then all cells (x,y)(x,y) with x1xx2x_1 \le x \le x_2 are also in the subset.
  4. If cells (x,y1)(x,y_1) and (x,y2)(x,y_2) (y1y2y_1 \le y_2) are in the subset, then all cells (x,y)(x,y) with y1yy2y_1 \le y \le y_2 are also in the subset.

Compute the number of balanced subsets modulo 109+710^9+7.

Input Format

The first line contains NN.

The next NN lines each contain a string of length NN. If cell (i,j)(i,j) contains grass, then the jj-th character of the ii-th line is G\texttt{G}; otherwise it is .\texttt{.}.

Output Format

Output the number of balanced subsets modulo 109+710^9+7.

2
GG
GG
13
4
GGGG
GGGG
GG.G
GGGG
642

Hint

Explanation for Sample 1

For this test case, every 4-connected subset is balanced.

G.  .G  ..  ..  GG  .G  ..  G.  GG  .G  G.  GG  GG
.., .., G., .G, .., .G, GG, G., G., GG, GG, .G, GG

Explanation for Sample 2

Below is an example of a subset that satisfies the second condition (4-connected) but does not satisfy the third condition:

GG..
.G..
GG..
....

Constraints

1N1501 \le N \le 150.

Translated by ChatGPT 5