#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010

/* Aho Corasick */

int ACNodeId = 0;

struct ACNode{
  int val, id;
  ACNode *next[26], *failure;
  
  ACNode():val(0) {
    memset(next,0,sizeof(next));
    id = ACNodeId++;
  }
  
  void insert(char *s){
    if(!*s){ val++; return; }
    int al = *s-'A';
    if(next[al]==NULL) next[al] = new ACNode;
    next[al]->insert(s+1);
  }

  ACNode *nextNode(char c){
    int al = c - 'A';
    if (next[al]) return next[al];
    return failure == this ? this : failure->nextNode(c);
  }
};

struct AhoCorasick{
  ACNode *node;

  AhoCorasick(){node = new ACNode;}
  
  void insert(char *s) {
    node->insert(s);
  }
  
  void build() {
    queue<ACNode*> que;
    que.push(node);
    node->failure = node;
    
    while(que.size()){
      ACNode *p = que.front();
      que.pop();
      
      for(int i=0;i<26;i++){
        if(p->next[i]){
          ACNode *failure = p->failure;
          while(!failure->next[i] && failure != node){
            failure = failure->failure;
          }
          if (failure->next[i] && failure != p){
            p->next[i]->failure = failure->next[i];
            p->next[i]->val += failure->next[i]->val;
          }else{
            p->next[i]->failure = node;
          }
          que.push(p->next[i]);
        }
      }
    }
  }
};

int dx[8] = {0,0,1,-1,1,1,-1,-1};
int dy[8] = {1,-1,0,0,1,-1,1,-1};
char ds[] = "DURL";

bool solve(){
  int h, w;
  char s[51][51];
  int sx, sy, gx, gy;
  
  scanf("%d%d", &h, &w);

  if(h == 0) return false;
  
  for(int i=0;i<h;i++){
    scanf("%s", s[i]);
    for(int j=0;j<w;j++){
      if(s[i][j] == 'S'){
        sx = j; sy = i;
      }
      if(s[i][j] == 'G'){
        gx = j; gy = i;
      }
    }
  }

  int q;
  AhoCorasick ac;
  scanf("%d", &q);

  for(int i=0;i<q;i++){
    char pattern[11];
    scanf("%s", pattern);
    ac.insert(pattern);
  }

  ac.build();

  bool visited[51][51][200] = {};
  queue<pair<pair<int,int>,ACNode*> > que;
  que.push({{sy, sx}, ac.node});
  
  for(int i=0;que.size();i++){
    queue<pair<pair<int,int>,ACNode*> > que2;

    while(que.size()){
      auto p = que.front(); que.pop();

      int y = p.first.first;
      int x = p.first.second;
      ACNode* cur = p.second;

      if(cur->val) continue;
      if(y < 0 || h <= y || x < 0 || w <= x) continue;
      if(s[y][x] == '#') continue;
      if(visited[y][x][cur->id]) continue;
      visited[y][x][cur->id] = true;

      if(gy == y && gx == x){
        printf("%d\n", i);
        return true;
      }
      
      for(int j=0;j<4;j++){
        auto next = cur->nextNode(ds[j]);
        que2.push({{dy[j] + y, dx[j] + x}, next});
      }
    }
    
    que = que2;
  }
  
  puts("-1");
  return true;
}

int main(){
  while(solve());
}



Measured memory_bytes: 4316