Problem: The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back.

However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. "

You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. "

For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern)

<image>

On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern.

<image>

As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern.

Notes on Test Cases

Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format.

When n, m is 0, it indicates the end of input.

<!-



Input

The inputs include the shape of the underground warehouse and the prohibition pattern.

The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50)

Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows.

Character | Meaning
--- | ---
S | The entrance to the underground warehouse. Only one is always included per underground warehouse.
G | Magic circle. Only one is always included per underground warehouse.
. | Aisle. You can pass (if you don't take the prohibition pattern).
| Wall. You can't go through the wall.

Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10)

Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows.

Character | Meaning
--- | ---
U | ↑ move.
Move R |->.
D | ↓ movement.
L | <-Move.

The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern.

Output

Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1.

Examples

Input

7 6
......
.####.
.####.
...S#.
...##.
...##.
.....G
3
LD
DD
LLL
7 8
S#......
.#.####.
.#.#G.#.
.#.##.#.
.#....#.
.######.
........
8
DDDD
DDDU
UUUU
UUUD
RRRR
RRRL
LLLL
LLLR
3 8
########
S......G
########
2
U
D
6 10
..........
.S........
..........
..........
........G.
..........
0
6 7
.......
...#...
...#.S.
...###.
.G.....
.......
2
LL
DD
0 0


Output

13
60
7
10
-1


Input

7 6
......
.####.
.####.
...S#.
...##.
...##.
.....G
3
LD
DD
LLL


Output

13


Input

7 8
S#......
.#.####.
.#.#G.#.
.#.##.#.
.#....#.
.######.
........
8
DDDD
DDDU
UUUU
UUUD
RRRR
RRRL
LLLL
LLLR


Output

60


Input

3 8

S......G

2
U
D


Output

7


Input

6 10
..........
.S........
..........
..........
........G.
..........
0


Output

10


Input

6 7
.......
...#...
...#.S.
...###.
.G.....
.......
2
LL
DD


Output

-1


Input

7 6
......
.####.
.####.
...S#.
...##.
...##.
.....G
3
LD
DD
LLL
7 8
S#......
.#.####.
.#.#G.#.
.#.##.#.
.#....#.
.######.
........
8
DDDD
DDDU
UUUU
UUUD
RRRR
RRRL
LLLL
LLLR
3 8

S......G

2
U
D
6 10
..........
.S........
..........
..........
........G.
..........
0
6 7
.......
...#...
...#.S.
...###.
.G.....
.......
2
LL
DD
0 0


Output

13
60
7
10
-1

Solution (in CPP):

Solution:
class _in{struct my_iterator{int it;const bool rev;explicit constexpr my_iterator(int it_, bool rev=false):it(it_),rev(rev){}constexpr int operator*(){return it;}constexpr bool operator!=(my_iterator& r){return it!=r.it;}void operator++(){rev?--it:++it;}};const my_iterator i,n;public:explicit constexpr _in(int n):i(0),n(n){}explicit constexpr _in(int i,int n):i(i,n<i),n(n){}constexpr const my_iterator& begin(){return i;}constexpr const my_iterator& end(){return n;}};

#include <bits/stdc++.h>
using namespace std;

template<int Code>
struct Node_ {
    Node_* next[Code];
    Node_* fail;
    bool is_taboo;
    Node_() : fail(nullptr), is_taboo(false)
      { fill_n(next, Code, nullptr);}
};

typedef Node_<4> node_t;

const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const string urdl = "URDL"; map<char, int> encoder;
void setEncoder() { for(int i : _in(4)) encoder[urdl[i]] = i;}

void constructAC(vector<node_t>& nodes, const vector<string>& patterns) {
    node_t* root = &nodes[0];
    //Phase 1 (trie)
    int count = 0;
    for(const auto& s : patterns) {
        node_t* cur = root;
        for(const char& c : s) {
            int k = encoder[c];
            if(cur->next[k] == nullptr)
              cur->next[k] = &nodes[++count];
            cur = cur->next[k];
        }
        cur->is_taboo = true;
    }

    //Phase 2 (failue link)
    queue<node_t*> que;
    for(int i : _in(4)) {
        if(root->next[i] != nullptr) {
            root->next[i]->fail = root;
            que.emplace(root->next[i]);
        }
        else
          root->next[i] = root;
    }
    
    while(!que.empty()) {
        node_t* cur = que.front(); que.pop();
        for(int i : _in(4)) {
            if(cur->next[i] == nullptr) continue;
            que.emplace(cur->next[i]);
            node_t* f = cur->fail;
            while(f->next[i] == nullptr) f = f->fail;
            cur->next[i]->fail = f->next[i];
        }
    }
}

bool getTaboo(node_t* cur, node_t* root) {
    if(cur == root) return false;
    if(cur->is_taboo == false)
      return getTaboo(cur->fail, root);
    else
      return true;
}
using bs = bitset<2500>;
int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    setEncoder();
    int N, M;
    while(cin >> N >> M && N) {
        vector<string> fld(N);
        for(auto& s : fld) cin >> s;
        int P; cin >> P;
        vector<string> patterns(P);
        for(auto& s : patterns) cin >> s;
        vector<node_t> nodes(P * 10 + 1);
        constructAC(nodes, patterns);
        node_t* root = &nodes[0];
        int sx = -1, sy = -1;
        for(int i : _in(N))
          for(int j : _in(M))
            if(fld[i][j] == 'S'){
                sx = j, sy = i;
                break;
            }
        vector<bs> used(nodes.size());
        queue<tuple<int, int, int, int>> que;
        que.emplace(0, sx, sy, 0); used[0][sy * 50 + sx] = true;
        bool ok = false;
        while(!que.empty()) {
            int cid, cx, cy, dist;
            tie(cid, cx, cy, dist) = que.front(); que.pop();
            if(fld[cy][cx] == 'G') {
                cout << dist << endl;
                ok = true;
                break;
            }
            for(int i : _in(4)) {
                int nx = cx + dx[i], ny = cy + dy[i];
                if(nx < 0 || ny < 0 || M <= nx || N <= ny) continue;
                if(fld[ny][nx] == '#') continue;
                node_t* cur = &nodes[cid];
                while(cur->next[i] == nullptr) cur = cur->fail;
                cur = cur->next[i];
                if(getTaboo(cur, root)) continue;
                int id = cur - root;
                if(used[id][ny * 50 + nx] == true) continue;
                que.emplace(id, nx, ny, dist + 1);
                used[id][ny * 50 + nx] = true;
            }
        }
        if(!ok) cout << -1 << endl;
    }
}