
You are given a N*M 2-D array grid, which represents an underground sewage system where N is the height of the sewage system, and M is the width. There are pipelines in the sewage system, and in total, there are seven types of pipes, as shown below:
Type - 1

Type - 2

Type - 3

Type - 4

Type - 5

Type - 6

Type - 7

The given N*M grid will contain numbers from 0-7, where 0 will mean that there is no pipeline, and numbers from 1 to 7 will indicate the type of pipeline present at that point.
Now you are standing at any given point (X, Y) in this sewage system, and you can walk up to L units in any direction of your choice from the starting point. Your task is to calculate the total number of pipes you can explore if you start from your initial position.
Note:
1. The given input will always contain numbers from 0 to 7.
2. The point where you are initially standing will always be a valid point. I.e., you will always be present inside the N*M grid.
The first line contains an integer T, which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case contains five integers N, M, X, Y, and L, as described in the problem statement.
Then each of the next N lines will contain M integers, each from 0-7, describing the sewage system.
Output Format:
For each test case, print in a new line, one integer representing the total number of pipes you can explore if you start from your initial position.
Output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N, M <= 1000
0 <= X < N
0 <= Y < M
1 <= L <= 10^6
Time Limit: 1 second
1
3 6 1 3 3
0 0 1 0 0 0
0 0 5 1 6 0
0 0 2 0 2 0
5
You are standing at (1,3), and you can go to (1,2),(1,4),(2,2), and (2,4); therefore, you can explore 5 pipes.

Use a BFS approach to explore all possible pipes.
Here we will be using the BFS approach. Initially, we are standing at a given point (X, Y), and from this point, we will explore in all four directions if the pipelines allow us to visit(i.e., If they are connected) and if we have distance left to cover. So initially we will push the current position and the length we can travel further in a queue. Then we check in all four directions. If the pipe is not visited before and if we can go in any direction with a positive distance left, then we will push that point and the distance left in the queue and will repeat this process till the queue is not empty. Each time we visit an unvisited point whose value is greater than 0, we will increase the answer count and finally return the answer.
Steps:
O(N*M), Where N is the height of the sewage system, and M is the width.
Since we are doing a BFS on the given grid, we will visit each node only once. Hence, the time complexity will be O(N*M).
O(N*M), Where N is the height of the sewage system, and M is the width.
Since we have created a visited array of size N*M and also our queue will store the coordinates of the grid. Hence, the space complexity will be O(N*M).