#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class PMA
{
    int n;
    vector<vector<int> > next;
    vector<bitset<32> > match;
    vector<int> failure;
public:
    PMA(const vector<string>& pattern){
        next.assign(1, vector<int>(128, -1));
        match.assign(1, 0);
        for(unsigned i=0; i<pattern.size(); ++i){
            int curr = 0;
            for(unsigned j=0; j<pattern[i].size(); ++j){
                if(next[curr][pattern[i][j]] == -1){
                    next[curr][pattern[i][j]] = next.size();
                    next.push_back(vector<int>(128, -1));
                    match.push_back(0);
                }
                curr = next[curr][pattern[i][j]];
            }
            match[curr][i] = true;
        }

        n = next.size();
        failure.resize(n, 0);
        vector<int> node1(1, 0);
        while(!node1.empty()){
            vector<int> node2;
            for(unsigned i=0; i<node1.size(); ++i){
                for(int j=0; j<128; ++j){
                    int s = node1[i];
                    if(next[s][j] == -1)
                        continue;
                    node2.push_back(next[s][j]);
                    int t = s;
                    while(t != 0){
                        if(next[failure[t]][j] != -1){
                            failure[next[s][j]] = next[failure[t]][j];
                            match[next[s][j]] |= match[next[failure[t]][j]];
                            break;
                        }
                        t = failure[t];
                    }
                }
            }
            node1.swap(node2);
        }
    }
    int size(){
        return n;
    }
    int transition(int curr, char c){
        if(next[curr][c] != -1)
            return next[curr][c];
        if(curr == 0)
            return 0;
        return transition(failure[curr], c);
    }
    bitset<32> checkMatch(int curr){
        return match[curr];
    }
};

char dc[] = "URDL";
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};

int main()
{
    for(;;){
        int h, w;
        cin >> h >> w;
        if(h == 0)
            return 0;

        vector<string> grid(h+2, string(w+2, '#'));
        int sy, sx, gy, gx;
        for(int i=1; i<=h; ++i){
            for(int j=1; j<=w; ++j){
                cin >> grid[i][j];
                if(grid[i][j] == 'S'){
                    sy = i;
                    sx = j;
                    grid[i][j] = '.';
                }
                if(grid[i][j] == 'G'){
                    gy = i;
                    gx = j;
                    grid[i][j] = '.';
                }
            }
        }

        int p;
        cin >> p;
        vector<string> pattern(p);
        for(int i=0; i<p; ++i)
            cin >> pattern[i];

        PMA pma(pattern);
        int n = pma.size();
        vector<vector<vector<int> > > minCost(h+2, vector<vector<int> >(w+2, vector<int>(n, INT_MAX)));
        minCost[sy][sx][0] = 0;
        queue<pair<pair<int, int>, int> > q;
        q.push(make_pair(make_pair(sy, sx), 0));
        bool ng = true;
        while(!q.empty()){
            int y0 = q.front().first.first;
            int x0 = q.front().first.second;
            int s = q.front().second;
            int cost = minCost[y0][x0][s];
            q.pop();

            if(y0 == gy && x0 == gx){
                cout << cost << endl;
                ng = false;
                break;
            }

            for(int i=0; i<4; ++i){
                int y = y0 + dy[i];
                int x = x0 + dx[i];
                int t = pma.transition(s, dc[i]);
                if(grid[y][x] == '#' || minCost[y][x][t] < INT_MAX || pma.checkMatch(t).any())
                    continue;
                minCost[y][x][t] = cost + 1;
                q.push(make_pair(make_pair(y, x), t));
            }
        }

        if(ng)
            cout << -1 << endl;
    }
}

Measured memory_bytes: 1520