#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define range(x,l,r) (l<=x && x<r)  
using namespace std;
typedef pair<int,int> pii;

const int dy[] = {-1,0,1,0}, dx[] = {0,1,0,-1};
int w,h;
int g[110][110];

int dijkstra(int s, int t){
  vector<int> dis(h*w,1e8);
  dis[s] = 0;
  priority_queue< pii,vector<pii>,greater<pii> > q;
  q.push(pii(0,s));
  
  while(q.size()){
    pii cur = q.top(); q.pop();
    int y = cur.second/w, x = cur.second%w, d = cur.first;
    if(t == cur.second)return d;
    if(dis[cur.second] < d)continue;

    rep(dir,4){
      int ny = y+dy[dir], nx = x+dx[dir];
      if(range(ny,0,h) && range(nx,0,w) && g[ny][nx]!=4){
	int nxt = ny*w + nx, nd = dis[cur.second] + g[y][x];
	if(dis[nxt] > nd){
	  dis[nxt] = nd;
	  q.push(pii(nd, nxt));
	}
      }
    }
  }
  return -1;
}

int main(){
  cin >> w >> h;
  vector<string> s(h);
  rep(i,h){
    cin >> s[i];
    rep(j,w)g[i][j] = 1;
  }

  int S,G;
  vector<int> maki;
  rep(i,h)rep(j,w){
    if(s[i][j] == '#'){
      for(int ii=-2;ii<=2;ii++)for(int jj=-2;jj<=2;jj++){
	  int ni = i+ii, nj = j+jj;
	  if(range(ni,0,h) && range(nj,0,w)){
	    int dif = max(abs(ii),abs(jj));
	    g[ni][nj] = max(g[ni][nj], 4-dif);
	  }
	}
    }else if(s[i][j] == 'M')maki.push_back(i*w + j);
    else if(s[i][j] == 'S')S = i*w + j;
    else if(s[i][j] == 'G')G = i*w + j;
  }

  int K = maki.size();
  int dis[10][10];
  rep(i,K){
    dis[0][i+1] = dijkstra(S,maki[i]);
    dis[i+1][K+1] = dijkstra(maki[i],G);
    rep(j,i){
      dis[i+1][j+1] = dijkstra(maki[i], maki[j]);
      dis[j+1][i+1] = dijkstra(maki[j], maki[i]);
    }
  }

  vector<int> perm(K);
  rep(i,K)perm[i] = i+1;
  int ans = 1e8;
  do{
    int sum = dis[0][perm[0]] + dis[perm[K-1]][K+1];
    for(int i=0;i<K-1;i++)sum += dis[perm[i]][perm[i+1]];
    ans = min(ans, sum);
  }while(next_permutation(perm.begin(),perm.end()));
  cout << ans << endl;
}
### Prompt

Your challenge is to write a CPP solution to the following problem:

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

### Response

```cpp
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define range(x,l,r) (l<=x && x<r)  
using namespace std;
typedef pair<int,int> pii;

const int dy[] = {-1,0,1,0}, dx[] = {0,1,0,-1};
int w,h;
int g[110][110];

int dijkstra(int s, int t){
  vector<int> dis(h*w,1e8);
  dis[s] = 0;
  priority_queue< pii,vector<pii>,greater<pii> > q;
  q.push(pii(0,s));
  
  while(q.size()){
    pii cur = q.top(); q.pop();
    int y = cur.second/w, x = cur.second%w, d = cur.first;
    if(t == cur.second)return d;
    if(dis[cur.second] < d)continue;

    rep(dir,4){
      int ny = y+dy[dir], nx = x+dx[dir];
      if(range(ny,0,h) && range(nx,0,w) && g[ny][nx]!=4){
	int nxt = ny*w + nx, nd = dis[cur.second] + g[y][x];
	if(dis[nxt] > nd){
	  dis[nxt] = nd;
	  q.push(pii(nd, nxt));
	}
      }
    }
  }
  return -1;
}

int main(){
  cin >> w >> h;
  vector<string> s(h);
  rep(i,h){
    cin >> s[i];
    rep(j,w)g[i][j] = 1;
  }

  int S,G;
  vector<int> maki;
  rep(i,h)rep(j,w){
    if(s[i][j] == '#'){
      for(int ii=-2;ii<=2;ii++)for(int jj=-2;jj<=2;jj++){
	  int ni = i+ii, nj = j+jj;
	  if(range(ni,0,h) && range(nj,0,w)){
	    int dif = max(abs(ii),abs(jj));
	    g[ni][nj] = max(g[ni][nj], 4-dif);
	  }
	}
    }else if(s[i][j] == 'M')maki.push_back(i*w + j);
    else if(s[i][j] == 'S')S = i*w + j;
    else if(s[i][j] == 'G')G = i*w + j;
  }

  int K = maki.size();
  int dis[10][10];
  rep(i,K){
    dis[0][i+1] = dijkstra(S,maki[i]);
    dis[i+1][K+1] = dijkstra(maki[i],G);
    rep(j,i){
      dis[i+1][j+1] = dijkstra(maki[i], maki[j]);
      dis[j+1][i+1] = dijkstra(maki[j], maki[i]);
    }
  }

  vector<int> perm(K);
  rep(i,K)perm[i] = i+1;
  int ans = 1e8;
  do{
    int sum = dis[0][perm[0]] + dis[perm[K-1]][K+1];
    for(int i=0;i<K-1;i++)sum += dis[perm[i]][perm[i+1]];
    ans = min(ans, sum);
  }while(next_permutation(perm.begin(),perm.end()));
  cout << ans << endl;
}
```