#include <bits/stdc++.h>
using namespace std;
char s[2000][2000];
int dd[2000][2000], sumx[2000][2000], sumy[2000][2000], sumxy[2000][2000];
const int N = 1000000007;
int h, w;
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
    }
  }
  dd[0][0] = 1;
  for (int i = 0; i < w; i++) {
    sumx[0][i] = 0, sumxy[0][i] = 0;
  }
  for (int i = 0; i < h; i++) {
    sumy[i][0] = 0, sumxy[i][0] = 0;
  }
  for (int i = 0; i < h - 1; i++) {
    if (s[i][0] == '.') {
      sumx[i + 1][0] = (sumx[i][0] + dd[i][0]) % N;
    } else {
      sumx[i + 1][0] = 0;
    }
    dd[i + 1][0] = sumx[i + 1][0];
  }
  for (int i = 0; i < w - 1; i++) {
    if (s[0][i] == '.') {
      sumy[0][i + 1] = (sumy[0][i] + dd[0][i]) % N;
    } else {
      sumy[0][i + 1] = 0;
    }
    dd[0][i + 1] = sumy[0][i + 1];
  }
  for (int i = 0; i < h - 1; i++) {
    for (int j = 0; j < w - 1; j++) {
      if (s[i][j + 1] == '.') {
        sumx[i + 1][j + 1] = (sumx[i][j + 1] + dd[i][j + 1]) % N;
      } else {
        sumx[i + 1][j + 1] = 0;
      }
      if (s[i + 1][j] == '.') {
        sumy[i + 1][j + 1] = (sumy[i + 1][j] + dd[i + 1][j]) % N;
      } else {
        sumy[i + 1][j + 1] = 0;
      }
      if (s[i][j] == '.') {
        sumxy[i + 1][j + 1] = (sumxy[i][j] + dd[i][j]) % N;
      } else {
        sumxy[i + 1][j + 1] = 0;
      }
      dd[i + 1][j + 1] = ((sumx[i + 1][j + 1] + sumy[i + 1][j + 1]) % N + sumxy[i + 1][j + 1]) % N;
    }
  }
  cout << dd[h - 1][w - 1] << endl;
  return 0;
}
/**
 *    author:  otera    
**/
#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef long long ll;
typedef long double ld;
#define rep(i, n) for(int i = 0; i < n; ++ i)
#define per(i,n) for(int i=n-1;i>=0;i--)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int inf = 1'000'000'007;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

void solve() {
    int h, w; cin >> h >> w;
    vector<vector<char>> a(h, vector<char>(w));
    int sx = -1, sy = -1, gx = -1, gy = -1;
    vector<vector<int>> tpos(26, vector<int>());
    rep(i, h) {
        rep(j, w) {
            cin >> a[i][j];
            if(a[i][j] == 'S') sx = i, sy = j;
            else if(a[i][j] == 'G') gx = i, gy = j;
            else if(a[i][j] != '.' and a[i][j] != '#') {
                int c = a[i][j] - 'a';
                tpos[c].push_back(i * w + j);
            }
        }
    }
    vector<int> dist(h * w + 26, inf);
    dist[sx * w + sy] = 0;
    priority_queue<P, vector<P>, greater<P>> pque;
    pque.push({0, sx * w + sy});
    while(pque.size()) {
        P p = pque.top(); pque.pop();
        int pos = p.sc, di = p.fr;
        if(di > dist[pos]) continue;
        if(pos < h * w) {
            int x = pos / w, y = pos % w;
            rep(k, 4) {
                int nx = x + dx[k], ny = y + dy[k];
                // cerr << nx << " " << ny << "\n";
                if(0 <= nx and nx < h and 0 <= ny and ny < w and a[nx][ny] != '#') {
                    if(chmin(dist[nx * w + ny], di + 1)) {
                        pque.push({di + 1, nx * w + ny});
                    }
                }
            }
            if(a[x][y] != '.' and a[x][y] != 'S' and a[x][y] != 'G') {
                // teleport
                int c = a[x][y] - 'a';
                if(chmin(dist[h * w + c], di)) {
                    pque.push({di, h * w + c});
                }
            }
        } else {
            int c = pos - h * w;
            for(int np: tpos[c]) {
                if(chmin(dist[np], di + 1)) {
                    pque.push({di + 1, np});
                }
            }
        }
    }
    if(dist[gx * w + gy] == inf) dist[gx * w + gy] = -1;
    cout << dist[gx * w + gy] << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(20);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}