#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
int dx1[]={-1,0,1,-1,1,-1,0,1};
int dy1[]={-1,-1,-1,0,0,1,1,1};
int dx2[]={-2,-1,0,1,2,-2,2,-2,2,-2,2,-2,-1,0,1,2};
int dy2[]={-2,-2,-2,-2,-2,-1,-1,0,0,1,1,2,2,2,2,2};
int aa[110][110][1<<6]={-1};
void mainmain(){
	int w,h;
	cin>>w>>h;
	vvint vv;
	rep(i,110){
		rep(j,110){
			rep(k,1<<6) aa[i][j][k]=-1;
		}
	}
	// vvint aa;
	vvint mm;
	initvv(vv,h,w,1);
	// initvv(aa,h,w,INF);
	initvv(mm,h,w,-1);
	int M=0;
	pii S,G;
	rep(i,h){
		rep(j,w){
			char t;
			cin>>t;
			if(t=='S'){
				S=mkp(i,j);
			}
			else if(t=='G'){
				G=mkp(i,j);
			}
			else if(t=='M'){
				mm[i][j]=M++;
			}
			else if(t=='#'){
				vv[i][j]=-1;
			}
		}
	}
	rep(i,h){
		rep(j,w){
			if(vv[i][j]==-1){
				rep(k,8){
					if(0<=dx1[k]+j&&dx1[k]+j<w&&0<=dy1[k]+i&&dy1[k]+i<h){
						if(vv[dy1[k]+i][dx1[k]+j]==-1) continue;
						vv[dy1[k]+i][dx1[k]+j]=max(vv[dy1[k]+i][dx1[k]+j],3);
					}
				}
				rep(k,16){
					if(0<=dx2[k]+j&&dx2[k]+j<w&&0<=dy2[k]+i&&dy2[k]+i<h){
						if(vv[dy2[k]+i][dx2[k]+j]==-1) continue;
						vv[dy2[k]+i][dx2[k]+j]=max(vv[dy2[k]+i][dx2[k]+j],2);
					}
				}
			}
		}
	}
	// rep(i,h){
		// rep(j,w){
			// printf("%2d",vv[i][j]);
			// cout<<vv[i][j]<<" ";
		// }
		// cout<<endl;
	// }
	// cout<<endl;
	// rep(i,h){
		// rep(j,w){
			// printf("%2d",mm[i][j]);
			// cout<<vv[i][j]<<" ";
		// }
		// cout<<endl;
	// }
	// cout<<endl;
	int dx[]={0,0,1,-1};
	int dy[]={1,-1,0,0};
	priority_queue<pair<pii,pii> > pq;
	pq.push(mkp(mkp(0,0),S));
	while(!pq.empty()){
		pair<pii,pii> top=pq.top();
		pq.pop();
		// bitset<6> tt=top.F.S;
		if(aa[top.S.F][top.S.S][top.F.S]>=0) continue;
		// cout<<top.S.F<<" "<<top.S.S<<" "<< tt.to_string() <<" "<<top.F.S<<" "<<-top.F.F<<endl;
		aa[top.S.F][top.S.S][top.F.S]=-top.F.F;
		if(mm[top.S.F][top.S.S]!=-1){
			top.F.S|=1<<mm[top.S.F][top.S.S];
			// if(aa[top.S.F][top.S.S][top.F.S]>=0) continue;
		}
		aa[top.S.F][top.S.S][top.F.S]=-top.F.F;
		if(top.S==G&&top.F.S==(1<<M)-1) break;
		rep(i,4){
			int y=top.S.F+dy[i],x=top.S.S+dx[i];
			if(0<=x&&x<w&&0<=y&&y<h){
				if(aa[y][x][top.F.S]==-1&&vv[y][x]!=-1){
					// if(x==5&&y==6&&top.F.S==5) cout<<-top.F.F<<" hoge\n";
					pq.push(mkp(mkp(top.F.F-vv[top.S.F][top.S.S],top.F.S),mkp(y,x)));
				}
			}
		}
	}
	cout<<aa[G.F][G.S][(1<<M)-1]<<endl;
}



}
int main() try{
    mainmain();
}
catch(...){
}