PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000094. Kho báu của cướp biển Râu Đen (Blackbeard the Pirate)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Kho báu của cướp biển Râu Đen (Blackbeard the Pirate)

Blackbeard the Pirate

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

An island map contains landing point @, treasures !, water ~, palm trees #, hostile camps *, and sand .. Camps and all eight neighboring cells are forbidden. Movement uses four cardinal directions with unit cost. Collect all treasures and return to @ in minimum time; print -1 if impossible.

Input

First H,WH,W, then HH map rows of width WW. Let kk be the number of ! treasure cells.

Output

Print minimum hours or -1.

Subtasks

  • Subtask 1 (40 points): 2≤H,W≤6,  0≤k≤42\le H,W\le 6,\;0\le k\le 4. All input-format and graph conditions above apply.

  • Subtask 2 (60 points): 2≤H,W≤14,  0≤k≤82\le H,W\le 14,\;0\le k\le 8. All input-format and graph conditions above apply.

Examples

Input

2 2
@!
..

Output

2

Explanation

Start at @ in row 1, column 1, move right to collect !, then return one step left. Total two moves.