#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, pair<ll, ll> > P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(1e5) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)

int h,w,si,sj,gi,gj;
ll cost[105][105];
ll dmin[10][105][105];
string s[105];
vector<P> hole,m;

void dijkstra(int i, int j, int k){
    REP(i,h){
        fill(dmin[k][i],dmin[k][i]+w,IINF);
    }
    dmin[k][i][j] = 0;
    priority_queue<P3,vector<P3>,greater<P3> > que;
    que.push({0,{i,j}});

    while(!que.empty()){
        P3 p = que.top();
        que.pop();
        int vi = p.second.first, vj = p.second.second;
        if(dmin[k][vi][vj] < p.first)continue;
        REP(t,4){
            int ni = vi+dy[t], nj = vj+dx[t];
            if(ni<0||ni>=h)continue;
            if(nj<0||nj>=w)continue;
            //cout << dmin[k][ni][nj]  << " " << dmin[k][vi][vj] + cost[vi][vj] << endl;
            if(dmin[k][ni][nj] > dmin[k][vi][vj] + cost[vi][vj]){
                dmin[k][ni][nj] = dmin[k][vi][vj] + cost[vi][vj];
                que.push({dmin[k][ni][nj],{ni,nj}});
            }
        }
    }
}


int main() {
    cin >> w >> h;
    REP(i,h)cin >> s[i];
    REP(i,h){
        REP(j,w){
            if(s[i][j]=='S'){
                si = i;
                sj = j;
            }
            if(s[i][j]=='G'){
                gi = i;
                gj = j;
            }
            if(s[i][j]=='#'){
                hole.push_back({i,j});
            }
            if(s[i][j]=='M'){
                m.push_back({i,j});
            }
        }
    }
    REP(i,h){
        fill(cost[i],cost[i]+w+1,1);
    }
    for(auto it:hole){
        int pi = it.first, pj = it.second;
        cost[pi][pj] = 99999999;
        for(int i=-2;i<=2;i++){
            for(int j=-2;j<=2;j++){
                int ni = pi+i, nj = pj+j;
                if(i==0&&j==0)continue;
                if(ni<0||ni>=h)continue;
                if(nj<0||nj>=w)continue;
                if(max(abs(i),abs(j))==2){
                    cost[ni][nj]=max(cost[ni][nj],2LL);
                }
                else{
                    cost[ni][nj]=max(cost[ni][nj],3LL);
                }
            }
        }
    }
    for(auto it:hole){
        int pi = it.first, pj = it.second;
        cost[pi][pj] = 99999999;
    }
    dijkstra(si,sj,0);
    REP(i,m.size()){
        dijkstra(m[i].first,m[i].second,i+1);
    }
    dijkstra(gi,gj,m.size()+1);
    vector<int> ls;
    REP(i,m.size())ls.push_back(i+1);
    SORT(ls);
    ll ans = LLINF;
    while(1){
        ll tmp = 0;
        REP(i,m.size()-1){
            tmp += dmin[ls[i]][m[ls[i+1]-1].first][m[ls[i+1]-1].second];

        }
        tmp += dmin[0][m[ls[0]-1].first][m[ls[0]-1].second];
        tmp += dmin[ls[m.size()-1]][gi][gj];
        ans = min(ans,tmp);
        if(!next_permutation(ls.begin(),ls.end()))break;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3968