#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define si short int
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pill pair<ll,ll>
#define f first
#define s second
#define pilc pair<ll,char>
#define all(a) (a).begin(),(a).end()
#define rep(s,e,step) for(int i = (s); i < (e) ; i += step)
#define vrep(s,e,step) for(int j = (s); j < (e) ; j += step)
#define ex exit(0) 
#define sz(a) (ll)(a).size()
#define triple pair<pill, ll>
#define pinode pair<node*, node*>
#define quadra pair<pill, pill>
#define ld long double
using namespace std;
 
const ll N = 500 + 10;
const ll M = 1e14;
const ll big = 1e14;
const ll hsh2 = 1964325029 ;
const long long mod = 1e9 + 7;
const long double EPS = 1e-20;
const ll block = 500;
const ll shift = 2e3;
const ld pi = acos(-1.0);

ll n, m;
vector<int> g[N];
ll ans = 0;
ll glo = 1;
ll c[N];
vector<ll> cmp;

void solve(int pos) {
	if(pos == cmp.size()) {
	    ans++;
		return;
	}
	bool can[4] = {0};
	for(auto u : g[cmp[pos]]) {
		if(u > cmp[pos])
			break;
    	can[c[u]] = 1;	
    }
    if(!can[1])
    	c[cmp[pos]] = 1,
    	solve(pos + 1);
    if(!can[2])
    	c[cmp[pos]] = 2,
    	solve(pos + 1);
    if(!can[3])
    	c[cmp[pos]] = 3,
    	solve(pos + 1);
    c[cmp[pos]] = 0;
}

bool us[N];

void dfs(ll v) {
	us[v] = 1;
	for(auto u : g[v])
		if(!us[u])dfs(u);
	cmp.pb(v);
}

int main() {  
	speed;
	cin >> n >> m;
	for(int i = 1; i <= m; i++) {
		ll a, b;
		cin >> a >> b;
		g[b].pb(a);
		g[a].pb(b);
	}   
	for(int i = 1; i <= n; i++)
		sort(all(g[i]));
	for(int i = 1; i <= n; i++) {
		if(!us[i]) {			
			dfs(i);
			sort(all(cmp));
			cmp.erase(unique(all(cmp)), cmp.end());
			solve(0);
			glo = glo * ans;
			ans = 0;
			cmp.clear();
		}
	}
	cout << glo;
}
/*
4
4 6 1 9
2 5 3 2 
*/
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/segtree>
//using namespace atcoder;

using ll = long long int;
using P = pair<int, int>;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define sz(x) int(x.size())
#define endl '\n'
const int INF = 1001001001;
const ll mod = 1000000007;
const ll INFL = 10000000010000000;

int main() {
    int h, w; cin >> h >> w;
    vector<string> s(h); rep(i,h) cin >> s[i];
    int sx, sy, gx, gy;
    vector<pair<int, int>> tp[26];
    rep(i,h) rep(j,w) {
        if (s[i][j] == 'S') {
            sx = i;
            sy = j;
        }
        else if (s[i][j] == 'G') {
            gx = i;
            gy = j;
        }
        int d = s[i][j]-'a';
        if (0 <= d && d <= 25) tp[d].push_back({i, j});
    }

    vector<vector<int>> dist(h, vector<int>(w, INF));
    vector<pair<int,int>> dxdy = {{1,0}, {0,1}, {-1,0}, {0,-1}};
    queue<pair<int,int>> que;
    dist[sx][sy] = 0;
    que.push({sx, sy});

    while (que.size()) {
        pair<int,int> p = que.front(); que.pop();
        int cx = p.first, cy = p.second;
        for (int i = 0; i < 4; i++) {
            int nx = cx+dxdy[i].first, ny = cy+dxdy[i].second;
            if (!(0 <= nx && nx < h && 0 <= ny && ny < w)) continue;
            if (s[nx][ny] == '#') continue;
            if (dist[nx][ny] == INF) {
                dist[nx][ny] = dist[cx][cy]+1;
                que.push({nx, ny});
            }
        }
        int d = s[cx][cy]-'a';
        if (d < 0 || 25 < d) continue;
        for (auto pn : tp[d]) {
            if (dist[pn.first][pn.second] == INF) {
                dist[pn.first][pn.second] = dist[cx][cy]+1;
                que.push({pn.first, pn.second});
            }
        }
        tp[d].clear();
    }
    if (dist[gx][gy] == INF) puts("-1");
    else cout << dist[gx][gy] << endl;
    return 0;
}