
The first line of input contains an integer 'T' representing the number of test cases.
Each test case’s first line contains two space-separated integers ‘N’ and ‘M’. Here ‘N’ denotes the number of rows and ‘M’ represents the number of columns in the grid.
The next ‘N’ lines contain a single string of length ‘M’.
The next line contains a string ‘S’.
For each test case, print a single integer that denotes the total number of unique paths in the string.
The output of each test case will be printed in a separate line.
1 <= T <= 5
1 <= N, M <= 10
1 <= |S| <= N * M
Where ‘T’ is the number of test cases, ‘N’ denotes the number of rows and ‘M’ denotes the number of columns in the grid, and |S| denotes the length of string ‘S’.
You do not need to print anything, it has already been taken care of. Just implement the given function.
Both ‘S’ and grid contain only uppercase English characters.
The idea here is to use brute force. For each coordinate in the grid, we will count the total number of paths starting from this coordinate.
Algorithm:
Description of the ‘countPaths’ function:
This function will take 6 arguments: ‘curX’ and ‘curY’ to keep track of current coordinate in the grid, 2 D grid and the string ‘S’ given in the problem, ‘curIndex’ to keep track of current index-matched so far of string ‘S’ and a 2 – D ‘visited’ array to keep track of visited elements in the grid.
int countPaths(curX, curY, grid,S, curIndex, visited)