
1. The number on these cells form a permutation of the set {0,1, 2, 3}.
2. The cell marked 1 is directly reachable from the cell marked 0.
3. The cell marked 2 is directly reachable from cell marked 1.
4. The cell marked 3 is directly reachable from cell marked 2.
The first line contains one positive integer ‘T’, denoting the number of test cases, then ‘T’ test cases follow.
The first line of each test case contains two integers ‘N’ and ‘M’, denoting the number of rows and columns in a given grid.
The next ‘N’ line contains ‘M’ space-separated integers consisting of either 0, 1, 2 or 3 denoting the grid cell.
The first line of each test case contains one integer ‘X’, denoting the maximum number of “Lucky Group”.
The output of each test case will be printed on a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N, M <= 20
0 <= cell[i][j] <= 3
Time Limit: 1 sec.
This problem can be solved using maximum flow concept.
Consider the following network:
We add the following edges to our network.
Let ‘countGroup(grid, n, m)’ be the function that returns the maximum number of lucky groups.
The steps are as follows :