Treasure Lair
시간 제한1초메모리 제한2048 MB
각 질의 칸에서 보물 K개를 시작 칸으로 가져오는 최소 시간을 구한다. 이동은 8방향이고 한 번에 보물 하나만 옮길 수 있다.
문제
During your recent exploration, you come across a treasure lair that can be represented as a grid with rows (numbered from to ) and columns (numbered from to ). The cell at row and column is denoted as . Cell contains a treasure if , and it is empty if .
There are independent scenarios. For each scenario, you start in cell and you want to take exactly treasures. In one second, you can move to any orthogonally or diagonally adjacent cell to the cell you are currently in, as shown in the following illustration. Since the treasures are heavy, you can only carry one treasure at a time, meaning you must bring the treasure back to cell before going for the next one or completing the scenario. The action of taking or putting down a treasure takes zero seconds.

For each scenario, determine the minimum required time (in seconds) to take treasures back to cell if you start in . As all scenarios are independent of each other, the treasures are back to their original positions at the beginning of a scenario.
입력
The first line consists of two integers ().
Each of the next lines consists of a binary string of length . The th character of string describes cell : it is if cell contains a treasure, and if cell is empty. The number of treasures in the lair is at least one.
The next line consists of an integer ().
Each of the next lines consists of three integers (; ; ) describing each scenario. The value of does not exceed the number of treasures in the lair.
출력
For each scenario, output an integer in a single line representing the minimum required time (in seconds) to take treasures back to cell if you start in .