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

#FW0000095. Larry nhặt hạt dẻ (Nuts for nuts..)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Larry nhặt hạt dẻ (Nuts for nuts..)

Nuts for nuts..

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Larry starts at cell L on a grid, with nuts at # cells. Larry may move to any of eight neighboring cells, and all cells are passable. Find the minimum number of steps needed to collect all nuts and return to L. The distance between cells is max⁡(∣Δx∣,∣Δy∣)\max(|\Delta x|,|\Delta y|).

Input

First H,WH,W, followed by HH rows containing L, #, .. Let kk be the number of nuts #.

Output

Print the minimum number of steps.

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≤12,  0≤k≤92\le H,W\le 12,\;0\le k\le 9. All input-format and graph conditions above apply.

Examples

Input

2 2
L#
..

Output

2

Explanation

The only nut # is adjacent to L. Under eight-direction movement it takes one step to reach and one to return, totaling 1+1=21+1=2.