#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#define  _SCL_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream> 
#include<map>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cstdint>
#define REP(i,n) for(int i = 0;i < n;++i)
using namespace std;
vector<pair<int, int>> red;
bool dp[2][2000][550][550] = {};
int main() {
	int h, w;
	scanf("%d%d", &h, &w);
	pair<int, int> start, end;
	REP(i, h) {
		string hoge;
		cin >> hoge;
		for (int q = 0;q < w;++q) {
			if (hoge[q] == 'S') {
				start = make_pair(i, q);
			}
			else if (hoge[q] == 'G') {
				end = make_pair(i, q);
			}
			else if (hoge[q] == 'R') {
				red.push_back(make_pair(i, q));
			}
		}
	}
	dp[0][0][start.first][start.second] = true;
	for (int i = 1;i < 1200;++i) {
		for (int q = 0;q < h;++q) {
			for (int j = 0;j < w;++j) {
				if (dp[1][i - 1][q][j] == true) {
					//ミニビショップ存在
					if (q + 1 < h&&j+1 < w) {
						dp[1][i][q + 1][j + 1] = true;
					}
					if (q + 1 < h&&j - 1 > -1) {
						dp[1][i][q + 1][j - 1] = true;
					}
					if (q - 1 > -1 && j - 1 > -1) {
						dp[1][i][q - 1][j - 1] = true;
					}
					if (q - 1 > -1 && j + 1 < w) {
						dp[1][i][q - 1][j + 1] = true;
					}
				}
				if (dp[0][i - 1][q][j] != 0) {
					//ナイト存在
					if (q + 2 < h&&j + 1 < w) {
						dp[0][i][q + 2][j + 1] = true;
					}
					if (q + 1 < h&&j + 2 < w) {
						dp[0][i][q + 1][j + 2] = true;
					}
					if (q - 1 > -1&&j + 2 < w) {
						dp[0][i][q - 1][j + 2] = true;
					}
					if (q - 2 > -1&&j + 1 < w) {
						dp[0][i][q - 2][j + 1] = true;
					}
					if (q - 2 > -1 && j - 1 > -1) {
						dp[0][i][q - 2][j - 1] = true;
					}
					if (q - 1 > -1 && j - 2 > -1) {
						dp[0][i][q - 1][j - 2] = true;
					}
					if (q + 1 < h&&j - 2 > -1) {
						dp[0][i][q + 1][j - 2] = true;
					}
					if (q + 2 < h&&j - 1 > -1) {
						dp[0][i][q + 2][j - 1] = true;
					}
				}
			}
		}
		for (int q = 0;q < red.size();++q) {
			swap(dp[0][i][red[q].first][red[q].second], dp[1][i][red[q].first][red[q].second]);
		}
		if (dp[0][i][end.first][end.second] == true || dp[1][i][end.first][end.second] == true) {
			cout << i << endl;
			return 0;
		}
	}
	cout << -1 << endl;
}