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:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}
class AhoCorasick {
    void clear_graph() {
        root.child.clear();
        root.pattern.clear();
    }
    void generate_trie(const vector<string>& patterns) {
        ll n = patterns.size();
        rep(i, n) {
            Node* t = &root;
            each(c, patterns[i]) {
                if (t->child.count(c) == 0) {
                    t->child[c] = Node(nodes.size());
                    nodes.pb(&t->child[c]);
                }
                t = &(t->child[c]);
            }
            t->pattern.push_back(i);
        }
    }
    void add_failure_edge() {
        queue<Node*> Q; Q.push(&root);
        //幅優先探索で帰納的に失敗時の遷移辺を追加していく
        while (!Q.empty()) {
            Node* t = Q.front(); Q.pop();
            each(p, t->child) {
                Q.push(&(p.second));
                char c = p.first;
                Node* node = &(p.second); // 文字cで遷移する頂点
                Node* anode = t->failure; // 失敗したときの遷移先
                while ( anode != NULL && anode->child.count(c) == 0 ) {
                    anode = anode->failure;
                }
                //遷移失敗時に続けられる別の頂点へ遷移
                if (anode == NULL) {
                    node->failure = &root;
                }
                else {
                    node->failure = &(anode->child[c]);
                }
                //マッチするパターンを追加
                each(to, node->failure->pattern) {
                    node->pattern.pb(to);
                }
                //メモリ食いすぎ回避のため切り詰めておく
                vector<size_t>(node->pattern).swap(node->pattern);
            }
        }
    }
    //Pattern Match Automatonを構築
    void make_PMA(const vector<string>& patterns) {
        clear_graph();
        generate_trie(patterns);
        add_failure_edge();
    }
public:
    struct Node {
        ll id;
        map<char,Node> child; //遷移辺(!)
        vector<size_t> pattern; //マッチするパターン(のindex)
        Node* failure; //遷移失敗時の遷移先ノード
        Node():failure(NULL) {
        }
        Node(ll id):failure(NULL), id(id) {}
    };
    vector<Node*> nodes;
    Node root;
    AhoCorasick(const vector<string>& patterns) : nodes(0), root(0) {
        nodes.assign(1, &root);
        make_PMA(patterns);
    }
    pair<Node*, vector<ll>> find(Node* node, char c) {
        vector<ll> res;
        while (node != NULL && node->child.count(c) == 0) {
            node = node->failure;
        }
        if (node == NULL) node = &root;
        else node = &(node->child[c]);
        each(ptn, node->pattern) {
            res.pb(ptn);
        }
        return pair<Node*, vector<ll>>(node, res);
    }
};

const string dname = "URDL";
const ll dx[] = {0, 1, 0, -1};
const ll dy[] = {-1, 0, 1, 0};

struct Node2 {
    ll x, y, nid;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll h, w;
    while (cin >> h >> w, h || w) {
        vector<string> m(h); cin >> m;
        ll patterns; cin >> patterns;
        vector<string> ptn(patterns); cin >> ptn;
        AhoCorasick ac(ptn);
        auto inRange = [&](ll x, ll y) {
            return 0 <= x && x < w && 0 <= y && y < h;
        };
        ll sx, sy, gx, gy;
        rep(y, h) rep(x, w) {
            if (m[y][x] == 'S') sx = x, sy = y;
            if (m[y][x] == 'G') gx = x, gy = y;
        }
        ll n = ac.nodes.size();
        vector<vector<vector<ll>>> dist(h, vector<vector<ll>>(w, vector<ll>(n, linf)));
        queue<Node2> Q;
        dist[sy][sx][0] = 0;
        Q.push({sx, sy, 0});
        while ( !Q.empty() ) {
            Node2 node = Q.front(); Q.pop();
            ll x = node.x, y = node.y, nid = node.nid;
            AhoCorasick::Node* ac_node = ac.nodes[nid];
            rep(d, 4) {
                ll nx = x + dx[d];
                ll ny = y + dy[d];
                if ( !inRange(nx, ny) ) continue;
                if ( m[ny][nx] == '#' ) continue;
                auto result = ac.find(ac_node, dname[d]);
                if ( result.second.size() > 0 ) continue;
                ll nnid = result.first->id;
                assert(nnid < n);
                if (dist[y][x][nid]+1 < dist[ny][nx][nnid]) {
                    dist[ny][nx][nnid] = dist[y][x][nid]+1;
                    Q.push({nx, ny, nnid});
                }
            }
        }
        ll ans = linf;
        rep(i, n) chmin(ans, dist[gy][gx][i]);
        if (ans == linf) cout << -1 << endl;
        else cout << ans << endl;
    }
}