//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define node pair<int,int>
#define x first
#define y second
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int n,m,l=1,r=1,dis[2002][2002];
char c[2002][2002];
bool fin[100002],vis[2002][2002];
node s,t,q[4000002];
void bfs(){
	q[r]=s;vis[s.x][s.y]=1;dis[s.x][s.y]=0;
	ri i,j;
	while(l<=r){
		i=q[l].x,j=q[l].y;
		l++;
		if(!vis[i+1][j]&&c[i+1][j]!='#'&&i<n){vis[i+1][j]=1;
			q[++r]=make_pair(i+1,j);
			dis[i+1][j]=dis[i][j]+1;
		}if(!vis[i][j+1]&&c[i][j+1]!='#'&&j<m){vis[i][j+1]=1;
			q[++r]=make_pair(i,j+1);
			dis[i][j+1]=dis[i][j]+1;
		}if(!vis[i-1][j]&&c[i-1][j]!='#'&&i>1){vis[i-1][j]=1;
			q[++r]=make_pair(i-1,j);
			dis[i-1][j]=dis[i][j]+1;
		}if(!vis[i][j-1]&&c[i][j-1]!='#'&&j>1){vis[i][j-1]=1;
			q[++r]=make_pair(i,j-1);
			dis[i][j-1]=dis[i][j]+1;
		}if(c[i][j]>='a'&&c[i][j]<='z'){
			if(!fin[c[i][j]]){
				fin[c[i][j]]=1;
				F(ii,1,n)F(jj,1,m){
					if(c[ii][jj]==c[i][j]&&!vis[ii][jj]){
						vis[ii][jj]=1;
			q[++r]=make_pair(ii,jj);
			dis[ii][jj]=dis[i][j]+1;
					}
				}
			}
		}
	}
}
int main(){memset(dis,0x3f,sizeof(dis));
    cin>>n>>m;
    F(i,1,n)scanf("%s",c[i]+1);
    F(i,1,n){
    	F(j,1,m){
    		if(c[i][j]=='S'){
    			s=make_pair(i,j);
			}else if(c[i][j]=='G'){
				t=make_pair(i,j);
			}
		}
	}bfs();
	cout<<(dis[t.x][t.y]<1e9?dis[t.x][t.y]:-1);
    return 0;
}

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

string refridgerator(string s, vector<char> &p){
    for(char &c : s) c = p[c-'0'];
    return s;
}

signed main(){
    string s1,s2,s3; cin >> s1 >> s2 >> s3;
    set<char> cs;
    for(char c : s1) cs.insert(c);
    for(char c : s2) cs.insert(c);
    for(char c : s3) cs.insert(c);
    if(cs.size() > 10){
        cout << "UNSOLVABLE"; return 0;
    }
    vector<char> css(cs.begin(), cs.end());
    for(char &c : s1) c = '0'+(find(css.begin(), css.end(), c) - css.begin());
    for(char &c : s2) c = '0'+(find(css.begin(), css.end(), c) - css.begin());
    for(char &c : s3) c = '0'+(find(css.begin(), css.end(), c) - css.begin());
    vector<char> p(10); for(int i = 0; i < 10; i ++) p[i] = '0'+i;
    do{
        string t1 = refridgerator(s1,p);
        string t2 = refridgerator(s2,p);
        string t3 = refridgerator(s3,p);
        if(t1[0] == '0' or t2[0] == '0' or t3[0] == '0') continue;
        if(stoll(t1) + stoll(t2) == stoll(t3)){
            cout << t1 << "\n" << t2 << "\n" << t3; return 0;
        }
    }while(next_permutation(p.begin(),p.end()));
    cout << "UNSOLVABLE\n";
}