#P8639. [蓝桥杯 2016 国 B] 生成树计数
[蓝桥杯 2016 国 B] 生成树计数
Problem Description
Given an grid graph with rows and columns, there are vertices in total. There is an edge between every pair of adjacent vertices.

The figure above shows an example of a grid graph.
If some vertices and all edges adjacent to them are deleted from the graph (for example, deleting the vertex at row , column and the vertex at row , column in the figure above), the graph becomes as shown below.

A spanning tree of a graph is a subgraph that contains all vertices of the graph and some of its edges, such that for any two vertices there is exactly one unique path made of edges between them. If two spanning trees differ in at least one edge, they are considered different. In the graph above, there are different spanning trees in total: there are spanning trees that do not choose edge a, and spanning trees that choose edge a.
Given the information of which vertices are kept in the grid graph, compute how many different spanning trees the graph has.
Input Format
The first line contains two integers and , separated by spaces, representing the number of rows and columns of the grid graph.
The next lines each contain letters (with no separators). Each letter is either uppercase E or uppercase N. E means the corresponding vertex exists, and N means the corresponding vertex does not exist. It is guaranteed that at least one vertex exists.
Output Format
Output one line containing an integer, the number of spanning trees. The answer may be very large; you only need to output the result modulo (i.e. ).
3 4
EEEE
EENE
NEEE
31
Hint
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, .
For another of the testdata, .
For another of the testdata, .
For of the testdata, and .
Translated by ChatGPT 5