// ---------- begin Aho-Corasick ----------
const F: usize = 4;

fn convert(s: Vec<char>) -> Vec<usize> {
    let mut a = Vec::with_capacity(s.len());
    for c in s {
        let k = match c {
            'L' => 0,
            'R' => 1,
            'U' => 2,
            'D' => 3,
            _ => unreachable!(),
        };
        a.push(k);
    }
    a
}

struct TrieNode {
    elem: bool,
    fail: usize,
    depth: usize,
    next: [usize; F],
}

impl TrieNode {
    fn new() -> TrieNode {
        TrieNode {
            elem: false,
            fail: 0,
            depth: 0,
            next: [0; F],
        }
    }
}

struct AhoCorasick {
    node: Vec<TrieNode>,
}

#[allow(dead_code)]
impl AhoCorasick {
    fn new() -> Self {
        let root = TrieNode::new();
        AhoCorasick {
            node: vec![root],
        }
    }
    fn insert(&mut self, s: &Vec<usize>) {
        let node = &mut self.node;
        let mut v = 0;
        for &k in s {
            if node[v].next[k] == 0 {
                node[v].next[k] = node.len();
                let mut t = TrieNode::new();
                t.depth = node[v].depth + 1;
                node.push(t);
            }
            v = node[v].next[k];
        }
        node[v].elem = true;
    }
    fn build(&mut self) {
        let mut q = std::collections::VecDeque::new();
        let node = &mut self.node;
        for i in 0..F {
            if node[0].next[i] != 0 {
                q.push_back(node[0].next[i]);
            }
        }
        while let Some(v) = q.pop_front() {
            for i in 0..F {
                let u = node[v].next[i];
                if u == 0 {
                    continue;
                }
                let mut fail = node[v].fail;
                while fail > 0 && node[fail].next[i] == 0 {
                    fail = node[fail].fail;
                }
                let f = node[fail].next[i];
                node[u].fail = f;
                node[u].elem |= node[v].elem || node[f].elem;
                q.push_back(u);
            }
        }
    }
    fn next(&self, v: usize, k: usize) -> usize {
        self.node[v].next[k]
    }
    fn trans(&self, mut v: usize, k: usize) -> usize {
        let node = &self.node;
        while v > 0 && node[v].next[k] == 0 {
            v = node[v].fail;
        }
        node[v].next[k]
    }
    fn elem(&self, k: usize) -> bool {
        self.node[k].elem
    }
    fn fail(&self, k: usize) -> usize {
        self.node[k].fail
    }
    fn depth(&self, k: usize) -> usize {
        self.node[k].depth
    }
    fn get_size(&self) -> usize {
        self.node.len()
    }
}
// ---------- end Aho-Corasick ----------
use std::io::Read;
use std::io::Write;

fn run() {
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    loop {
        let h: usize = it.next().unwrap().parse().unwrap();
        let w: usize = it.next().unwrap().parse().unwrap();
        if h == 0 {
            break;
        }
        let s: Vec<Vec<char>> = (0..h).map(|_| it.next().unwrap().chars().collect()).collect();
        let q: usize = it.next().unwrap().parse().unwrap();
        let mut aho = AhoCorasick::new();
        for _ in 0..q {
            aho.insert(&convert(it.next().unwrap().chars().collect()));
        }
        aho.build();
        let size = aho.get_size();
        let inf = 1_000_000_000;
        let mut dp = vec![vec![vec![inf; size]; w]; h];
        let mut q = std::collections::VecDeque::new();
        for i in 0..h {
            for j in 0..w {
                if s[i][j] == 'S' {
                    q.push_back((i, j, 0));
                    dp[i][j][0] = 0;
                }
            }
        }
        while let Some((r, c, v)) = q.pop_front() {
            for (k, &(x, y)) in [(r, c - 1), (r, c + 1), (r - 1, c), (r + 1, c)].iter().enumerate() {
                let u = aho.trans(v, k);
                if !(x < h && y < w && s[x][y] != '#' && !aho.elem(u)) {
                    continue;
                }
                if dp[x][y][u] > dp[r][c][v] + 1 {
                    dp[x][y][u] = dp[r][c][v] + 1;
                    q.push_back((x, y, u));
                }
            }
        }
        let mut ans = inf;
        for i in 0..h {
            for j in 0..w {
                if s[i][j] == 'G' {
                    ans = *dp[i][j].iter().min().unwrap();
                }
            }
        }
        if ans < inf {
            writeln!(out, "{}", ans).ok();
        } else {
            writeln!(out, "-1").ok();
        }
    }
}

fn main() {
    run();
}



Measured memory_bytes: 5064