Hide

Problem M
Killing Aliens in a Borg Maze

The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated subspace network that ensures each member is given constant supervision and guidance.

Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over $100$ individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is defined as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is $11=5+3+3$.

Input

On the first line of input there is one integer, $1 \le N \le 50$, giving the number of test cases in the input.

Each test case starts with a line containing two integers $x$, $y$ such that $1 \le x, y \le 50$. After this, $y$ lines follow, each which $x$ characters. For each character, a space “ ” stands for an open space, a hash mark “#” stands for an obstructing wall, the capital letter “A” stands for an alien, and the capital letter “S” stands for the start of the search (there is exactly one “S” in the maze). The perimeter of the maze is always closed, i.e., there is no way to get out from the coordinate of the “S”. At most $100$ aliens are present in the maze, and everyone is reachable.

Output

For every test case, output one line containing the minimal cost of a successful search of the maze leaving no aliens alive.

Sample Input 1 Sample Output 1
2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  
8
11 

Please log in to submit a solution to this problem

Log in