S2 Cells

All submissions for this problem are available.
Little Anuj is in trouble.
He's trapped in a rectangular grid of cells.
Each cell has four doors.. One each to the north, south, east and west.
He has called home and asked for help, but it will take time to for his folks to get him out.
In the meantime, bored, he wants to determine for some pairs of cells the number of doors of the second cell that he can get to starting from the first one.
The only information he has is the number of rows and columns of cells and the status of each door.. whether closed or open.
All doors facing the outside world are closed.
Input
First line contains two numbers, m and n, where m= number of rows and n=number of columns of cells separated by a space. ( < 100)
The next m lines contain n1 numbers each, signifying the status of eastwest facing doors. ( 1 for open, 0 for closed )
The next m1 lines contain n numbers each, signifying the status of northsouth facing doors. ( 1 for open, 0 for closed )
For example, for m=2 and n=2, and the four cells as follows,
a b
c d
door between a and b open.
door between b and d open.
The input will be as follows
2 2
1 0
0 1
The next line will then contain the number of test cases for this scenario.
Each test case is given by four numbers, signifying index coordinates of starting cell and ending cell.
Output
For each test case, output a single number between 0 to 4 on a new line, signifying the number of doors of the second cell that he can enter through starting from the first one.
if the start and end cells are the same, output 1.
Example
Input: 5 11 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 1 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 10 2 0 2 8 2 8 2 0 2 3 2 4 1 1 2 6 1 1 1 6 1 2 0 3 1 1 3 1 2 0 2 3 3 2 1 2 2 4 2 5 Output: 2 1 1 1 2 0 2 2 2 1
Author:  credenz 
Tags  credenz 
Date Added:  3092012 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, GO, NODEJS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 