#include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <deque>
    #include <list>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <utility>
    #include <algorithm>
    #include <map>
    #include <set>
    #include <complex>
    #include <cmath>
    #include <limits>
    #include <climits>
    #include <ctime>
    #include <cassert>
    using namespace std;
     
    #define rep(i,a,n) for(int i=a; i<n; i++)
    #define repr(i,a,n) for(int i=a; i>=n; i--)
    #define pb(a) push_back(a)
    #define fr first
    #define sc second
    #define INF 999999999
     
    #define X real()
    #define Y imag()
    #define EPS (1e-10)
    #define EQ(a,b) (abs((a) - (b)) < EPS)
    #define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
    #define LE(n, m) ((n) < (m) + EPS)
    #define LEQ(n, m) ((n) <= (m) + EPS)
    #define GE(n, m) ((n) + EPS > (m))
    #define GEQ(n, m) ((n) + EPS >= (m))
     
    typedef vector<int> VI;
    typedef vector<VI> MAT;
    typedef pair<int, int> pii;
    typedef long long int ll;
     
    typedef complex<double> P;
    typedef pair<P, P> L;
    typedef pair<P, double> C;
     
    int dy[]={0, 0, 1, -1};
    int dx[]={1, -1, 0, 0};
    int const MOD = 1000000007;
     
    namespace std {
        bool operator<(const P& a, const P& b) {
            return a.X != b.X ? a.X < b.X : a.Y < b.Y;
        }
    }
     
    char board[510][510];
    bool isthrough[510][510];
    int dist[510][510];
     
    int main() {
        int H, W; cin >> H >> W;
        memset(isthrough, false, sizeof(isthrough));
        rep(i,0,H) rep(j,0,W) dist[i][j] = -1;
        pii escape;
        rep(i,0,H) rep(j,0,W) {
            cin >> board[i][j];
            if(board[i][j] == '%') {
                escape.first = i;
                escape.second = j;
                dist[i][j] = 0;
                isthrough[i][j] = true;
            }
        }
     
        int soldier = INF;
        int princess;
        queue<pii> q;
        q.push(escape);
        while(!q.empty()) {
            pii t = q.front(); q.pop();
            int x = t.fr, y = t.sc;
            int prev = dist[x][y];
            if(board[x][y] == '$') soldier = min(soldier, dist[x][y]);
            if(board[x][y] == '@') princess = dist[x][y];
            if(x != 0 && board[x-1][y] != '#' && !isthrough[x-1][y]) {
                isthrough[x-1][y] = true;
                dist[x-1][y] = prev + 1;
                q.push(pii(x-1, y));
            }
            if(x != H-1 && board[x+1][y] != '#' && !isthrough[x+1][y]) {
                isthrough[x+1][y] = true;
                dist[x+1][y] = prev + 1;
                q.push(pii(x+1, y));
            }
            if(y != 0 && board[x][y-1] != '#' && !isthrough[x][y-1]) {
                isthrough[x][y-1] = true;
                dist[x][y-1] = prev + 1;
                q.push(pii(x, y-1));
            }
            if(y != W-1 && board[x][y+1] != '#' && !isthrough[x][y+1]) {
                isthrough[x][y+1] = true;
                dist[x][y+1] = prev + 1;
                q.push(pii(x, y+1));
            }
        }
     
        if(princess < soldier) cout << "Yes" << endl;
        else cout << "No" << endl;
        return 0;
    }

Measured memory_bytes: 3896