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:
// 12:17 - 13:14
#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cassert>
#include<climits>
#include<map>
#include<queue>
#include<set>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define IINF (INT_MAX)
#define MAX_SP  15
#define MAX_LEN 60

using namespace std;

typedef pair<int,int> ii;
struct Data{
  int cur,cost;
  ii info;
  string debug;
  Data(int cur=IINF,int cost=IINF,ii info=ii(IINF,IINF),string debug="$$$"):cur(cur),cost(cost),info(info),debug(debug){}
  bool operator < (const Data& a)const{
    return cost > a.cost;
  }
};

int h,w,sp,gp,P;
char G[MAX_LEN][MAX_LEN];
/*
  next_state[index][the number of match]
  0 -> empty
  1 -> spell[0]
  2 -> spell[1]
  ...
 */
ii next_state[MAX_SP][MAX_SP][4];
int mincost[MAX_LEN*MAX_LEN][MAX_SP][MAX_SP];
string spell[MAX_SP];
int  dx[] = {+0,+1,+0,-1};
int  dy[] = {-1,+0,+1,+0};
char dc[] = {'U','R','D','L'};

ii getValue(string s){
  rep(i,P+1){
    int cur = 0;
    rep(j,spell[i].size()){
      if(s[cur] != spell[i][j]){
	break;
      } 

      cur++;
      if(cur >= s.size()){
	return ii(i,j+1);
      }
    }
  }
  return ii(0,0);
}

void INIT(){
  rep(i,MAX_SP)rep(j,MAX_SP)rep(k,4)next_state[i][j][k] = ii(0,0);

  rep(i,P+1){
    rep(j,spell[i].size()+1){
      rep(dir,4){
	rep(k,j+1){
	  ii value = getValue(spell[i].substr(0,j).substr(k)+dc[dir]);
	  if(value != ii(0,0) && next_state[i][j][dir].second < value.second){
	    next_state[i][j][dir] = value;
	  }
	}
      }
    }
  }
}

inline bool isValid(int x,int y){
  return ( 0 <= x && x < w && 0 <= y && y < h );
}

void compute(){

  priority_queue<Data> Q;
  Q.push(Data(sp,0,ii(0,0)));
  rep(i,h*w)rep(j,MAX_SP)rep(k,MAX_SP)mincost[i][j][k] = IINF;
  mincost[sp][0][0] = 0;

  while(!Q.empty()){

    Data data = Q.top(); Q.pop();
    //cout << "(" << data.cur % w << "," << (int)(data.cur / w) << ") " << data.cost << " info(" << data.info.first << "," << data.info.second << ")" << endl;

    if(data.cur == gp){
      //cout << "path : " << data.debug << endl;
      cout << data.cost << endl;
      return;
    }

    rep(dir,4){

      int nx = data.cur % w + dx[dir];
      int ny = data.cur / w + dy[dir];
      
      if(!isValid(nx,ny))continue;
      
      if(G[ny][nx] == '#')continue;
      
      ii next = next_state[data.info.first][data.info.second][dir];
      
      if(next.first != 0 && spell[next.first].size() == next.second)continue;
      
      if(mincost[nx+ny*w][next.first][next.second] > data.cost + 1){
	mincost[nx+ny*w][next.first][next.second] = data.cost + 1;
	Q.push(Data(nx+ny*w,data.cost+1,next,data.debug+dc[dir]));
      }
      
    }
    
  }
  cout << -1 << endl;
}

bool cmp(const string& a,const string& b){
  return a.size() < b.size();
}

int main(){
  while(cin >> h >> w,h|w){
    rep(i,h){
      rep(j,w){
	cin >> G[i][j];
	if(G[i][j] == 'S'){
	  sp = j + i * w;
	}
	else if(G[i][j] == 'G'){
	  gp = j + i * w;
	}
      }
    }
    cin >> P;
    spell[0].clear();
    vector<string> tmp;
    rep(i,P){
      cin >> spell[i+1];
      tmp.push_back(spell[i+1]);
    }

    sort(tmp.begin(),tmp.end(),cmp);

    bool out[tmp.size()];
    rep(i,tmp.size())out[i] = false;
    rep(i,tmp.size()){
      REP(j,i+1,tmp.size()){
	bool check = true;
	
	if(tmp[j].find(tmp[i]) == string::npos){
	  check = false;
	}

	if(check){
	  out[j] = true;
	}
      }
    }

    vector<string> tmp2;
    rep(i,tmp.size())if(!out[i]){
      tmp2.push_back(tmp[i]);
    }
    P = tmp2.size();
    rep(i,P){
      spell[i+1] = tmp2[i];
    }

    INIT();
    /*
    rep(i,P+1){
      cout << "spell[" << i<< "] =  " << spell[i] << endl;
      rep(j,spell[i].size()+1){
	rep(dir,4){
	  cout << "next_state[" << i << "][" << j << "][" << dir << "] => " << " | " << spell[i].substr(0,j) << " + " << dc[dir]<< " | " <<" next = (" << next_state[i][j][dir].first << "," << next_state[i][j][dir].second << ")" << endl; 
	}
      }
    }
    */

    compute();

  }
  return 0;
}