Link to this code: `https://cses.fi/paste/6a3d2f3546a5a059225c7d/`
```// AUTHOR vijay
#define ll long long
#define dd long double
#define pb push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define fo(i ,a, n) for(ll i =a ; i < n ; i++)
//#define all(x) x.begin(), x.end()
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)//traverse through a dataset
//#define sortx(x) sort(all(x))
#define maxpq priority_queue<pair<ll,ll>>
#define minpq priority_queue<ll,vector<ll>,greater<ll>>
#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<utility>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<bitset>
ll fact[200001] ;
ll inf=1e18;
ll z=1e9+7;
ll gdp(ll a , ll b){return (a - (a%b)) ;}
ll ld(ll a , ll b){if(a < 0) return -1*gdp(abs(a) , b) ; if(a%b == 0) return a ; return (a + (b - a%b)) ;} // least number >=a divisible by b
ll gd(ll a , ll b){if(a < 0) return(-1 * ld(abs(a) , b)) ;    return (a - (a%b)) ;} // greatest number <= a divisible by b
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
ll e_gcd(ll a , ll b , ll &x , ll &y){ if(b > a) return e_gcd(b , a , y , x) ; if(b == 0){x = 1 ; y = 0 ; return a ;}
ll x1 , y1 ; e_gcd(b , a%b , x1 , y1) ; x = y1 ; y = (x1 - ((a/b) * y1)) ; return e_gcd(b , a%b , x1 , y1) ;}
ll power(ll a ,ll b , ll p){if(b == 0) return 1 ; ll c = power(a , b/2 , p) ; if(b%2 == 0) return ((c*c)%p) ; else return ((((c*c)%p)*a)%p) ;}
ll inverse(ll a ,ll n){return power(a , n-2 , n) ;}
ll max(ll a , ll b){if(a > b) return a ; return b ;}
ll min(ll a , ll b){if(a < b) return a ; return b ;}
ll left(ll i){return ((2*i)+1) ;}
ll right(ll i){return ((2*i) + 2) ;}
ll ncr(ll n , ll r){if(n < r|| (n < 0) || (r < 0)) return 0 ; return ((((fact[n] * inverse(fact[r] , z)) % z) * inverse(fact[n-r] , z)) % z);}
void swap(char&a , char&b){char c = a ; a = b ; b = c ; return ;}
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define ub(v,x) upper_bound(v.begin(),v.end(),x);
#define lb(v,x) lower_bound(v.begin(),v.end(),x);
dd pi = acos(-1) ;
ll pow(ll a ,ll b ){if(b == 0) return 1 ; ll c = pow(a , b/2) ; if(b%2 == 0) return (c*c) ; else return ((c*c)*a);}

using namespace std ;

struct mytype{
bool a;
ll b;
};
const int N=1e6+7;

vector<ll>dis(N,1e10);
vector<ll>dis2(N,1e10);

vector<bool>vis(N,false);
char grid[1002][1002];

ll n,m;

bool isvalid(ll x,ll y){

if(x>=0 and x<n and y>=0 and y<m)
return true;
else
return false;
}
vector<ll>p(N,-1);
ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};

for(ll i=0;i<4;i++){
ll r=dx[i]+x;
ll c=dy[i]+y;
//cout<<'F'<<endl;
if(isvalid(r,c) and grid[r][c]!='#'){
//cout<<"There is and egde from"<<x<<" "<<y<<" "<<"to"<<" "<<r<<" "<<c<<endl;
}
}
}

void bfs(vector<ll>&s){
queue<ll>Q;
for(auto x:s){
Q.push(x);
dis[x]=0;
vis[x]=1;
}

while(!Q.empty()){
ll u=Q.front();
Q.pop();

if(vis[x])continue;
dis[x]=1+dis[u];
vis[x]=1;
Q.push(x);
}
}

}

void bfs2(ll s){
queue<ll>Q;
dis2[s]=0;
Q.push(s);
vis[s]=1;

while(!Q.empty()){
ll u=Q.front();
Q.pop();

if(vis[x])continue;
dis2[x]=1+dis2[u];
p[x]=u;
vis[x]=1;
Q.push(x);
}
}
}

void solve(){
//ll n,m;
cin>>n>>m;

fo(i,0,n){
string s;
cin>>s;
fo(j,0,m)grid[i][j]=s[j];
}

vector<ll>monstors;
ll start_id;
fo(i,0,n){
fo(j,0,m){
if(grid[i][j]=='#')continue;
if(grid[i][j]=='M')monstors.pb(i*m+j);
if(grid[i][j]=='A')start_id=i*m+j;
}

}

bfs(monstors);
vis.assign(N,false);
bfs2(start_id);
vector<ll>winning_corner;
for(ll i=0;i<n;i++){
for(ll j=0;j<m;j++){
if(i==0||i==n-1||j==0||j==m-1){
if(dis2[i*m+j]<dis[i*m+j])winning_corner.pb(i*m+j);
// cout<<dis[i*m+j]<<" "<<dis2[i*m+j]<<" "<<i<<" "<<j<<endl;
}
//cout<<dis[i*m+j]<<" ";
}
//cout<<endl;

}

// path restoration part
if(winning_corner.size()==0){
cout<<"NO\n";
return ;
}
ll node=winning_corner[0];
string ans;
//cout<<start_id<<endl;
//cout<<node<<endl;

while(node!=start_id){
ll i,j,ii,jj;
i=node/m;
j=node%m;
ii=p[node]/m;
jj=p[node]%m;
//cout<<node<<endl;
if(jj==j and ii+1==i)ans+='D';
else if(jj==j and i+1==ii)ans+='U';
else if(ii==i and jj+1==j)ans+='R';
else if(ii==i and j+1==jj)ans+='L';
node=p[node];

}
cout<<"YES\n";
reverse(ans.begin(),ans.end());
cout<<ans.size()<<endl;
cout<<ans<<endl;

}
int main(){

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
// ios_base::sync_with_stdio(false);
//  cin.tie(NULL);

ll t;
//  cin>>t;
t=1;

while(t--){
solve();
}
}```