/*
 * author :Sadik Hassan(_sad_)
 *
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include "bits/stdc++.h"
using namespace std;
using   ll =          long long;
using   ld =          long double;
#define nl            "\n"
#define pb            push_back
#define fi            first
#define se            second
#define MP            make_pair
#define PI            (acos(-1.0))
#define rep1(i,n)     for(int i=1;i<=n;i++)
#define rep(i,n)      for(int i=0;i<n;i++)
#define SZ(s)         (int)s.size()
#define all(x)        (x).begin(), (x).end()
#define rall(x)       (x).rbegin(), (x).rend()
#define w(t)          int t;cin>>t;while(t--)
#define _SAD()     	  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0),cout<<fixed<<setprecision(12);

typedef vector<int> vi;
typedef vector<ll> vii;
typedef set<int> si;
typedef set<ll> sii;
/*---------------------------------------------------------------------*/
//freopen("auxiliary.in","r",stdin);
//freopen("auxiliary.out","w",stdout);
const int N=(int)5e5+5;
const ll MOD=(ll)1e9+7;
const ll INF=(ll)1e18+5;
const ll nn=1e5+5;

int main()
{
    _SAD()
    int n;
    cin>>n;
    vii a(n);
    rep(i,n)cin>>a[i];
    rep(i,n)
    {
        int x;cin>>x;
        a[i]*=x;
    }
    ll ans=accumulate(all(a),0);
    if(ans==0)cout<<"Yes"<<nl;
    else cout<<"No"<<nl;
    return 0;
}

#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define cool ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int  long long int
#define pb push_back
#define fe first
#define lb lower_bound 
#define ub upper_bound
#define pii pair<pair<int,int>,pair<int,int> >
#define se second
#define endl "\n"
#define pi pair<int, int>
#define vi vector<int> 
#define vvi vector<vi>
#define bs   binary_search
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i<=b;i++) 
#define all(c) (c).begin(),(c).end()
#define sz(x)       (int)x.size() 
#define PI 3.14159265358979323846
 
const int N =2e2+10;
 
int mod= 1e9+7;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
 
 
 
int po(int a, int b) {
int res = 1;
while (b > 0) {
  if (b & 1)
    res = (res%mod * a%mod) % mod;
  a =(a%mod*a%mod) % mod;
  b >>= 1;
}
 return res;
} 
 

vector<pi> adj[N];
int n,m;

void solve(){
   cin>>n>>m;
   vector<string > s(n);
   vector<vi > cost(n,vi (m,INT_MAX)); 

   rep(i,0,n) cin>>s[i];
   
   int sx,sy,gx,gy;

   rep(i,0,n) {
     rep(j,0,m) {
        char c=s[i][j];
         if(islower(c)) adj[c].pb({i,j});
         else if(s[i][j]=='S') {
            sx=i;
            sy=j;
         }
         else if(s[i][j]=='G') {
             gx=i;
             gy=j;
         }
     }
   }

   cost[sx][sy]=0;
   queue<pi> q;
   q.push({sx,sy});

   while(!q.empty()) {
     pi p=q.front();
     q.pop();
     int x=p.fe,y=p.se;

     int nc=cost[x][y]+1;

     for(int d=0;d<4;d++) {
         int x2=x+dx[d],y2=y+dy[d];
         if(x2<0 || x2>=n || y2<0 || y2>=m || s[x2][y2]=='#') continue;
         if(cost[x2][y2]>nc) {
            cost[x2][y2]=nc; 
            q.push({x2,y2});
          }
        }

         if(islower(s[x][y])) {
            auto& to=adj[s[x][y]];
            for(auto r:to) {
                if(cost[r.fe][r.se]>nc) {
                    cost[r.fe][r.se]=nc;
                    q.push({r.fe,r.se});
                }
            }
            to.clear();
         }  
     
     
   }


   // rep(i,0,n){
   //   rep(j,0,m)
   //      cout<<cost[i][j]<<" ";
   //      cout<<endl;

   // }

     int ans=cost[gx][gy];
     if(ans==INT_MAX) ans=-1;
     
     cout<<ans<<endl;

}

 
int32_t main() {
cool;
int t=1;
 // cin>>t;
 
while(t--)
   solve();
   
   
   return 0;     
}