#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
#define WT int T=read();while(T--) 
#define NO puts("NO");
#define YES puts("YES");
using namespace std;
 
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}
 
const int Mod=998244353;
const int M=510;
int n,m,a[M][M],ans=1;
vector<int>v[M<<1];
 
signed main()
{
	cin>>n>>m;
	for (int i=1;i<=n;i++)
	{
		string s;cin>>s;
		for (int j=0;j<m;j++)
			if (s[j]=='B')a[i][j+1]=1;
			else if (s[j]=='R')a[i][j+1]=2;
			else a[i][j+1]=3;
	}
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			v[i+j].pb(a[i][j]);
	for (int i=2;i<=n+m;i++)
	{
		int X=0,cnt=0,f=1;
		for (int j=0;j<v[i].size();j++)
		{
			if (v[i][j]<=2)
			{
				if (!X||X==v[i][j])X=v[i][j];
				else f=0;
			}
			else cnt++;
		}ans=ans*f*(1+(X==0))%Mod;
	}cout<<ans<<endl;
	return 0;
}
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;

#define NUM_CAN 400

struct state
{
  int x;
  int y;
  int score;
  string path;
  set<int> used;
};
bool operator< (const state &state1, const state &state2){
    return state1.score < state2.score;
};

int main()
{
  struct timeval tmp_time;
  gettimeofday(&tmp_time, NULL);
  long long program_init_time = tmp_time.tv_sec*1000000+tmp_time.tv_usec;
  long long fin_time, current_time;
  fin_time = program_init_time + 1500000;

  int si, sj, count;
  cin >> si >> sj;
  vector<vector<int>> tiling(50,vector<int>(50)), grids(50,vector<int>(50));
  for (int i = 0; i < 50; i++)
  {
    for (int j = 0; j < 50; j++) cin >> tiling.at(i).at(j);
  }
  for (int i = 0; i < 50; i++)
  {
    for (int j = 0; j < 50; j++) cin >> grids.at(i).at(j);
  }
  vector<int> dx{1,-1,0,0}, dy{0,0,1,-1};
  string dir = "DURL";
  queue<state> que;
  priority_queue<state> next_que, blank;
  state now_state;
  int best_score;
  string best_path;
  now_state.x = si, now_state.y = sj;
  now_state.score = grids.at(si).at(sj);
  now_state.path = "";
  now_state.used.insert(tiling.at(si).at(sj));
  best_score = now_state.score;
  best_path = now_state.path;
  que.push(now_state);
  count = 0;
  while (!que.empty())
  {
    while (!que.empty())
    {
      count++;
      now_state = que.front();
      que.pop();
      for (int i = 0; i < 4; i++)
      {
        if (now_state.x+dx.at(i) < 0 || now_state.x+dx.at(i) >= 50 || now_state.y+dy.at(i) < 0 || now_state.y+dy.at(i) >= 50)
          continue;
        if (now_state.used.count(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i))))
          continue;
        now_state.score += grids.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i));
        now_state.path += dir.at(i);
        now_state.used.insert(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i)));
        now_state.x += dx.at(i), now_state.y += dy.at(i);
        if (best_score < now_state.score)
        {
          best_score = now_state.score;
          best_path = now_state.path;
        }
        next_que.push(now_state);
        now_state.x -= dx.at(i), now_state.y -= dy.at(i);
        now_state.score -= grids.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i));
        now_state.path.pop_back();
        now_state.used.erase(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i)));
      }
      if (count % 100 == 0)
      {
        gettimeofday(&tmp_time, NULL);
        current_time = tmp_time.tv_sec*1000000 + tmp_time.tv_usec;
        if (current_time > fin_time)
        break;
      }
    }
    if (count % 100 == 0)
    {
      if (current_time > fin_time)
        break;
    }
    for (int i = 0; i < NUM_CAN; i++)
    {
      if (next_que.empty())
        break;
      que.push(next_que.top());
      next_que.pop();
    }
    next_que = blank;
  }
  cout << best_path << endl;
}
