#K91742. Robotic Arm Mineral Extraction

    ID: 38043 Type: Default 1000ms 256MiB

Robotic Arm Mineral Extraction

Robotic Arm Mineral Extraction

You are given a grid of size \(n \times m\) where each cell contains either 0 or 1. A cell with a 1 indicates the presence of a mineral deposit.

A robotic arm starts at position \((0, 0)\). You will be given a sequence of queries. Each query is of one of the following two types:

  • 1 r c: Move the robotic arm to the cell at row r and column c.
  • 2: Calculate and output the Manhattan distance from the current position of the robotic arm to the nearest mineral deposit.

The Manhattan distance between two cells \((r_1, c_1)\) and \((r_2, c_2)\) is defined as \(|r_1 - r_2| + |c_1 - c_2|\). If there is no mineral deposit in the grid, output inf for that query.

inputFormat

The input is read from standard input (stdin) and consists of the following:

  1. A line containing two integers \(n\) and \(m\) — the number of rows and columns of the grid.
  2. \(n\) lines, each containing \(m\) integers (each either 0 or 1) representing the grid.
  3. A line containing an integer \(q\) — the number of queries.
  4. \(q\) lines, each representing a query. A query of type 1 contains three integers (1, r, c) and a query of type 2 contains a single integer 2.

outputFormat

For each query of type 2, output the Manhattan distance from the current arm position to the nearest mineral deposit on a separate line. If no mineral deposit exists, output inf.

## sample
3 3
0 0 1
0 1 0
1 0 0
3
1 0 0
2
1 1 1
2

</p>