#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

string ba;
int iceid[12][12];
int num[144];
const int dy[] = {-1,0,1,0};
const int dx[] = {0,1,0,-1};
int dist[12][12][144];
int vec[144];

int gy, gx;
struct P {
  string s;
  int y,x,d;
  P(const string &s, int y, int x, int d) :
    s(s),y(y),x(x),d(d) {}
};
bool operator<(const P &a, const P &b) {
  int ad = a.d + abs(a.y-gy) + abs(a.x-gx);
  int bd = b.d + abs(b.y-gy) + abs(b.x-gx);
  return ad != bd ? ad > bd : a.s != b.s ? a.s < b.s : a.y != b.y ? a.y < b.y : a.x != b.x ?
    a.x < b.x : a.d < b.d;
}
class Cmp {
public:
  bool operator()(const P &a, const P &b) const {
    return a.s != b.s ? a.s < b.s : a.y != b.y ? a.y < b.y : a.x < b.x;
  }
};


int main() {
  int w,h;
  while(cin>>w>>h,w||h) {
    ba = "";
    int sy,sx;
    REP(i,h) {
      string s; cin >> s;
      ba += s;
      REP(j,w) {
        if (ba[i*w+j] == 'S') {
          sy=i;sx=j;
          ba[i*w+j] = '.';
        } else if (ba[i*w+j] == 'G') {
          gy = i;
          gx = j;
        }
      }
    }
    memset(num,0,sizeof(num));
    memset(iceid,0,sizeof(iceid));
    int ice = 1;
    REP(i,h) {
      REP(j,w) {
        if (ba[i*w+j] != 'X') continue;
        if (iceid[i][j] == 0) {
          iceid[i][j] = ice;
          queue<pii> Q;
          Q.push(pii(i,j));
          while(!Q.empty()) {
            pii p = Q.front(); Q.pop();
            num[ice]++;
            int y = p.first;
            int x = p.second;
            REP(k,4) {
              int yy=y+dy[k];
              int xx=x+dx[k];
              if (yy<0||yy>=h||xx<0||xx>=w) continue;

              if (ba[yy*w+xx] == 'X' && iceid[yy][xx] == 0) {
                iceid[yy][xx] = ice;
                Q.push(pii(yy,xx));
              }
            }
          }
          ice++;
        }
      }
    }
    priority_queue<P> Q;
    map<P, int, Cmp> dist;
    Q.push(P(ba,sy,sx,0));
    dist[P(ba,sy,sx,0)] = 0;
    int ans = -1;
    while(!Q.empty()) {
      P p = Q.top(); Q.pop();
      if (p.d > dist[p]) continue;
      
      int id = iceid[p.y][p.x];
      if (p.y == gy && p.x == gx) {
        ans = p.d;
        break;
      }
      REP(i,ice) vec[i] = 0;
      REP(i,h)REP(j,w) if (p.s[i*w+j]=='*') vec[iceid[i][j]]++;

      // if (p.y == 8 && p.x == 1 && p.d == 11) {
      //   cout << p.y << " " << p.x << "  " << p.d << endl;
      //   REP(i,ice) cout << vec[i] << " "; cout << endl;
      //   REP(i,h) cout << p.s.substr(i*w,w) << endl;
      // }

      
      REP(k,4) {
        int yy=p.y+dy[k];
        int xx=p.x+dx[k];
        if (yy<0||yy>=h||xx<0||xx>=w) continue;
        if (p.s[yy*w+xx] == '#') continue;
        int id2 = iceid[yy][xx];
        if (p.s[yy*w+xx] == 'X') {
          if (2*(vec[id2]+1) <= num[id2]) {
            p.s[yy*w+xx] = '*';
            P next(p.s,yy,xx,p.d+1);
            if (dist.count(next) == 0 || dist[next] > p.d+1) {
              dist[next] = p.d+1;
              Q.push(next);
            }
            p.s[yy*w+xx] = 'X';
          }
        } else {
          // char c = p.s[yy*w+xx];
          // if (p.s[yy*w+xx] != '*') p.s[yy*w+xx] = '-';

          P next(p.s,yy,xx,p.d+1);
          if (dist.count(next) == 0 || dist[next] > p.d+1) {

            dist[next] = p.d+1;
            Q.push(next);
          }
          // p.s[yy*w+xx] = c;
        }
      }
    }
    assert(ans != -1);
    cout << ans << endl;
  }
}

Measured memory_bytes: 1644