X Aura
시간 제한1초메모리 제한2048 MB
격자 위 두 칸 사이를 이동할 때 발생하는 총 페널티의 최솟값을 구하고, 페널티가 한없이 작아질 수 있으면 INVALID를 출력한다.
문제
Mount ICPC can be represented as a grid of rows (numbered from to ) and columns (numbered from to ). The cell located at row and column is denoted as and has a height of . Two cells are adjacent to each other if they share a side. Formally, is adjacent to , , , and , if any exists.
You can move only between adjacent cells, and each move comes with a penalty. With an aura of an odd positive integer , moving from a cell with height to a cell with height gives you a penalty of . Note that the penalty can be negative.
You want to answer independent scenarios. In each scenario, you start at the starting cell and you want to go to the destination cell with minimum total penalty. In some scenarios, the total penalty might become arbitrarily small; such a scenario is called invalid. Find the minimum total penalty to move from the starting cell to the destination cell, or determine if the scenario is invalid.
입력
The first line consists of three integers (; ; is an odd integer).
Each of the next lines consists of a string of length . Each character in is a number from to . The th character of represents the height of cell , or .
The next line consists of an integer ().
Each of the next lines consists of four integers (; ).
출력
For each scenario, output the following in a single line. If the scenario is invalid, output INVALID. Otherwise, output a single integer representing the minimum total penalty to move from the starting cell to the destination cell.