Associated Course(s):
Must Do Interview Preparation

Given a boolean 2D matrix (0-based index), find whether there is path from (0,0) to (x,y) and if there is one path, print the minimum no of steps needed to reach it, else print -1 if the destination is not reachable. You may move in only four direction ie up, down, left and right. The path can only be created out of a cell if its value is 1.

**Input:**

The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines . The first line of each test case contains two integers n and m denoting the size of the matrix. Then in the next line are n*m space separated values of the matrix. The following line after it contains two integers x and y denoting the index of the destination.

**Output:**

For each test case print in a new line the min no of steps needed to reach the destination.

**Constraints:**

1<=T<=100

1<=n,m<=20

**Example:
Input:**

2

3 4

1 0 0 0 1 1 0 1 0 1 1 1

2 3

3 4

1 1 1 1 0 0 0 1 0 0 0 1

0 3

5

3

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.

Anirban166 | 375 |

Sathvik Avasarala | 308 |

abducodes | 285 |

KunalKrSingh | 279 |

UtkarshMalik | 270 |

Pulkit__Sharma__ | 618 |

Anirban166 | 503 |

abducodes | 446 |

Found_me | 425 |

Md Asif | 372 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.