Aakriti, Avantika and Mehak are playing Hopscotch (Stapu). The arena is in the form of 2d Array with little modification as shown below:

So, in this game the person who is playing need to collect stones present on the tiles that are adjacent to the tile on which the player stands. Mehak is playing and if Avantika asks her to collect stones then she will move only one step in any direction to collect all the stones from the tiles that are touching the edge with the tile on which she is standing. But if Aakriti ask her, she will move two steps in any direction and then collect stones. Help Mehak to count the total number of stones she collected.

**Input:**

The first line of input contains integer **T** denoting the number of test cases. Each test case has subsequent two lines of input. The first line contains 2 integer inputs i.e** N** and **M **the size of the matrix **W**.Next line contains the **N** **X M** space separated elements of the matrix **W **i.e. stones. Next line takes a boolean and 2 integers input i.e. **a , i, j**. If **a **is** 0 **means Avantika asks her to collect and **1 **means Aakriti asks her** **and (**i,j**)** **denotes the index of tile where she is standing.

**Output:**

For each test case, output is the total number of stones she will collect.

**Constraints:**

1<=T<=50

1<=N,M<=20

0<=i<=N

0<=j<=M

**Example:
Input:**

2

3 3

5 9 7

6 4 5

8 1 2

0 1 1

3 3

5 9 7

6 4 5

8 1 2

1 1 1

31

12

**Explanation:**

1. 0 1 1 means Avantika asks her to collect so she will move one step and collect stones from the tiles that have common edge. Here (0,1), (1,0), (1,2), (2,1), (2,2), (2,0) have common edge with the tile (1,1). So adding all 9+6+5+8+1+2=31.

2. 1 1 1 means Aakriti asks her to collect so she will move two steps. Here (0,0) and (0,2) are the tiles , hence adding them 5+7 =12.

Author: Vanshika_pec

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

parass321 | 46 |

YogeshSharma7479 | 46 |

ayushwagdre1 | 44 |

yashranjan74 | 42 |

geekscoder | 41 |

Pulkit__Sharma__ | 806 |

Anirban166 | 682 |

Found_me | 636 |

abducodes | 607 |

Sathvik Avasarala | 526 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.