#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
template <class X>
void print(X x)
{
    cout << x << endl;
}
void print(vl x)
{
    for (ll i : x)
    {
        cout << i << " ";
    }
    cout << endl;
}
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007 /*998244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v, m;
ll codeforces = 1;
string S, T;
vector<vl> g(MAX_N * 10);
vl A, B;
ll masu[2001][2001];
vector<vector<PP>> V(26);
vl dis = vl(MAX_N * 10, INF);
void input()
{
    cin >> h >> w;
    rep(i, h)
    {
        cin >> S;
        rep(j, w)
        {
            if (S[j] == 'S')
            {
                e = i;
                f = j;
            }
            else if (S[j] == 'G')
            {
                r = i;
                t = j;
            }
            else if (S[j] == '#')
            {
                masu[i][j] = -1;
            }
            else if (S[j] != '.')
            {
                V[S[j] - 'a'].pb({i, j});
                masu[i][j] = S[j] - 'a' + 1;
            }
        }
    }
}
void solve()
{
    rep(i, h)
    {
        rep(j, w)
        {
            if (i > 0 && masu[i][j] != -1 && masu[i - 1][j] != -1)
                g[i * w + j].pb((i - 1) * w + j);
            if (i < h - 1 && masu[i][j] != -1 && masu[i + 1][j] != -1)
                g[i * w + j].pb((i + 1) * w + j);
            if (j > 0 && masu[i][j] != -1 && masu[i][j - 1] != -1)
                g[i * w + j].pb(i * w + j - 1);
            if (j < w - 1 && masu[i][j] != -1 && masu[i][j + 1] != -1)
                g[i * w + j].pb(i * w + j + 1);
        }
    }

    queue<ll> q;
    q.push(e * w + f);
    dis[e * w + f] = 0;
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        for (ll x : g[v])
        {
            if (dis[x] == INF)
            {
                dis[x] = dis[v] + 1;
                q.push(x);
            }
        }

        a = v / w;
        b = v % w;
        //print({v, a, b, masu[a][b]});
        if (masu[a][b] > 0)
        {
            p = masu[a][b] - 1;
            rep(i, V[p].size())
            {
                e = V[p][i].fi;
                f = V[p][i].se;
                masu[e][f] = 0;
                if (dis[e * w + f] == INF)
                {
                    dis[e * w + f] = dis[v] + 1;
                    q.push(e * w + f);
                }
            }
        }

        masu[a][b] = 0;
    }

    if (dis[r * w + t] < INF)
        print(dis[r * w + t]);
    else
        print(-1);
}
int main()
{
    // cout<<fixed<<setprecision(15);
    cin.tie(0);
    ios::sync_with_stdio(false);
    input();
    while (codeforces--)
    {
        solve();
    }
}
#include<iostream>
#include<cstdio>
using namespace std;
const int N=5002020,INF=2e9;
int n,m,cnt,tot; char s[2020][2020];
int id[1000],v[N],dis[N],stk[N<<3]; bool vis[N];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
struct Edge{int to,next,w;}e[N<<4];
void add(int x,int y,int z){
	e[++tot].to=y; e[tot].next=v[x]; v[x]=tot; e[tot].w=z;
}
int get(int x,int y){return (x-1)*m+y;}
bool check(int x,int y){
	if(x<=n && y<=m && x>=1 && y>=1 && s[x][y]!='#')return 1;
	return 0;
}
void spfa(int x){
	for(int i=1;i<=cnt;i++)dis[i]=INF;
	dis[x]=0; int top; stk[top=1]=x;
	for(int i=1;i<=top;i++){
		int w=stk[i]; vis[w]=0;
		for(int p=v[w];p;p=e[p].next){
			int kp=e[p].to;
			if(dis[kp]>dis[w]+e[p].w){
				dis[kp]=dis[w]+e[p].w;
				if(!vis[kp])vis[stk[++top]=kp]=1;
			}
		}
	}
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		scanf("%s",s[i]+1);
	cnt=get(n,m);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(s[i][j]=='#')continue;
			for(int k=0;k<4;k++){
				int x=dx[k]+i,y=dy[k]+j;
				if(check(x,y))add(get(i,j),get(x,y),1),add(get(x,y),get(i,j),1);
			}
			if(s[i][j]>='a' && s[i][j]<='z'){
				if(!id[s[i][j]])id[s[i][j]]=++cnt;
				add(get(i,j),id[s[i][j]],1); add(id[s[i][j]],get(i,j),0);
			}
		}
	}
	int bg;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(s[i][j]=='S')bg=get(i,j);
	spfa(bg);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(s[i][j]=='G'){
				if(dis[get(i,j)]<INF)printf("%d\n",dis[get(i,j)]);
				else puts("-1");
			}
	return 0;
}