#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
#include<stack>
#include<list>
#include<queue>
#include<set>
#include<string>
#include<string.h>
#include <sstream>
#include<map>

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")

#define ll long long
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
using namespace std;

const ll MOD = 1000000007;
const ll mod = 1000000007;
const int Nmax=1000005;

ll gcd(ll a,ll b)
{
    if(a==0){
        return b;
    }

    return gcd(b%a,a);
}

int lcm(int a,int b)
{
    return (a*b)/gcd(a,b);
}

bool isPrime(ll n){
	for(int i = 2; i <=sqrt(n); i++)
		if(n % i == 0)
			return 0;
	return 1;
}

void binaryRepresentation(int x)
{
    for (int i = 31; i >= 0; i--)
    {
        if (x&(1<<i)) cout << "1";
        else cout << "0";
    }
}

ll combination(ll n,ll r){
    if(r==n || r==0)
    {
        return 1LL;
    }

    return combination(n-1,r-1) + combination(n-1,r);
}

bool isPowerOfTwo(ll n)
{
   if(n==0)
        return false;

   return (ceil(log2(n)) == floor(log2(n)));
}

ll power(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a);
		b/=2;
		a=(a*a);
	}
	return ans;
}

ll inverse(ll i)
{
    if(i==1) return 1;
    return (MOD-((MOD/i)*inverse(MOD%i))%MOD+MOD)%MOD;
}

bool cmp(const pair<ll,ll> &a,const pair<ll,ll> &b)
{
    if(a.first==b.first)
        return a.second<b.second;
    return a.first>b.first;
}

string toString(ll a)
{
    ostringstream temp;
    temp << a;
    return temp.str();
}


int main()
{
    //vector<vector<int> >ar(n,vector<int> (m,0));
    //vector<int>::iterator ptr;

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll h,w,x,y;
    cin>>h>>w>>x>>y;
    --x;--y;
    vector<string>ar(h);
    for(ll i=0;i<h;i++)
        cin>>ar[i];
    ll ans=0;
    if(ar[x][y]=='.')
        ans++;
    for(ll i=x;i<=x;i++)
    {
        bool flag=true;
        for(ll j=y+1;j<w;j++)
        {
            if(ar[i][j]=='.')
                ans++;
            else
            {
                flag=false;
                break;
            }
        }
    }

    for(ll i=x;i<=x;i++)
    {
        bool flag=true;
        for(ll j=y-1;j>=0;j--)
        {
            if(ar[i][j]=='.')
                ans++;
            else
            {
                flag=false;
                break;
            }
        }
    }

    for(ll i=y;i<=y;i++)
    {
        bool flag=true;
        for(ll j=x+1;j<h;j++)
        {
            if(ar[j][i]=='.')
                ans++;
            else
            {
                flag=false;
                break;
            }
        }
    }

    for(ll i=y;i<=y;i++)
    {
        bool flag=true;
        for(ll j=x-1;j>=0;j--)
        {
            if(ar[j][i]=='.')
                ans++;
            else
            {
                flag=false;
                break;
            }
        }
    }

    cout<<ans;
    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;
}