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;
using ll = long long;
#define int ll
using PII = pair<int, int>;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using VVV = vector<VV<T>>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll INF = (1LL<<60);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

// D, R, U, L
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

// 文字の種類数をtemplate引数で渡す
template <int types = 26>
struct AhoCorasick {
  // trie木のnode
  struct node {
    node *fail;
    V<node*> next;
    V<int> matched;
    int idx;
    node() : fail(nullptr), next(types, nullptr) {}
    node(int idx) : fail(nullptr), next(types, nullptr), idx(idx) {}
  };

  // 辞書のサイズ, 頂点数
  int sz, nodesize;
  // trie木の根
  node *root;
  // vectorを結合
  V<int> unite(const V<int> &a, const V<int> &b) {
    V<int> ret;
    set_union(ALL(a), ALL(b), back_inserter(ret));
    return ret;
  }
  // 文字と数字の対応付けをする関数
  function<int(char)> trans;
  // 初期化
  AhoCorasick() {}
  AhoCorasick(V<string> pattern, function<int(char)> f = [](char c){return c-'a';}) {
    nodesize = 0;
    trans = f;
    build(pattern);
  }
  // 文字列集合patternからtrie木っぽいオートマトンを作成
  void build(V<string> pattern) {
    sz = pattern.size(), root = new node(nodesize++);
    node *now;
    root->fail = root;
    REP(i, sz) {
      now = root;
      for(const auto &c: pattern[i]) {
        if(now->next[trans(c)] == nullptr) {
          now->next[trans(c)] = new node(nodesize++);
        }
        now = now->next[trans(c)];
      }
      now->matched.PB(i);
    }

    queue<node*> que;
    REP(i, types) {
      if(root->next[i] == nullptr) {
        root->next[i] = root;
      } else {
        root->next[i]->fail = root;
        que.push(root->next[i]);
      }
    }
    while(que.size()) {
      now = que.front(); que.pop();
      REP(i, types) {
        if(now->next[i] != nullptr) {
          node *nxt = now->fail;
          while(!nxt->next[i]) nxt = nxt->fail;
          now->next[i]->fail = nxt->next[i];
          now->next[i]->matched = unite(now->next[i]->matched, nxt->next[i]->matched);
          que.push(now->next[i]);
        }
      }
    }
  }
  // 一文字ずつ照合していく
  node* next(node* p, const char c) {
    while(p->next[trans(c)] == nullptr) p = p->fail;
    return p->next[trans(c)];
  }
  // 文字列s中に辞書と一致する部分列がどれだけあるか
  V<int> match(const string s) {
    V<int> res(sz);
    node *now = root;
    for(auto c : s) {
      now = next(now, c);
      for(auto i : now->matched) res[i]++;
    }
    return res;
  }
};

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(1) {
    int h, w;
    cin >> h >> w;
    if(!h) break;
    V<string> board(h);
    REP(i, h) cin >> board[i];
    int n;
    cin >> n;
    V<string> v(n);
    REP(i, n) cin >> v[i];

    int sx = -1, sy = -1, gx = -1, gy = -1;
    REP(i, h) REP(j, w) {
      if(board[i][j] == 'S') {
        sx = j, sy = i;
      } else if(board[i][j] == 'G') {
        gx = j, gy = i;
      }
    }

    char dir[4] = {'D', 'R', 'U', 'L'};
    auto trans = [&](char c) {
      if(c == 'D') return 0;
      else if(c == 'R') return 1;
      else if(c == 'U') return 2;
      else if(c == 'L') return 3;
    };
    AhoCorasick<4> aho(v, trans);
    // cout << "nodesize=" << aho.nodesize << endl;
    AhoCorasick<4>::node *now = aho.root;

    struct nt {
      int y, x;
      AhoCorasick<4>::node *node;
      nt(int x, int y, AhoCorasick<4>::node *n) : y(y), x(x) { node = n; }
    };

    // d[y][x][あほこらのnode] = 最小距離
    VVV<int> d(h, VV<int>(w, V<int>(aho.nodesize, INF)));
    queue<nt> que;
    d[sy][sx][0] = 0;
    que.push(nt(sx, sy, now));
    while(que.size()) {
      nt vec = que.front(); que.pop();
      // cout << vec.x << " " << vec.y << " " << vec.node->idx << endl;
      REP(i, 4) {
        int nx = vec.x + dx[i], ny = vec.y + dy[i];
        // cout << "nx=" << nx << " ny=" << ny << endl;
        if(!IN(0LL,w,nx) || !IN(0LL,h,ny) || board[ny][nx]=='#') continue;
        AhoCorasick<4>::node *next = aho.next(vec.node, dir[i]);
        if(next == nullptr) {
          continue;
        }
        // cout << next->matched << endl;
        if(next->matched.size()) continue;
        if(d[ny][nx][next->idx] > d[vec.y][vec.x][vec.node->idx] + 1) {
          d[ny][nx][next->idx] = d[vec.y][vec.x][vec.node->idx] + 1;
          que.push(nt(nx, ny, next));
        }
      }
    }

    int ret = INF;
    REP(i, aho.nodesize) chmin(ret, d[gy][gx][i]);
    if(ret == INF) cout << -1 << endl;
    else cout << ret << endl;
  }
 
  return 0;
}