Maze
题号:NC17708
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Niuniu likes his maze (Mei Zi) and he wants to collect more mazes.

Given n, m, a maze is a n x m grid. The rows and columns are numbered from 0.
Let's call the cell on the i-th row and the j-th column as (i,j).
There may be vertical walls between (i, j) and (i, j + 1).
There may be horizontal walls between (i, j) and (i + 1, j).

Two array v and h are given.
The size of v is n x (m - 1).
If v[i][j] is 1, then there is a wall between (i, j) and (i, j + 1).
If v[i][j] is 0, then there are two possibilities. There may or may not be a wall between (i, j) and (i, j + 1).

The size of h is (n-1) x m.
If h[i][j] is 1, then there is a wall between (i, j) and (i + 1, j).
If h[i][j] is 0, then there are two possibilities. There may or may not be a wall between (i, j) and (i + 1, j).

The beauty is defined as follows:
Calculate the size of each connected component, the sum of their square is the beauty.

You need output the sum of beauty of all possibilities.
(Obviously, the number of possibilities are 2^{the number of 0 in v and h})

As the answer might be very large, you only need to output the answer mod 1000000007.

输入描述:

The first line contains two integer n, m.
In following n lines, each line contains m-1 integers, which are the array v.
In following n-1 lines, each line contains m integers, which are the array h.

1 <= n <= 7
1 <= m <= 7
0 <= v[i][j] <= 1
0 <= h[i][j] <= 1

输出描述:

You should output the anwer in one line.
示例1

输入

复制
2 2
0
0
0 0

输出

复制
164

说明

If there is 0 or 1 wall, the beauty is 4 * 4 = 16
If there are 2 walls and they form shape L, the beauty is 3 * 3 + 1 * 1 = 10
If there are 2 walls and they form shape I, the beauty is 2 * 2 + 2 * 2 = 8
If there are 3 walls, the beauty is 2 * 2 + 1 * 1 + 1 * 1 = 6
If there are 4 walls, the beauty is 1 * 1 + 1 * 1 + 1 * 1 + 1 * 1 = 4
The answer should be 16 * 5 + 10 * 4 + 8 * 2 + 6 * 4 + 4 * 1 = 164
示例2

输入

复制
2 2
0
1
0 1

输出

复制
26

说明

The two existing wall forms shape L.
If there is no more wall, the beauty is 3 * 3 + 1 * 1 = 10
If there is 1 more wall, the beauty is 2 * 2 + 1 * 1 + 1 * 1 = 6
If there is 2 more walls, the beauty is 1 * 1 + 1 * 1 + 1 * 1 + 1 * 1 = 4
The answer should be 10 * 1 + 6 * 2 + 4 * 1 = 26