#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define int long long int
int n, m;
vector<pair<int,int>> monster;
vector<vector<int>> g;
map<pair<int,int>, pair<int,int>> par_mp;
pair<int,int> si, se;
vector<pair<int,int>> moves = {{-1,0},{1,0},{0,1},{0,-1}};
bool isValid(int x, int y, int timer)
{
if(x<0 or y<0 or x>=n or y>=m)
{
return false;
}
if(g[x][y] <= timer)
{
return false;
}
return true;
}
bool isExcape(int x, int y, int timer)
{
if(!isValid(x,y,timer)) return false;
if(x == 0 or y == 0 or
x == n-1 or y == m-1) return true;
return false;
}
bool bfs_escape()
{
queue<pair<pair<int,int>,int>> q;
q.push(make_pair(si,0));
par_mp[si] = {-1,-1};
while(!q.empty())
{
int cx = q.front().first.first;
int cy = q.front().first.second;
int timer = q.front().second;
timer++; q.pop();
for(auto mv: moves)
{
int tx = cx+mv.first;
int ty = cy+mv.second;
if(isExcape(tx,ty,timer))
{
par_mp[{tx,ty}] = {cx,cy};
se = {tx,ty}; return true;
}
if(isValid(tx,ty,timer))
{
par_mp[{tx,ty}] = {cx,cy};
g[tx][ty] = timer;
q.push({{tx,ty},timer});
}
}
}
return false;
}
void preprocess_lava_flow()
{
queue<pair<pair<int,int>,int>> q;
for(auto m: monster)
{
q.push(make_pair(m,0));
}
while(!q.empty())
{
int cx = q.front().first.first;
int cy = q.front().first.second;
int timer = q.front().second;
timer++; q.pop();
for(auto mv: moves)
{
int tx = cx+mv.first;
int ty = cy+mv.second;
if(isValid(tx,ty,timer))
{
g[tx][ty] = timer;
q.push({{tx,ty},timer});
}
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
g.resize(n);
for(int i = 0; i < n; ++i)
{
g[i].resize(m);
}
for(int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
g[i][j] = INT_MAX;
}
}
for(int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
char c; cin >> c;
if(c == '#')
{
g[i][j] = 0;
}
else if(c == 'M')
{
g[i][j] = 0;
monster.push_back({i,j});
}
else if(c == 'A')
{
g[i][j] = 0;
si = {i,j};
}
else
{
g[i][j] = INT_MAX;
}
}
}
if(si.first == 0 or si.second == 0 or si.first == n-1 or si.second == m-1)
{
cout << "YES" << endl;
cout << 0;
return 0;
}
preprocess_lava_flow();
if(!bfs_escape())
{
cout << "NO";
return 0;
}
cout << "YES" << endl;
pair<int,int> tmp = se;
pair<int,int> tmp1 = par_mp[se];
pair<int,int> ed = {-1,-1};
vector<char> ans;
while(tmp1 != ed)
{
if((tmp.second - tmp1.second) == 1 and (tmp.first - tmp1.first) == 0)
{
ans.push_back('R');
}
if((tmp.second - tmp1.second) == -1 and (tmp.first - tmp1.first) == 0)
{
ans.push_back('L');
}
if((tmp.second - tmp1.second) == 0 and (tmp.first - tmp1.first) == 1)
{
ans.push_back('D');
}
if((tmp.second - tmp1.second) == 0 and (tmp.first - tmp1.first) == -1)
{
ans.push_back('U');
}
tmp = par_mp[tmp];
tmp1 = par_mp[tmp];
}
reverse(ans.begin(), ans.end());
cout << ans.size() << endl;
for(auto c: ans)
{
cout << c;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZW5kbCAiXG4iCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgaW50IGxvbmcgbG9uZyBpbnQKIAppbnQgbiwgbTsKdmVjdG9yPHBhaXI8aW50LGludD4+IG1vbnN0ZXI7CnZlY3Rvcjx2ZWN0b3I8aW50Pj4gZzsKbWFwPHBhaXI8aW50LGludD4sIHBhaXI8aW50LGludD4+IHBhcl9tcDsKcGFpcjxpbnQsaW50PiBzaSwgc2U7IAp2ZWN0b3I8cGFpcjxpbnQsaW50Pj4gbW92ZXMgPSB7ey0xLDB9LHsxLDB9LHswLDF9LHswLC0xfX07IAoKYm9vbCBpc1ZhbGlkKGludCB4LCBpbnQgeSwgaW50IHRpbWVyKQp7CiAgaWYoeDwwIG9yIHk8MCBvciB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SA9IHRpbWVyOwogICAgICAgIHEucHVzaCh7e3R4LHR5fSx0aW1lcn0pOwogICAgICB9CiAgICB9CiAgfQogIHJldHVybiBmYWxzZTsKfQoKdm9pZCBwcmVwcm9jZXNzX2xhdmFfZmxvdygpCnsKICBxdWV1ZTxwYWlyPHBhaXI8aW50LGludD4saW50Pj4gcTsKICBmb3IoYXV0byBtOiBtb25zdGVyKQogIHsKICAgIHEucHVzaChtYWtlX3BhaXIobSwwKSk7CiAgfQogIHdoaWxlKCFxLmVtcHR5KCkpCiAgewogICAgaW50IGN4ID0gcS5mcm9udCgpLmZpcnN0LmZpcnN0OwogICAgaW50IGN5ID0gcS5mcm9udCgpLmZpcnN0LnNlY29uZDsKICAgIGludCB0aW1lciA9IHEuZnJvbnQoKS5zZWNvbmQ7IAogICAgdGltZXIrKzsgcS5wb3AoKTsKCiAgICBmb3IoYXV0byBtdjogbW92ZXMpCiAgICB7CiAgICAgIGludCB0eCA9IGN4K212LmZpcnN0OwogICAgICBpbnQgdHkgPSBjeSttdi5zZWNvbmQ7CiAgICAgIGlmKGlzVmFsaWQodHgsdHksdGltZXIpKQogICAgICB7CiAgICAgICAgZ1t0eF1bdHldID0gdGltZXI7CiAgICAgICAgcS5wdXNoKHt7dHgsdHl9LHRpbWVyfSk7CiAgICAgIH0KICAgIH0KICB9CgogIAogIAp9CgppbnQzMl90IG1haW4oKQp7CiAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgY2luLnRpZShOVUxMKTsKICBjaW4gPj4gbiA+PiBtOwogIGcucmVzaXplKG4pOwogIGZvcihpbnQgaSA9IDA7IGkgPCBuOyArK2kpCiAgewogICAgZ1tpXS5yZXNpemUobSk7CiAgfQogCiAgZm9yKGludCBpID0gMDsgaSA8IG47ICsraSkKICB7CiAgICBmb3IgKGludCBqID0gMDsgaiA8IG07ICsraikKICAgIHsKICAgICAgZ1tpXVtqXSA9IElOVF9NQVg7CiAgICB9CiAgfSAgCiAKICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgKytpKQogIHsKICAgIGZvciAoaW50IGogPSAwOyBqIDwgbTsgKytqKQogICAgewogICAgICBjaGFyIGM7IGNpbiA+PiBjOwogICAgICBpZihjID09ICcjJykKICAgICAgewogICAgICAgIGdbaV1bal0gPSAwOwogICAgICB9CiAgICAgIGVsc2UgaWYoYyA9PSAnTScpCiAgICAgIHsKICAgICAgICBnW2ldW2pdID0gMDsKICAgICAgICAKICAgICAgICAgIG1vbnN0ZXIucHVzaF9iYWNrKHtpLGp9KTsKICAgICAgfQogICAgICBlbHNlIGlmKGMgPT0gJ0EnKQogICAgICB7CiAgICAgICAgZ1tpXVtqXSA9IDA7CiAgICAgICAgc2kgPSB7aSxqfTsKICAgICAgfQogICAgICBlbHNlCiAgICAgIHsKICAgICAgICBnW2ldW2pdID0gSU5UX01BWDsKICAgICAgfQogICAgfQogIH0KICBpZihzaS5maXJzdCA9PSAwIG9yIHNpLnNlY29uZCA9PSAwIG9yIHNpLmZpcnN0ID09IG4tMSBvciBzaS5zZWNvbmQgPT0gbS0xKSAKICB7CiAgICBjb3V0IDw8ICJZRVMiIDw8IGVuZGw7CiAgICBjb3V0IDw8IDA7CiAgICByZXR1cm4gMDsKICB9CiAgcHJlcHJvY2Vzc19sYXZhX2Zsb3coKTsKICAKICBpZighYmZzX2VzY2FwZSgpKQogIHsKICAgIGNvdXQgPDwgIk5PIjsKICAgIHJldHVybiAwOwogIH0KICBjb3V0IDw8ICJZRVMiIDw8IGVuZGw7CiAgcGFpcjxpbnQsaW50PiB0bXAgPSBzZTsKICBwYWlyPGludCxpbnQ+IHRtcDEgPSBwYXJfbXBbc2VdOwogIHBhaXI8aW50LGludD4gZWQgPSB7LTEsLTF9OyAKICB2ZWN0b3I8Y2hhcj4gYW5zOwogIHdoaWxlKHRtcDEgIT0gZWQpCiAgewogICAgCiAgICBpZigodG1wLnNlY29uZCAtIHRtcDEuc2Vjb25kKSA9PSAxIGFuZCAodG1wLmZpcn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