#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define all(v) v.begin(),v.end()
#pragma gcc optimize("O3")
#pragma gcc optimize("Ofast") 
#pragma gcc optimize("unroll-loops")
using namespace std;
const int INF = 1e9;
const int TMX = 1 << 18;
const long long llINF = 1e16;
const long long mod = 1e9+7;
const long long hashmod = 100003;
const int MAXN = 100000;
const int MAXM = 1000000;
typedef long long ll;
typedef long double ld;
typedef pair <int,int> pi;
typedef pair <ll,ll> pl;
typedef vector <int> vec;
typedef vector <pi> vecpi;
typedef long long ll;
int n,m,a[105],c[105];
int arrow[10005],ch[10005];
pi edge[10005];
vecpi v[105];

void dfs(int x) {
	c[x] = 1;
	for(pi i : v[x]) {
		if(!ch[abs(i.y)]) {
			if(i.y > 0) arrow[i.y] = 1;
			else arrow[-i.y] = 0;
			ch[abs(i.y)] = 1;
		}
		if(!c[i.x]) dfs(i.x);
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= m;i++) {
		int x,y; cin >> x >> y;
		edge[i] = {x,y};
	}
	for(int i = 1;i <= n;i++) cin >> a[i];
	for(int i = 1;i <= m;i++) {
		int x = edge[i].x, y = edge[i].y;
		if(a[x] < a[y]) arrow[i] = 0;
		else if(a[x] > a[y]) arrow[i] = 1;
		else v[x].pb({y,i}), v[y].pb({x,-i});
	}
	for(int i = 1;i <= n;i++) {
		if(!c[i]) dfs(i);
	}
	for(int i = 1;i <= m;i++) {
		if(arrow[i]) cout << "->\n";
		else cout << "<-\n";
	}
}
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(),(x).end()
#define ll long long
const long long MOD = 998244353;
const int N = 2020;
int d[N][N];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
vector<pair<int, int> > g[N];
char a[N][N];
int n, m;
void BFS01(int sx, int sy)
{
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++)d[i][j] = 1e9;
	}
	d[sx][sy] = 0;
	deque<pair<int, int> > q;
	q.emplace_back(sx, sy);
	while (!q.empty()) {
		int vx, vy;
		tie(vx, vy) = q.front();
		q.pop_front();
		int c = a[vx][vy];
		if (c >= 'a' && c <= 'z') {
			for (auto el:g[c - 'a']) {
				int x = el.first;
				int y = el.second;
				if (d[x][y] > d[vx][vy] + 1) {
					d[x][y] = d[vx][vy] + 1;
					q.push_back({x, y});
				}
			}
			g[c - 'a'].resize(0);
		}
		for (int i = 0; i < 4; i++) {
			int x = vx + dx[i];
			int y = vy + dy[i];
			if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#' && d[x][y] > d[vx][vy] + 1) {
				d[x][y] = d[vx][vy] + 1;
				q.push_back({x, y});
			}
		}
	}
}
void solve()
{
	cin >> n >> m;
	int sx, sy;
	int tx, ty;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
			if (a[i][j] == 'G') sx = i, sy = j;
			if (a[i][j] == 'S') tx = i, ty = j;
			char c = a[i][j];
			if (c >= 'a' && c <= 'z') g[c - 'a'].emplace_back(i, j);
		}
	}
	BFS01(sx, sy);
	if (d[tx][ty] == 1e9) cout << -1 << endl;
	else cout << d[tx][ty] << endl;
}
int main()
{
#ifdef QWERTY
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	srand(time(NULL));
	int TC = 1;
//	cin >> TC;
	while (TC--) {
//		cout << TC << endl;
		solve();
	}
	return 0;
}