Link to this code: https://cses.fi/paste/848b20585920bd83cdaf35/
#include <bits/stdc++.h>
using namespace std;
#define int long long
string ans;
bool dfs(int i,int j , vector<vector<char>>&b){
    b[i][j]='#';
    int delr[] = {0,-1,0,1};
    int delc[] = {-1,0,1,0};
    char path[]= {'L','U','R','D'};
    int n =b.size();
    int m = b[0].size();

    for(int k=0;k<4;k++){
        int nr = i+delr[k];
        int nc = j+ delc[k];
        if(nr>=0 && nc>=0 && nr<n && nc<m){
            if(b[nr][nc]=='.' && dfs(nr,nc,b)==true) {
                ans+=path[k];
                return true;
            }
            else if(b[nr][nc]=='B'){
                b[nr][nc]='#';
                ans+=path[k];
                return true;
            }
        }
    }
    return false;
}
void solve()
{
    int n,m;
    cin >> n>>m;
    int x,y;
    vector<vector<char>> b(n);
    for(int i =0;i<n;i++){
        for(int j =0;j<m;j++){
            char c;
            cin>>c;
            if(c=='A'){
                x=i;y=j;
            }
            b[i].push_back(c);         
        }
    }
    ans = "";
    if(dfs(x,y,b)){
        cout<<"YES\n";
        cout<<ans.size()<<"\n";
        reverse(ans.begin(),ans.end());
        cout<<ans<<"\n";
    }else cout<<"NO\n";
}
int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

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