p01533 Acrophobia
C: Acrophobia

Yayoi Takasugi is a super-selling idol. There is one thing she is not good at. It's a high place ... She is extremely afraid of heights. This time, due to the producer's inadequacy, she decided to take on the following challenges on a variety show.

This location will be held in a room in a ninja mansion. The floor of this room is lined with square tiles, and some tiles have fallen out to form holes. If you fall through this hole, you will find yourself in a pond a few meters below. Yayoi's challenge is to start from the designated place in this room, collect all the scrolls placed in the room, and bring them to the goal point.

Yayoi can move in the room according to the following rules.

* When moving from one tile to the next, you can only move to the top, bottom, left, and right tiles.
* If it is not close to the hole, it will take 1 second to move to the next tile.
* If you get close to the hole, Yayoi will be scared and it will take time to move as shown in Fig. C-1. For example, it takes 3 seconds to move from [2] [2] to [1] [2] as shown by the arrow in the figure.
* If there are multiple holes nearby, you will be afraid of the nearest hole.
* The time it takes to take a scroll can be assumed to be 0.



<image>
---
Figure C-1: Travel time near the hole

Yayoi wants to finish this challenge as soon as possible. Your job is to help Yayoi write a program that asks for the shortest time to complete this challenge.

Input

The floor information of the ninja mansion is given.

First, W and H, which represent the size of the room, are entered on the first line, separated by spaces (2 <= W, H <= 100). A W character string is input to each of the following H lines. There are the following types of characters that represent floor information.

*'.': Walkable floor
* '#' : hole
*'S': Where Yayoi stands at the start
*'G': Yayoi's place to reach
*'M': Where the scroll is located



'S',' G'and'M' are walking floors. There is exactly one'S'and one'G'in the room, respectively. You may pass the goal without all the scrolls. There are a minimum of 1'M'and a maximum of 5'M'in the room.

Output

Collect all the scrolls and output the shortest time to reach the goal from the start on one line. You can always assume that the input data has such a route.
Output a line break at the end of the line.

Sample Input 1


3 4
S.M
...
...
M.G


Sample Output 1


9


Sample Input 2


4 4
S..M
....
... #
..G


Sample Output 2


18


Sample Input 3


11 7
M ......... M
...........
... #. S. # ...
...........
... #. G. # ...
...........
M ......... M


Sample Output 3


62






Example

Input

3 4
S.M
...
...
M.G


Output

9
```python
from itertools import permutations
from collections import deque

w, h = map(int, input().split())
mp = ["X" * (w + 4)] * 2 + ["XX" + input() + "XX" for _ in range(h)] + ["X" * (w + 4)] * 2
m_lst = []
holes = []
for y in range(2, h + 2):
  for x in range(2, w + 2):
    if mp[y][x] == "S":
      sx, sy = x, y
    if mp[y][x] == "G":
      gx, gy = x, y
    if mp[y][x] == "M":
      m_lst.append((x, y))
    if mp[y][x] == "#":
      holes.append((x, y))

move_costs = [[1] * (w + 4) for _ in range(h + 4)]
for x, y in holes:
  for ny in range(y - 2, y + 3):
    for nx in range(x - 2, x + 3):
      move_costs[ny][nx] = max(2, move_costs[ny][nx])

  for ny in range(y - 1, y + 2):
    for nx in range(x - 1, x + 2):
      move_costs[ny][nx] = 3

def shortest(x1, y1):
  INF = 10 ** 20
  vec = ((1, 0), (0, -1), (-1, 0), (0, 1))
  costs = [[INF] * (w + 4) for _ in range(h + 4)]
  costs[y1][x1] = 0
  que = deque()
  que.append((0, x1, y1))
  while que:
    score, x, y = que.popleft()
    new_score = score + move_costs[y][x]
    for dx, dy in vec:
      nx, ny = x + dx, y + dy
      if mp[ny][nx] in {"S", "G", ".", "M"} and new_score < costs[ny][nx]:
        costs[ny][nx] = new_score
        que.append((new_score, nx, ny))
  return costs

edges = []
for x, y in m_lst:
  costs = shortest(x, y)
  edge = []
  for x2, y2 in m_lst:
    edge.append(costs[y2][x2])
  edge.append(costs[gy][gx])
  edges.append(edge)

start_cost = shortest(sx, sy)
goal_cost = shortest(gx, gy)
ans = 10 ** 20
for t in permutations(range(len(edges)), len(edges)):
  lst = list(t)
  score = 0
  for i in range(len(edges) - 1):
    score += edges[lst[i]][lst[i + 1]]
  xs, ys = m_lst[lst[0]]
  xf, yf = m_lst[lst[-1]]
  score += start_cost[ys][xs]
  score += edges[lst[-1]][-1]
  ans = min(ans, score)

print(ans)

```