// C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
const LL MOD = 998244353;
int board[1010][1010]; // マスの色(0: 色なし, 1: 赤, 2: 青).
int cnt[1010][3];      // 出現回数(0: 色なし, 1: 赤, 2: 青).

int main(){
    
    // 1. 入力情報.
    int H, W;
    scanf("%d %d", &H, &W);
    rep(i, H){
        char c[505];
        scanf("%s", c);
        rep(j, W){
            if(c[j] == 'R') board[i][j] = 1;
            if(c[j] == 'B') board[i][j] = 2;
        }
    }
    
    // 2. 右上から左下に向かって, マスの色を確認.
    rep(k, H + W - 1){
        rep(i, H + W - 1){
            int j = k - i;
            if(j >= 0) cnt[k][board[i][j]]++;
            else       break;
        }
    }
    // rep(i, H + W - 1) printf("n=%d r=%d b=%d\n", cnt[i][0], cnt[i][1], cnt[i][2]);
    
    // 3. 色なしのみをカウント.
    // -> 赤, 青が混在している場合は, 着色不可能と判定.
    // -> 開始地点, 終了地点も, '.' となる場合があるので, 注意.
    LL ans = 1;
    int noColor = 0;
    rep(i, H + W - 1){
        if(cnt[i][1] && cnt[i][2])   ans = 0;
        if(!cnt[i][1] && !cnt[i][2]) noColor++;
    }
    
    // 4. 塗り方の総数は？
    rep(i, noColor) ans <<= 1, ans %= MOD;
    
    // 5. 出力.
    printf("%lld\n", ans);
    return 0;
    
}
/**
*    author:  shu8Cream
*    created: 26.04.2021 20:04:07
**/

#include <bits/stdc++.h>
#include <chrono>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using namespace std::chrono;
inline double get_time_sec(void){
    return static_cast<double>(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count())/1000000000;
}

std::random_device seed_gen;
std::mt19937 engine(seed_gen());

int si,sj;
vvi t(50,vi(50));
vvi p(50,vi(50));
vector<vector<P>> locate(2500);

double start;
string finans = "";
int finscore = 0;

const vi di = {-1,0,1,0,-1,0};
const vi dj = {0,-1,0,1,0,-1};
const vector<string> direct = {"U","L","D","R","U","L"};

void input(){
    cin >> si >> sj;
    rep(i,50)rep(j,50){
        cin >> t[i][j];
        locate[t[i][j]].push_back({i,j});
    }
    rep(i,50)rep(j,50) cin >> p[i][j];
}

pair<int, string> fans = {-100, "U"};

vvi used(50,vi(50));
void dfs(int score, int i, int j, string s, vi rand){
    double end = get_time_sec();
    if(end-start>=1.99){
        cout << fans.second << endl;
        exit(0);
    }
    // shuffle(all(rand),engine);

    bool ok = true;
    rep(k,4){
        int ni = i + di[rand[k]];
        int nj = j + dj[rand[k]];
        if(ni<0 || ni>=50 || nj<0 || nj>=50 || used[ni][nj])
            continue;
        ok = false;
        int nowpos = t[ni][nj];
        for(auto [x,y] : locate[nowpos]){
            used[x][y]=1;
        }
        dfs(score+p[ni][nj],ni,nj,s+direct[rand[k]], rand);

        for(auto [x,y] : locate[nowpos]){
            used[x][y]=0;
        }
    }

    if(ok){
        if(fans.first<score){
            fans.first = score;
            fans.second = s;
        }
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    input();
    start = get_time_sec();

    int sPos = t[si][sj];
    for(auto [x,y]: locate[sPos]){
        used[x][y]=1;
    }

    vi rand = {0,1,2,3};
    do{
        dfs(p[si][sj],si,sj,finans,rand);
    }while(next_permutation(all(rand)));

    
    cout << finans << endl;
}