题目描述
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
Chef loves single-colored squares, so he got a rectangular board with size $N \times M$ consisting only of black cells and white cells. He calculates the cost of the board in the following manner:
Let's denote the number of ways to choose $k^{2}$ white cells which form a square with edge length $k$ (and edges parallel to the borders of the board) by $W_{k}$. For example, a $2 \times 3$ board consisting of 6 white cells and no black cells has $W_{1}=6$, $W_{2}=2$ and $W_{3}=0$.
Similarly, let's denote the number of ways to choose $k^{2}$ black cells forming a square by $B_{k}$.
Chef chose two sequences of non-negative integers $C_{W}$ and $C_{B}$, each with length $\mathrm{min}(N, M)$. The cost of the board is defined as
$$\sum_{i=1}^{\mathrm{min}(N, M)} C_{B, i} \cdot B_{i} + C_{W, i} \cdot W_{i}\,.$$
However, Chef accidentally spilled some sauce on the board while cooking, so the colors of some cells cannot be determined. Now he wants to repaint each of these cells using black or white paint. What is the maximum achievable cost of the resulting board? Note that he may paint different cells using different colors.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains a string with length $M$ describing the $i$-th row of the board — the character '0' stands for a white cell, '1' stands for a black cell and '?' stands for a cell with unknown color.
The following line contains $K=\mathrm{min}(N, M)$ space-separated integers $C_{W, 1}, C_{W, 2}, \dots, C_{W, K}$.
The last line contains $K=\mathrm{min}(N, M)$ space-separated integers $C_{B, 1}, C_{B, 2}, \dots, C_{B, K}$.
------ Output ------
For each test case, print a single line containing one integer — the maximum possible cost.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N \cdot M ≤ 500$
each string contains only characters '0', '1' and '?'
$0 ≤ C_{W, i} ≤ 10^{8}$ for each valid $i$
$0 ≤ C_{B, i} ≤ 10^{8}$ for each valid $i$
------ Subtasks ------
Subtask #1 (5 points): $1 ≤ N \cdot M ≤ 10$
Subtask #2 (15 points): $1 ≤ N \cdot M ≤ 40$
Subtask #3 (80 points): original constraints
----- Sample Input 1 ------
1
3 3
10?
01?
???
1 1 1
2 2 2
----- Sample Output 1 ------
18
----- explanation 1 ------
Example case 1: Replacing each '?' by '1' gives the optimal answer.
输入格式
见题面
输出格式
见题面