#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

using namespace std;
int fi[64][64];
int main(void)
{
  int r,c;
  vector<string> field;
  cin >> r >> c;
  field.resize(r);
  for(int i=0;i<r;++i) cin >> field[i];
  for(int i=0;i<64;++i)
    for(int j=0;j<64;++j)
      fi[i][j]=-3;
  int si,sj,gi,gj;
  for(int i=0;i<r;++i)
    for(int j=0;j<c;++j){
      if(field[i][j]=='.') fi[i+10][j+10]=1;
      if(field[i][j]=='#') fi[i+10][j+10]=-3;
      if(field[i][j]=='o') fi[i+10][j+10]=0;
      if(field[i][j]=='x') fi[i+10][j+10]=2;
      if(field[i][j]=='S'){
	fi[i+10][j+10]=1;
	si=i+10;
	sj=j+10;
      }
      if(field[i][j]=='G'){
	fi[i+10][j+10]=1;
	gi=i+10;
	gj=j+10;
      }
    }
  priority_queue<tuple<int,int,int> > dijk;
  set<tuple<int,int> > already;
  // -cost,i,j;
  dijk.push(make_tuple(0,si,sj));
  while(!dijk.empty()){
    int negc,h,w;
    tie(negc,h,w)=dijk.top();
    dijk.pop();
    if(!already.insert(make_tuple(h,w)).second) continue;
    if(h==gi&&w==gj){
      cout << -negc << endl;
      return 0;
    }
    if(fi[h+1][w]/2==0) dijk.push(make_tuple(negc-1-fi[h+1][w],h+1,w));
    if(fi[h-1][w]/2==0) dijk.push(make_tuple(negc-1-fi[h-1][w],h-1,w));
    if(fi[h][w+1]/2==0) dijk.push(make_tuple(negc-1-fi[h][w+1],h,w+1));
    if(fi[h][w-1]/2==0) dijk.push(make_tuple(negc-1-fi[h][w-1],h,w-1));
    for(int i=1;i<=3;++i){
      if(fi[h+i][w]<0) break;
      dijk.push(make_tuple(negc-2-i,h+i,w));
    }
    for(int i=1;i<=3;++i){
      if(fi[h-i][w]<0) break;
      dijk.push(make_tuple(negc-2-i,h-i,w));
    }
    for(int i=1;i<=3;++i){
      if(fi[h][w+i]<0) break;
      dijk.push(make_tuple(negc-2-i,h,w+i));
    }
    for(int i=1;i<=3;++i){
      if(fi[h][w-i]<0) break;
      dijk.push(make_tuple(negc-2-i,h,w-i));
    }
    
  }
}