import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		char[][] field = new char[h][];
		int sr = 0;
		int sx = 0;
		int gr = 0;
		int gc = 0;
		int[][] kCosts = new int[h][w];
		int[][] bCosts = new int[h][w];
		for (int i = 0; i < h; i++) {
		    field[i] = sc.next().toCharArray();
    		Arrays.fill(kCosts[i], Integer.MAX_VALUE);
    		Arrays.fill(bCosts[i], Integer.MAX_VALUE);
		    for (int j = 0; j < w; j++) {
		        if (field[i][j] == 'S') {
		            sr = i;
		            sx = j;
		        } else if (field[i][j] == 'G') {
		            gr = i;
		            gc = j;
		        }
		    }
		}
		PriorityQueue<Path> queue = new PriorityQueue<>();
		queue.add(new Path(sr, sx, 0, true));
		while (queue.size() > 0) {
		    Path p = queue.poll();
		    if (p.row < 0 || p.row >= h || p.col < 0 || p.col >= w) {
		        continue;
		    }
		    if (p.isKnight) {
		        if (kCosts[p.row][p.col] <= p.value) {
		            continue;
		        }
		        kCosts[p.row][p.col] = p.value;
		    } else {
		        if (bCosts[p.row][p.col] <= p.value) {
		            continue;
		        }
		        bCosts[p.row][p.col] = p.value;
		    }
		    if (field[p.row][p.col] == 'R') {
		        p.isKnight ^= true;
		    }
		    if (p.isKnight) {
		        queue.add(new Path(p.row - 2, p.col - 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row - 2, p.col + 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 2, p.col - 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 2, p.col + 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row - 1, p.col - 2, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row - 1, p.col + 2, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 1, p.col - 2, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 1, p.col + 2, p.value + 1, p.isKnight));
		    } else {
		        queue.add(new Path(p.row - 1, p.col - 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row - 1, p.col + 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 1, p.col - 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 1, p.col + 1, p.value + 1, p.isKnight));
		    }
		}
		int ans = Math.min(kCosts[gr][gc], bCosts[gr][gc]);
		if (ans == Integer.MAX_VALUE) {
		    System.out.println(-1);
		} else {
		    System.out.println(ans);
		}
	}
	
	static class Path implements Comparable<Path> {
	    int row;
	    int col;
	    int value;
	    boolean isKnight;
	    
	    public Path(int row, int col, int value, boolean isKnight) {
	        this.row = row;
	        this.col = col;
	        this.value = value;
	        this.isKnight = isKnight;
	    }
	    
	    public int compareTo(Path another) {
	        return value - another.value;
	    }
	}
}