Counting Codes
시간 제한5초메모리 제한1024 MB
1부터 9까지의 숫자로 채워진 m×n 격자에서 0을 채워 모든 L자 모양이 네 가지 산술 관계 중 하나를 만족하도록 하는 완성 방법의 수를 센다.
문제

Graphic by Henry Wang.
You're one of the king's spies sent on a secret mission to retrieve an item of incredible value, an ancient scroll from the throne room. Legend has it, the scroll contains the answer to the versus problem. When you finally reach the throne room, you realize there is a code the guards enter every day while observing them. As a spy, you've started to notice a few rules for each guard's code:
-
each code is a matrix consisting of nonzero decimal digits (integers from to ) with rows and columns
-
no digit repeats within any row of the code
-
for each digit in the code, except for those in the topmost row and rightmost column, let be the digit above it and let be the digit to its right in the code matrix. Then one of the following must be true:
- is the product of and
- is the sum of and
- is the difference of and or and
- is the quotient of and or and
On day , you've noticed a guard has seem to walked off while entering his code. Some digits have been omitted, but after careful consideration you think you can crack the code. Digits that have been omitted are represented with a . How many complete codes are possible, given the guard's partial code?
입력
A test case starts with a line containing two numbers () and (), which is the number of rows and number of columns of the grid. The following lines contain integers from to , separated by spaces. indicates an unknown value that you can supply, and there will be at most unknown values.
You can assume the guard has followed the rules with the partial code (i.e. no repeated digits appear in any row in the input, and any three pairs of non-zero digits that form an L have the property described above).
출력
For each test case, print the number of complete codes you can find.