#P6008. [USACO20JAN] Cave Paintings P

[USACO20JAN] Cave Paintings P

Problem Description

Bessie has become an artist and is painting a mural. The painting is a square grid of height NN, and each row contains MM cells (1N,M10001\le N,M\le 1000). Each cell is either empty, painted as rock, or painted as water. Bessie has already painted all rock cells, including the entire border of the painting. She now wants to paint water in some of the empty cells so that, if the painting were real, there would be no net movement of water.

Define the height of a cell in row ii (counting from top to bottom) as N+1iN+1-i. Bessie wants her painting to satisfy the following rule:

Suppose cell aa is painted as water. Then if there exists a path from aa to a cell bb, consisting only of empty cells or water cells whose heights are not greater than the height of aa, where each pair of consecutive cells on the path shares a common edge, then bb is also painted as water.

Find the number of different paintings Bessie can create, modulo 109+710^9+7. Bessie may paint water in any number of empty cells, including painting none or painting all.

Input Format

The first line contains two space-separated integers NN and MM.

The next NN lines each contain MM characters. Each character is . or #, representing an empty cell and a rock cell, respectively. The first and last row, and the first and last column, contain only #.

Output Format

Output one integer: the number of paintings that satisfy the rule, modulo 109+710^9+7.

4 9
#########
#...#...#
#.#...#.#
#########
9

Hint

Sample Explanation

If any cell in the second row is painted as water, then all empty cells must be painted as water. Otherwise, suppose no cell in the second row is painted as water. Then Bessie may choose any subset of the three connected empty regions in the third row to paint as water. Therefore, the total number of paintings is 1+23=91+2^3=9.

Subtasks

  • Test cases 151 \sim 5 satisfy N,M10N,M \leq 10.
  • Test cases 6156 \sim 15 have no additional constraints.

Translated by ChatGPT 5