#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=2e3+5;
ll dp[maxn][maxn],sum[3][maxn*2],belong[maxn][maxn];
char buff[maxn][maxn];
const int mod=1e9+7;
int main() {
    int n,m;cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>(buff[i]+1);
    for(int i=1;i<=m;i++) belong[1][i]=i;
    for(int i=2;i<=n;i++) belong[i][1]=m+i-1;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            if(belong[i][j]) continue;
            belong[i][j]=belong[i-1][j-1];
        }
    }
    dp[1][1]=1;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            if(buff[i][j]=='#') {
                sum[0][belong[i][j]]=sum[1][i]=sum[2][j]=0;
            }else {
                if(i!=1||j!=1) dp[i][j]=(sum[0][belong[i][j]]+sum[1][i]+sum[2][j])%mod;
                (sum[0][belong[i][j]]+=dp[i][j])%=mod;
                (sum[1][i]+=dp[i][j])%=mod;
                (sum[2][j]+=dp[i][j])%=mod;
            }
        }
    }
    cout<<dp[n][m]%mod<<endl;
    return 0;
}
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <random>
#include <stack>
#include <set>
#include <list>
#include <unordered_set>

#define bug(x) cout<<"zdongdebug1: "<<x<<endl;
#define bug2(x, y) cout<<"zdongdebug2: "<<x<<" "<<y<<endl;
#define bug3(x, y, z) cout<<"zdongdebug3: "<<x<<" "<<y<<" "<<z<<endl;
using namespace std;
typedef long long ll;
const int maxn = 2005;
const int mod = 1000000007;

int d[maxn][maxn];
string s[maxn];
vector<pair<int,int>>a[26];

int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  //freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n,m;
  cin>>n>>m;
  int sx,sy,tx,ty;
  for(int i=0;i<n;i++){
    cin>>s[i];
    for(int j=0;j<m;j++){
      auto tt = s[i][j];
      if(tt>='a'&&tt<='z')a[tt-'a'].push_back(make_pair(i,j));
      else if(tt=='S')sx=i,sy=j;
      else if(tt=='G')tx=i,ty=j;
    }
  }
  int mask=0;
  queue<int>qx;
  queue<int>qy;
  memset(d,-1,sizeof(d));
  d[sx][sy] = 0;
  qx.push(sx);
  qy.push(sy);
  while(!qx.empty()){
    int x = qx.front();
    qx.pop();
    int y = qy.front();
    qy.pop();
    auto tt = s[x][y];
    if(tt>='a'&&tt<='z'&&!(mask&(1<<(tt-'a')))){
      mask|=(1<<(tt-'a'));
      auto tmp = d[x][y];
      for(auto pr: a[tt-'a']){
        if(d[pr.first][pr.second]==-1){
          d[pr.first][pr.second]=tmp+1;
          qx.push(pr.first);
          qy.push(pr.second);
        }
      }
    }
    for(int i=0;i<4;i++){
      int nx = x+dir[i][0];
      int ny = y+dir[i][1];
      if(nx<0||nx>=n||ny<0||ny>=m||s[nx][ny]=='#'||d[nx][ny]!=-1)continue;
      d[nx][ny] = d[x][y] + 1;
      qx.push(nx);
      qy.push(ny);
    }
  }
  cout<<d[tx][ty]<<endl;
  return 0;
}