#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()

#define fp(a,i,c)   for ( int (a)=(i); (a)<(c); (a)++)
#define fm(a,b,c)  for (  int (a)=(b); (a)>=(c); (a)--)
char a[9999][9999];
vector <pair<int, int>> v[300];
int dis[9999][9999];
int c[999];
void S()
{
	int n, m;
	cin >> n >> m;
	fp(i,0,n+2)
	fp(j,0,m+2)
		a[i][j] = '#', dis[i][j] = -1;
	int s1, c1, s2, c2;
	fp(i,1,n+1)
	{
		fp(j,1,m+1)
		{
			cin >> a[i][j];
			if(a[i][j] == 'S')
				s1 = i, c1 = j;
			if(a[i][j] == 'G')
				s2 = i, c2 = j;
			if(a[i][j] >= 'a' && a[i][j] <= 'z')
				v[a[i][j]].push_back({i,j});
		}
	}
	queue<pair<int, int>> q;
	q.push({s1,c1});
	dis[s1][c1] = 0;
	while(q.size())
	{
		int x = q.front().first, y = q.front().second;
// 		cout << x << " " << y << " " << a[x][y] << "\n";
		q.pop();
		if(a[x][y] >= 'a' && a[x][y] <= 'z')
		{
			c[a[x][y]] ++;
			if(c[a[x][y]] == 1)
			{
				for(auto it : v[a[x][y]])
				{
					if(dis[it.first][it.second] == -1)
						dis[it.first][it.second] = dis[x][y]+1, q.push({it.first, it.second});
				}
			}
		}
			for(int i = -1; i <= 1; i ++)
			for(int j = -1; j <= 1; j ++)
			{
				if(abs(i-j) == 1)
				{
					if(a[x+i][y+j] != '#' && dis[x+i][y+j] == -1)
					{
						q.push({x+i, y+j});
						dis[x+i][y+j] = dis[x][y]+1;
					}
				}
			}
	}
	int ans = dis[s2][c2];
	cout << ans;
}
int main()
{
    IOS;
    S();
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int read()
{
	char c;
	int w=1;
	while((c=getchar())>'9'||c<'0')if(c=='-')w=-1;
	int ans=c-'0';
	while((c=getchar())>='0'&&c<='9')ans=(ans<<1)+(ans<<3)+c-'0';
	return ans*w;
}
int k;
string a,b;
int t[10];
int tot1[10];
int tot2[10];
double check(int id)
{
	if(t[id]<=0)return 0;
	int s=0;
	for(int i=1;i<=9;i++)s+=t[i];
	return 1.0*t[id]/s;
}
int pows[10];
bool js()
{
	int ans1=0;
	for(int i=1;i<=9;i++)ans1+=i*pows[tot1[i]];
	int ans2=0;
	for(int i=1;i<=9;i++)ans2+=i*pows[tot2[i]];
//	cout<<ans1<<" "<<ans2<<endl;
	return ans1>ans2;
}
int main(){
	pows[0]=1;
	for(int i=1;i<=6;i++)pows[i]=pows[i-1]*10;
	k=read();
	for(int i=1;i<=9;i++)t[i]=k;
	cin>>a>>b;
//	cout<<a.size()<<endl;
	for(int i=0;i<a.size()-1;i++)t[a[i]-'0']--,tot1[a[i]-'0']++;
//	cout<<"qweqweqwe"<<endl;
	for(int i=0;i<b.size()-1;i++)t[b[i]-'0']--,tot2[b[i]-'0']++;
	double ans=0;
//	cout<<js()<<endl;
//	return 0;
	for(int i=1;i<=9;i++)
	{
		if(!t[i])continue;
		double now=check(i);
		t[i]--;
		tot1[i]++;
		for(int j=1;j<=9;j++)
		{
			if(!t[j])continue;
			tot2[j]++;
			if(js())
			{
				ans+=now*check(j);
//		cout<<i<<" "<<j<<" "<<now*check(j)<<endl;
			}
			tot2[j]--;
		}
		tot1[i]--;
		t[i]++;
	}
	cout<<fixed<<setprecision(8)<<ans<<'\n';
	return 0;
}