{"trustable":true,"sections":[{"title":"","value":{"format":"MD","content":"The kingdom of Azkaroth is in great danger. Some strange creatures have captured the city and attacking the poor inhabitants.\n\nThe old wise king has chosen his best knights to save the kingdom from these creatures. They have already found that the creatures are targeting some food mills. So, the best way is to send the knights to look after the food mills. And if anything goes wrong he may call others to attack the creatures.\n\nThe kingdom can be modeled as an **n x n** grid. The legends are:\n\n\n\n| Value | Meaning |\n| :------: | ----------------------- |\n| `#` | Denotes a rock |\n| `.` | Denotes an empty space |\n| `A - Z` | Denotes a knight |\n| `m` | Denotes a mill |\n\n\n\nA knight can go to a mill using some moves, in each move a knight can go to its **four** neighboring cells **{North, East, West, South}** with the restriction that the destination cell shouldn\u0027t contain a rock (multiple knights can move together in a cell). And the number of mills that can be maintained by a knight is limited. The cost to look after a mill by a knight is the distance between the knight and the mill, distance means the number of moves the knight has to use to go to the mill. If a knight looks after multiple mills, the cost is the summation of distances from the knight to the mills.\n\nNow your task is to find the minimum total cost needed for the knights to look after all the mills. There can be more than one knight looking after a mill."}},{"title":"Input","value":{"format":"MD","content":"Input starts with an integer **T (\u0026#8804; 100)**, denoting the number of test cases.\n\nEach case starts with a line containing three integers **n (5 \u0026#8804; n \u0026#8804; 30)**, **k (1 \u0026#8804; k \u0026#8804; 26)** and **m (1 \u0026#8804; m \u0026#8804; 100)** where **k** denotes the number of knights and **m** denotes the number of mills in the grid. The knights will be marked by first **k** capital letters and the grid contains exactly one letter of each kind. Each of the next **n** lines contains **n** characters denoting the grid.\n\nThe next line contains **k** space separated integers (between **1** and **100**) denoting the number of mills the **i\u003csup\u003eth\u003c/sup\u003e** knight can look after. That means the first integer denotes the number of mills that can be looked after by knight **\u0027A\u0027**, second integer denotes the number of mills looked after by knight **\u0027B\u0027** and so on. And the outer cells of the grid will be rocks."}},{"title":"Output","value":{"format":"MD","content":"For each case, print the case number and the minimum cost as described above. You can assume that a solution will always exist."}},{"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\n7 4 5\n#######\n#A..mD#\n#....m#\n#..m.m#\n#....m#\n#B...C#\n#######\n1 2 1 1\n7 3 6\n#######\n#A#.m.#\n#.#..m#\n#m#m.m#\n##...m#\n#B...C#\n#######\n1 2 3\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003eCase 1: 15\nCase 2: 19\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}