Cossack Vus and the Table
Cossack Vus recently discovered a table with rows and columns. This matrix is filled with the symbols , , , and .
Cossack Vus has defined rules for moving through the matrix. If you are currently at cell (the cell at the intersection of the -th row and the -th column), then:
If , move to cell ;
If , move to cell ;
If , move to cell ;
If , move to cell .
The path ends when you move outside the boundaries of the matrix.
Cossack Vus has prepared queries for you, each in the form . He wants to know how many cells you will pass through to exit the matrix if you start at cell . Before starting, you must change the element to .
Note that these queries are independent. Any changes made for a query revert back before the next query.
Input
The first line contains two integers and () — the number of rows and columns, respectively.
The next lines contain the elements of each row of the matrix: , without spaces between them.
The following line contains one integer () — the number of queries.
Each of the next lines describes a query in the form ().
Output
For each query, output a single integer on a separate line — the number of cells you need to pass through to exit the matrix starting from cell . If you never exit the matrix, output «0
».
Examples
Scoring
( points): ;
( points): initially, it is not possible to exit the matrix from any cell;
( points): , initially, it is possible to exit the matrix from any cell;
( points): ;
( points): initially, it is possible to exit the matrix from any cell;
( points): no additional constraints.