{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":"On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.\r\u003cbr\u003e\r\u003cbr\u003eYour task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a \u0027.\u0027 means an empty space, an \u0027H\u0027 represents a house on that point, and am \u0027m\u0027 indicates there is a little man on that point.\r\u003cbr\u003e\u003ccenter\u003e\u003cimg src\u003d\"CDN_BASE_URL/29da4c22cd9a35eb96b2a08b75ce8420?v\u003d1714514274\"\u003e\u003c/center\u003e\r\u003cbr\u003eYou can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house."}},{"title":"Input","value":{"format":"HTML","content":"There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of \u0027H\u0027s and \u0027m\u0027s on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M."}},{"title":"Output","value":{"format":"HTML","content":"For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay. "}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e2 2\r\n.m\r\nH.\r\n5 5\r\nHH..m\r\n.....\r\n.....\r\n.....\r\nmm..H\r\n7 8\r\n...H....\r\n...H....\r\n...H....\r\nmmmHmmmm\r\n...H....\r\n...H....\r\n...H....\r\n0 0\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e2\r\n10\r\n28\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}