Peter Parker (Spider-Man) want to time travel to save Tony Stark.
So Peter came to Doctor Strange for help. Doctor strange told Peter that he would help him if he can solve Strange’s problem. Peter accepts the challenge.
Doctor Strange gave Peter a grid having ‘N’ strings of size ‘M’ each. Then he gave Peter a string ‘S’. The task is to count the total number of paths in grid starting from any coordinate in the grid by moving only in left, right, up and down directions such that the string formed by characters in the path will be equal to ‘S’. Also, visiting a coordinate twice in a path is not allowed.
As Peter wants to bring Tony back as soon as possible, he called you for help.
The fate of Tony and Peter lies in your hand.
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’.
Output Format :
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’.
Note :
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.
2
2 2
UP
PO
UP
2 3
GOD
ODG
GOD
2
3
Test Case 1 :
Given string ‘S’ is “UP”. The grid contains 2 paths that have the same value as ‘S’.
The paths are shown below :

Test Case 2 :
Given string ‘S’ is “GOD”. The grid contains 3 paths that have the same value as ‘S’.
The paths are shown below :

2
2 6
NINJAS
OPIKAT
NINJAS
2 1
C
C
CC
1
2
Test Case 1 :
Given string ‘S’ is “NINJAS”. The grid contains 1 path that has the same value as ‘S’.
The paths are shown below :

Test Case 2 :
Given string ‘S’ is “CC”. The grid contains 2 paths that have the same value as ‘S’.
The paths are shown below :

Here first path is (0,0) -> (0,1) and second path is (0,1) -> (0,0)
For each grid coordinate, try to find all valid paths starting from this coordinate.
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)
O( (N * M) * (3 ^ (N * M) ), where ‘N’ is the number of rows and ‘M’ is the number of columns in the grid.
Here, ‘countPath’ function takes O( (3 ^ (N * M) ) time in the worst case when all characters of grid and string ‘S’ are same and length of ‘S’ is (N * M). In this case, every possible path will be a valid path. In each recursive call, we make three more recursive calls to visit the neighbours of the current coordinate. Note that we cannot go back to the previous coordinate from where we have reached the current coordinate so we will recursively visit only three neighbours of current coordinate. In the worst case, we need to visit O(N * M) so elements. So it will require O( 3 ^ (N * M ) ) time. Also, we call ‘countPath’ for all nodes of which are N * M.
Hence, the overall time complexity will be O(N * M) * O( 3 ^ (N * M ) ) = O( (N * M) * (3 ^ (N * M) ).
O(N * M), where ‘N’ is the number of rows, and ‘M’ is the number of columns in the grid.
We are using a visited array that takes O(N * M) space. In the worst case, we need O(N * M) recursive calls which require O(N * M) stack space.
Hence, the overall space complexity will be O(N * M) + O(N * M) = O(N * M).