2
def ways(self, p: List[str], k: int) -> int:3
m, n = len(p), len(p[0])4
apples = [[0 for _ in range(n + 1)] for _ in range(m + 1)]5
# number of apples in (0,0) to (i,j)6
apples[1][1] = 0 if p[0][0] == "." else 18
apples[1][j + 1] = apples[1][j] if p[0][j] == "." else apples[1][j] + 110
cur_apple = 0 if p[i][0] == "." else 111
apples[i + 1][1] = apples[i][1] + cur_apple15
apples[i + 1][j + 1] = apples[i][j + 1] + cur_apple17
def getApples(x, y, z, w):18
# get the number of apples within range19
# x,y: top-left coordinates of current pizza29
def cut(c, x, y, z, w):30
# c: number of cuts already cut31
# x,y: top-left coordinates of current pizza34
return 1 if getApples(x, y, z, w) > 0 else 040
if getApples(x, y, i, w) > 0:44
r += cut(c + 1, i + 1, y, z, w)49
if getApples(x, y, z, j) > 0:53
r += cut(c + 1, x, j + 1, z, w)58
return cut(0, 0, 0, m - 1, n - 1) % (10**9 + 7)