#K66797. Final Position in Matrix

    ID: 32500 Type: Default 1000ms 256MiB

Final Position in Matrix

Final Position in Matrix

You are given an m x n grid (matrix) in which every cell has a value of 0. You start at the top-left corner of the grid (position (0,0)) and you are given a sequence of movement directions. The directions can be one of the following: up, down, left, or right. Your task is to determine your final position after performing all the movements.

Note that if a movement would take you out of the grid boundaries, you must ignore that move. Formally, if the grid has dimensions \( m \times n \), then a move is valid only if the new position \( (x+dx, y+dy) \) satisfies \(0 \le x+dx < m\) and \(0 \le y+dy < n\).

inputFormat

The input is given from stdin with the following format:

  1. Two integers, m and n, representing the number of rows and columns of the grid.
  2. m lines follow, each containing n integers (each will be 0) representing the grid.
  3. An integer d on a new line representing the number of movement directions.
  4. A single line containing d space-separated strings. Each string is one of "up", "down", "left", or "right".

outputFormat

Output to stdout a single line containing two integers separated by a space: the final row index and the final column index in the grid.

## sample
2 2
0 0
0 0
0
0 0

</p>