Omkar and Forest (Codeforces 1536E)
Tags: Math, shortest paths

Omkar's most recent follower, Ajit, has entered the Holy Forest. Ajit realizes that Omkar's forest is an $$$n$$$ by $$$m$$$ grid ($$$1 \leq n, m \leq 2000$$$) of some non-negative integers. Since the forest is blessed by Omkar, it satisfies some special conditions:  For any two adjacent (sharing a side) cells, the absolute value of the difference of numbers in them is at most $$$1$$$.  If the number in some cell is strictly larger than $$$0$$$, it should be strictly greater than the number in at least one of the cells adjacent to it. Unfortunately, Ajit is not fully worthy of Omkar's powers yet. He sees each cell as a "0" or a "#". If a cell is labeled as "0", then the number in it must equal $$$0$$$. Otherwise, the number in it can be any nonnegative integer.Determine how many different assignments of elements exist such that these special conditions are satisfied. Two assignments are considered different if there exists at least one cell such that the numbers written in it in these assignments are different. Since the answer may be enormous, find the answer modulo $$$10^9+7$$$.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 2000, nm \geq 2$$$) – the dimensions of the forest.$$$n$$$ lines follow, each consisting of one string of $$$m$$$ characters. Each of these characters is either a "0" or a "#".It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2000$$$.

Output: For each test case, print one integer: the number of valid configurations modulo $$$10^9+7$$$.

Note: For the first test case, the two valid assignments are$$$0000\\ 0000\\ 0000$$$and$$$0000\\ 0010\\ 0000$$$

Sample Inputs:
4
3 4
0000
00#0
0000
2 1
#
#
1 2
##
6 29
#############################
#000##0###0##0#0####0####000#
#0#0##00#00##00####0#0###0#0#
#0#0##0#0#0##00###00000##00##
#000##0###0##0#0##0###0##0#0#
#############################

Sample Outputs:
2
3
3
319908071

Solution (Python):
import sys

def reader():
    while True:
        x = sys.stdin.readline()
        yield x.strip()

def make_input(lines):
    x = iter(lines)
    return lambda : next(x)
P = int(1000000000.0 + 7)

def pp(lines):
    input = make_input(lines)
    cases = int(input())
    for case in range(cases):
        (n_row, n_col) = map(int, input().split())
        count = 0
        for _ in range(n_row):
            count += input().count('#')
        t = pow(2, count, P)
        if count == n_col * n_row:
            t -= 1
        print(t)
x = '4\n3 4\n0000\n00#0\n0000\n2 1\n#\n#\n1 2\n##\n6 29\n#############################\n#000##0###0##0#0####0####000#\n#0#0##00#00##00####0#0###0#0#\n#0#0##0#0#0##00###00000##00##\n#000##0###0##0#0##0###0##0#0#\n#############################'
if sys.argv[0].endswith('pydevconsole.py'):
    pp(x.split('\n'))
else:
    pp(reader())

Solution (Python):
import sys
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    hashcnt = 0
    for i in range(n):
        hashcnt += input().count('#')
    print(pow(2, hashcnt, 10 ** 9 + 7) - (hashcnt == n * m))

Solution (Python):
i = input
for _ in [1] * int(i()):
    (n, m) = map(int, i().split())
    s = sum((i().count('#') for _ in [1] * n))
    print(pow(2, s, 10 ** 9 + 7) - (s == n * m))