#include <iostream>
#include <vector>
#include <queue>

constexpr int INF = (1 << 29);

struct State {
    int cost, x, y;
    State (int cost, int x, int y) : cost{cost}, x{x}, y{y} {}
    bool operator < (const State &s) const {
        return cost > s.cost;
    }
};

bool in_field(int x, int y, int H, int W)
{
    return (0 <= x && x < W && 0 <= y && y < H);
}

int main()
{
    int H, W;
    int sx, sy, gx, gy;
    
    std::cin >> H >> W;
    std::vector<std::string> a(H);
    for (int i = 0; i < H; i++) {
        std::cin >> a[i];
        for (int j = 0; j < W; j++) {
            if (a[i][j] == 'S') {
                sx = j; sy = i;
                a[i][j] = '.';
            } else if (a[i][j] == 'G') {
                gx = j; gy = i;
                a[i][j] = '.';
            }
        }
    }
    
    std::priority_queue<State> pq;
    pq.push(State{0, sx, sy});
    
    std::vector<std::vector<int>> d(H, std::vector<int>(W, INF));
    d[sy][sx] = 0;
    
    constexpr int dx[4] = {-1, +0, +1, +0};
    constexpr int dy[4] = {+0, -1, +0, +1};
    
    while (!pq.empty()) {
        State s = pq.top(); pq.pop();
        int x = s.x, y = s.y;
               
        if (d[y][x] < s.cost) { continue; }
        
        if (x == gx && y == gy) { break; }

        for (int i = 0; i < 4; i++) {
            int nx, ny, ncost;
            for (int j = 1; j <= 3; j++) {
                nx = x + dx[i] * j, ny = y + dy[i] * j;
                
                if (!in_field(nx, ny, H, W) || a[ny][nx] == '#') {
                    break;
                }
                
                ncost = d[y][x] + j + 2;
                if (ncost < d[ny][nx]) {
                    d[ny][nx] = ncost;
                    pq.push(State(d[ny][nx], nx, ny));
                }
            }
            
            nx = x + dx[i], ny = y + dy[i];
            if (!in_field(nx, ny, H, W) || a[ny][nx] == '#' ||
                a[ny][nx] == 'x') {
                continue;
            }
            
            ncost = d[y][x] + (a[ny][nx] == 'o' ? 1 : 2);
            if (ncost < d[ny][nx]) {
                d[ny][nx] = ncost;
                pq.push(State(d[ny][nx], nx, ny));
            }
        }
    }
    
    std::cout << d[gy][gx] << std::endl;
    
    return 0;
}