#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for(int i = 0; i < n; ++i)
#define GETVEC(v) for(auto& m : v) cin >> m;
using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main()
{
	int H, W; cin >> H >> W;
	vector<char> as(H * W);
	multimap<char, int> ch2PosMap;
	int S, G;
	REP(i, H)REP(j, W)
	{
		char ch; cin >> ch; as[i * W + j] = ch;
		ch2PosMap.insert({ ch, i * W + j });
		if (ch == 'S') S = i * W + j;
		if (ch == 'G') G = i * W + j;
	}
	const int INF = 1e9;
	vector<int> dists(H * W, INF);
	dists[S] = 0;
	deque<int> Q{ S };
	vector<bool> used(H * W, false);
	while (!Q.empty())
	{
		int t = Q.front(); Q.pop_front();
		vector<int> ns;
		if ('a' <= as[t] && as[t] <= 'z' && used[t] == false)
		{
			used[t] = true;
			auto range = ch2PosMap.equal_range(as[t]);
			for (auto itr = range.first; itr != range.second; ++itr)
			{
				int pos = itr->second;
				if (pos != t && used[pos] == false)
				{
					ns.push_back(pos);
					used[pos] = true;
				}
			}
		}
		if (t - W > 0 && as[t - W] != '#') ns.push_back(t - W);
		if (t + W < H * W && as[t + W] != '#') ns.push_back(t + W);
		if (t % W > 0 && as[t - 1] != '#') ns.push_back(t - 1);
		if (t % W != W - 1 && as[t + 1] != '#') ns.push_back(t + 1);
		for (const int n : ns)
		{
			if (dists[n] > dists[t] + 1)
			{
				dists[n] = dists[t] + 1;
				Q.push_back(n);
			}
		}
	}
	cout << (dists[G] != INF ? dists[G] : -1) << endl;
}

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 5;

int N, M;
int a[MAXN][2];
vector <pair <int, int>> adj[MAXN];
int bio[MAXN], ans[MAXN];
int c[MAXN];

void load() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++)
    for (int j = 0; j < 2; j++)
      scanf("%d", a[i] + j);
  for (int i = 1; i <= N; i++)
    scanf("%d", c + i);
}

void dfs(int x, int p) {
  bio[x] = 1;
  for (auto it : adj[x]) {
    int nxt = a[it.first][it.second];
    if (nxt == p || bio[nxt] == 2)
      continue;
    ans[it.first] = it.second;
    if (!bio[nxt])
      dfs(nxt, x);
  }
  bio[x] = 2;
}

void solve() {
  for (int i = 0; i < M; i++)
    if (c[a[i][0]] != c[a[i][1]])
      ans[i] = c[a[i][0]] > c[a[i][1]];
    else
      for (int j = 0; j < 2; j++)
        adj[a[i][j]].push_back({i, j ^ 1});
  for (int i = 1; i <= N; i++)
    if (!bio[i])
      dfs(i, 0);
  for (int i = 0; i < M; i++)
    printf("%s\n", ans[i] ? "->" : "<-");
}

int main() {
  load();
  solve();
  return 0;
}