#include <iostream>
#include <vector>
#include <mdspan>
#include <limits>
#include <queue>
#include <set>
#include <utility>
struct Point {
int cost = std::numeric_limits<int>::max();
bool walkable = false;
char parent = '#';
};
int main()
{
int height, width;
std::cin >> height >> width;
std::vector<Point> points(height * width);
std::mdspan grid(points.data(),width,height);
int startx, starty, endx, endy;
std::set<std::pair<int,int>> visited;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char c;
std::cin >> c;
if (c == '#')
continue;
auto& point = grid[x,y];
point.walkable = true;
if (c == 'A')
{
startx = x;
starty = y;
point.cost = 0;
} else if (c == 'B')
{
endx = x;
endy = y;
}
}
}
// violates invariants but it should be fine
const auto cmp = [&](const std::pair<int,int>& a, const std::pair<int,int>& b)
{
return grid[a.first,a.second].cost > grid[b.first,b.second].cost;
};
std::priority_queue<std::pair<int,int>, std::vector<std::pair<int,int>>, decltype(cmp)> minheap(cmp);
minheap.push({startx,starty});
bool done = false;
const auto update = [&](int x, int y, int new_cost, char parent_dir)
{
if (!(0 <= x && x < width && 0 <= y && y < height))
return;
auto& point = grid[x,y];
if (point.walkable && new_cost < point.cost)
{
point.cost = new_cost;
point.parent = parent_dir;
minheap.push({x,y});
}
};
bool found_path = false;
while (!minheap.empty())
{
auto [parentx, parenty] = minheap.top();
minheap.pop();
if (parentx == endx && parenty == endy)
{
found_path = true;
break;
}
int new_cost = grid[parentx,parenty].cost + 1;
update(parentx+1,parenty,new_cost,'R');
update(parentx-1,parenty,new_cost,'L');
update(parentx,parenty+1,new_cost,'D');
update(parentx,parenty-1,new_cost,'U');
}
if (!found_path)
{
std::cout << "NO";
return 0;
}
std::vector<char> path;
int currentx = endx;
int currenty = endy;
while (true)
{
auto& point = grid[currentx, currenty];
if (point.parent == '#' || (currentx == startx && currenty == starty))
break;
switch (point.parent)
{
case 'L':
currentx++;
break;
case 'R':
currentx--;
break;
case 'D':
currenty--;
break;
case 'U':
currenty++;
break;
}
path.push_back(point.parent);
}
std::cout << "YES\n" << path.size() << std::endl;
for (auto it = path.rbegin(); it != path.rend(); it++)
{
std::cout << *it;
}
}