#include<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 555;
const int MO = 998244353;
int n,m,a[N][N],b[N*2][3];
char s[N];
int main()
{
	int i,j,x;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){
		scanf("%s",s+1);
		for(j=1;j<=m;j++){
			if(s[j]=='.')
				a[i][j]=0;
			else{
				if(s[j]=='B')
					a[i][j]=1;
				else
					a[i][j]=2;
			}
			b[i+j][a[i][j]]++;
		}
	}
	x=1;
	for(i=2;i<=n+m;i++){
		if(b[i][1]&&b[i][2])
			x=0;
		else{
			if(b[i][1]||b[i][2])
				;
			else
				x=x*2%MO;
		}
	}
	cout<<x;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define int long long
template<typename T>
void dbg_a(T *a,int l,int r){cout<<" {";for(int i = l;i<r;i++) cout<<a[i]<<", ";cout<<a[r]<<"}"<<endl;}
typedef long long ll;
template<typename T = int>
inline T read(){
    T f = 1,r = 0;
    char c = getchar();
    while(c<'0'||c>'9'){if(c == '-') f = -1; c = getchar();}
    while(c>='0'&&c<='9'){ r = (r<<1) + (r<<3) + c - '0';c = getchar();}
    return f*r;
}
typedef pair<int,int> PII;
#define fi first
#define se second
#define mst(a,b) memset(a,b,sizeof(a))
#define For(i,a,b) for(int i = a;i<=b;i++)
#define For_(i,a,b) for(int i = a;i>=b;i--)
#define _for(i,a) for(int i = 0;i<a;i++)
#define All(x) x.begin(),x.end()

//    For(i,1,n) For(j,1,m) printf("f[%lld][%lld] = %lld\n",a,b,c);
 
const int N = 2e5 + 5,M = N*8 + 10,INF = 1e18;
const int mod = 1e9 + 7;
int n,m;

void solve(){
    int x = read(),y = read();
    if(y <= x){
        cout<<(x - y)<<"\n";
        return ;
    }
    queue<PII> q;
    set<int> s;

    q.push({y,0});
    s.insert(y);
    int ans = INF;
    while(q.size()){
        auto [t,d] = q.front();
        q.pop();
        if(d > ans) continue;
        ans = min(ans,abs(t - x) + d);
        if(t % 2 == 0){
            if(!s.count(t/2)){
                q.push({t/2,d+1});
                s.insert(t/2);
            }
        }
        else{
            if(!s.count(t + 1)){
                q.push({t + 1,d + 1});
                s.insert(t + 1);
            }
            if(!s.count(t - 1)){
                q.push({t - 1,d + 1});
                s.insert(t - 1);
            }
        }
    }
    cout<<ans<<"\n";

}

signed main(){
    int T = 1;
    //T = read();
    while(T--) solve();
    system("pause");
    return 0;
}
