# Number of ways of cutting a Matrix such that atleast one cell is filled in each part

Given an integer **K** and a matrix **mat[][]** containing 1 and 0, where 1 denotes the cell is filled and 0 denotes an empty cell. The task is to find the number of ways to cut the matrix into **K** parts using **K-1** cuts such that each part of the matrix contains atleast one filled cell.

For each cut, there must be a direction either horizontal or vertical. Then you choose a cut position at the cell boundary and cut the matrix into two parts. If you cut the matrix vertically, the left part of the matrix will not be used again for further process. If you cut the matrix horizontally, the upper part of the matrix will not be used again.

**Examples:**

Input:mat[][] = {{1, 0, 0}, {1, 1, 1}, {0, 0, 0}}, K = 3

Output:3

Explantion:

Input:matrix = {{1, 0, 0}, {1, 1, 0}, {0, 0, 0}}, K = 3

Output:1

**Approach:** The idea is to use Dynamic Programming to compute the number of ways of cutting a matrix with atleast one filled cell.

- Compute the prefix sum of the matrix such that we can compute a particular portion of the matrix contains a filled cell or not in O(1) time.
- Define a dp table to store the number of ways to cut the pizza in K parts, where dp[k][r] denotes the number of ways to cut the matrix into K parts from top left to R
^{th}row and C^{th}Column. - Finally, iterate over for every possible matrix and check that if the matrix can be cut into the two parts upto that index and both part is valid or not.

Below is the implementation of the above approach:

## Python

`# Python implementation to find the ` `# number of ways to cut the matrix ` `# into the K parts such that each ` `# part have atleast one filled cell ` ` ` `# Function to find the ` `# number of ways to cut the matrix ` `# into the K parts such that each ` `# part have atleast one filled cell ` `def` `ways(arr, k): ` ` ` `R ` `=` `len` `(arr) ` ` ` `C ` `=` `len` `(arr[` `0` `]) ` ` ` `K ` `=` `k ` ` ` `preSum ` `=` `[[` `0` `for` `_ ` `in` `range` `(C)]\ ` ` ` `for` `_ ` `in` `range` `(R)] ` ` ` ` ` `# Loop to find prefix sum of the ` ` ` `# given matrix ` ` ` `for` `r ` `in` `range` `(R` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `for` `c ` `in` `range` `(C` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `preSum[r] ` `=` `arr[r] ` ` ` ` ` `if` `r ` `+` `1` `< R: ` ` ` `preSum[r] ` `+` `=` `preSum[r ` `+` `1` `] ` ` ` `if` `c ` `+` `1` `< C: ` ` ` `preSum[r] ` `+` `=` `preSum[r] ` ` ` ` ` `if` `r ` `+` `1` `< R ` `and` `c ` `+` `1` `< C: ` ` ` `preSum[r] ` `-` `=` `preSum[r ` `+` `1` `] ` ` ` ` ` `# dp(r, c, 1) = 1 ` ` ` `# if preSum[r] else 0 ` ` ` `dp ` `=` `[[[` `0` `for` `_ ` `in` `range` `(C)]\ ` ` ` `for` `_ ` `in` `range` `(R)]\ ` ` ` `for` `_ ` `in` `range` `(K ` `+` `1` `)] ` ` ` ` ` `# Loop to iterate over the dp ` ` ` `# table of the given matrix ` ` ` `for` `k ` `in` `range` `(` `1` `, K ` `+` `1` `): ` ` ` `for` `r ` `in` `range` `(R` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `for` `c ` `in` `range` `(C` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `if` `k ` `=` `=` `1` `: ` ` ` `dp[k][r] ` `=` `1` `\ ` ` ` `if` `preSum[r] > ` `0` `\ ` ` ` `else` `0` ` ` `else` `: ` ` ` `dp[k][r] ` `=` `0` ` ` `for` `r1 ` `in` `range` `(r ` `+` `1` `, R): ` ` ` ` ` `# Check if can cut horizontally ` ` ` `# at r1, at least one apple in ` ` ` `# matrix (r, c) -> r1, C-1 ` ` ` `if` `preSum[r] ` `-` `preSum[r1] > ` `0` `: ` ` ` `dp[k][r] ` `+` `=` `dp[k` `-` `1` `][r1] ` ` ` `for` `c1 ` `in` `range` `(c ` `+` `1` `, C): ` ` ` ` ` `# Check if we can cut vertically ` ` ` `# at c1, at least one apple in ` ` ` `# matrix (r, c) -> R-1, c1 ` ` ` `if` `preSum[r] ` `-` `preSum[r][c1] > ` `0` `: ` ` ` `dp[k][r] ` `+` `=` `dp[k` `-` `1` `][r][c1] ` ` ` `return` `dp[K][` `0` `][` `0` `] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[[` `1` `, ` `0` `, ` `0` `], [` `1` `, ` `1` `, ` `1` `], [` `0` `, ` `0` `, ` `0` `]] ` ` ` `k ` `=` `3` ` ` ` ` `# Function Call ` ` ` `print` `(ways(arr, k)) ` |

*chevron_right*

*filter_none*

**Output:**

3

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Number of ways to reach the end of matrix with non-zero AND value
- Number of ways to make exactly C components in a 2*N matrix
- Count the number of ways to traverse a Matrix
- Number of ways to reach (X, Y) in a matrix starting from the origin
- Count number of ways to reach a given score in a Matrix
- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix | Set 2
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix
- Final cell position in the matrix
- Sum of cost of all paths to reach a given cell in a Matrix
- Distance of nearest cell having 1 in a binary matrix
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Count of ways to traverse a Matrix according to given conditions
- Count of ways to traverse a Matrix and return to origin in K steps

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.