#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstring>

using namespace std;

struct Entry {
        int x, y, state, step;
};

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
const char *LABELS[] = { "L", "R", "U", "D" };

class Matcher {
        struct PMA {
                int next[256];
                vector<int> accept;
                PMA(){ fill(next, next + 256, -1); }
        };
        vector<PMA> states;
public:
        Matcher(const vector<string> &patterns){
                states.push_back(PMA());
                for(int i = 0; i < patterns.size(); ++i){
                        int cur = 0;
                        for(size_t j = 0; j < patterns[i].size(); ++j){
                                char c = patterns[i][j];
                                if(states[cur].next[c] < 0){
                                        states[cur].next[c] = static_cast<int>(states.size());
                                        states.push_back(PMA());
                                }
                                cur = states[cur].next[c];
                        }
                        states[cur].accept.push_back(i);
                }
                queue<int> q;
                for(int c = 1; c < 256; ++c){
                        if(states[0].next[c] >= 0){
                                states[states[0].next[c]].next[0] = 0;
                                q.push(states[0].next[c]);
                        }else{
                                states[0].next[c] = 0;
                        }
                }
                while(!q.empty()){
                        int t = q.front();
                        q.pop();
                        for(int c = 1; c < 256; ++c){
                                if(states[t].next[c] >= 0){
                                        q.push(states[t].next[c]);
                                        int r = states[t].next[0];
                                        while(states[r].next[c] < 0){ r = states[r].next[0]; }
                                        states[states[t].next[c]].next[0] = states[r].next[c];
                                }
                        }
                }
                for(int i = 0; i < patterns.size(); ++i){
                        const string &pat = patterns[i];
                        for(int j = 1; j < states.size(); ++j){
                                int current = j, k = 0;
                                for(; k < pat.size(); ++k){
                                        if(states[current].next[pat[k]] < 0){ break; }
                                        current = states[current].next[pat[k]];
                                }
                                if(k == pat.size()){ states[current].accept.push_back(i); }
                        }
                }
        }
        int match(const string &str, vector<int> &result, int state = 0){
                for(size_t i = 0; i < str.size(); ++i){
                        char c = str[i];
                        while(states[state].next[c] < 0){ state = states[state].next[0]; }
                        state = states[state].next[c];
                        for(size_t j = 0; j < states[state].accept.size(); ++j){
                                ++(result[states[state].accept[j]]);
                        }
                }
                return state;
        }
        int stateNum() const { return static_cast<int>(states.size()); }
};

bool passed[52][52][101];

int main(){
        while(true){
                int N, M;
                cin >> N >> M;
                if(N == 0 && M == 0){ break; }
                memset(passed, 0, sizeof(passed));
                vector<string> field(N + 2);
                field[0] = field[N + 1] = string(M + 2, '#');
                Entry start;
                int goalX = 0, goalY = 0;
                for(int i = 1; i <= N; ++i){
                        string line;
                        cin >> line;
                        field[i] = "#" + line + "#";
                        for(int j = 1; j <= M; ++j){
                                if(field[i][j] == 'S'){
                                        start.x = j;
                                        start.y = i;
                                        start.state = 0;
                                        start.step = 0;
                                }else if(field[i][j] == 'G'){
                                        goalX = j;
                                        goalY = i;
                                }
                        }
                }
                int P;
                cin >> P;
                vector<string> patterns(P);
                for(int i = 0; i < P; ++i){ cin >> patterns[i]; }
                Matcher matcher(patterns);
                queue<Entry> q;
                q.push(start);
                passed[start.y][start.x][start.state] = true;
                int answer = -1;
                vector<int> result(P);
                while(!q.empty()){
                        Entry e = q.front();
//cerr << "> " << e.x << " " << e.y << " " << e.state << endl;
                        if(e.x == goalX && e.y == goalY){
                                answer = e.step;
                                break;
                        }
                        q.pop();
                        for(int i = 0; i < 4; ++i){
                                Entry next;
                                next.x = e.x + dx[i];
                                next.y = e.y + dy[i];
                                if(field[next.y][next.x] == '#'){ continue; }
                                next.step = e.step + 1;
                                fill(result.begin(), result.end(), 0);
                                next.state = matcher.match(LABELS[i], result, e.state);
                                if(
                                        accumulate(result.begin(), result.end(), 0) != 0 ||
                                        passed[next.y][next.x][next.state]
                                ){
                                        continue;
                                }
                                passed[next.y][next.x][next.state] = true;
                                q.push(next);
                        }
                }
                cout << answer << endl;
        }
        return 0;
}

Measured memory_bytes: 0