#include<bits/stdc++.h>
using namespace std;
const int N=505;
char pic[N][N];
int book[N][N];
const int mod=998244353;
vector<char> c[N*2];
int n,m;
struct node
{
    int x,y,d;
};
queue<node> q;
int check(int p)
{
    int b,r,d;
    b=r=d=0;
    for (int i=0;i<c[p].size();i++)
    {
        char x=c[p][i];
        //printf("%c \n",x);
        if (x=='.')
            d++;
        else if (x=='B')
            b++;
        else
            r++;
    }
    //printf("%d %d %d %d %d\n",p,b,r,d,c[p].size());
    if (r==0&&b==0)
        return 2;
    if (r!=0&&b!=0)
        return 0;
    return 1;
}
int main()
{

    scanf("%d%d\n",&n,&m);
    for (int i=0;i<n;i++)
        scanf("%s",pic[i]);

    q.push({0,0,0});
    int mx=0;
    while (!q.empty())
    {
        node t=q.front();
        q.pop();
        if (book[t.x][t.y])
            continue;
        book[t.x][t.y]=1;
        mx=max(mx,t.d);
        int x=t.x,y=t.y,d=t.d;
        //printf("%d %d %d %c\n",x,y,d,pic[x][y]);
        c[t.d].push_back(pic[t.x][t.y]);
        if (x+1<n&&!book[x+1][y])
            q.push({x+1,y,d+1});
        if (x-1>=0&&!book[x-1][y])
            q.push({x-1,y,d+1});
        if (y+1<m&&!book[x][y+1])
            q.push({x,y+1,d+1});
        if (y-1>=0&&!book[x][y-1])
            q.push({x,y-1,d+1});
    }
    long long ans=1;
    for (int i=0;i<=mx;i++)
    {
        ans=ans*check(i)%mod;
    }
    printf("%lld\n",ans);
}

#include <bits/stdc++.h>
#define int long long int
#define all(x) x.begin(), x.end()
#define send ios_base::sync_with_stdio(false);
#define help cin.tie(NULL)
#define inf (int)(1e17+1)
#define mod (int)(998244353)
#define N (int)(2e5+5)
#define fi first
#define se second
#define endl "\n"
#define double long double
#define eps (double)(1e-9)
#define sa cout<<"sa"<<endl
using namespace std;

namespace math{
	vector <int> fac(N,1);
	int exp(int a,int b){
		if(!b) return 1;
		if(b&1) return a*exp(a,b-1)%mod;
		int t=exp(a,b/2);
		return t*t%mod;
	}
	void fc(){for(int i=2;i<N;i++)fac[i]=i*fac[i-1]%mod;}
	int com(int n,int r){return (fac[n]*exp(fac[r]*fac[n-r]%mod,mod-2))%mod;}
	int per(int n,int r){return (fac[n]*exp(fac[n-r],mod-2))%mod;}
	vector <int> getpr(int x){
		vector <int> res;if(x!=1) res.push_back(x);
		for(int i=2;i*i<=x;i++){
			if(i*i==x) res.push_back(i);
			else if(x%i==0) res.push_back(i),res.push_back(x/i);;
	}return res;}
}using namespace math;

void solve(){
	int n,m;
	cin>>n>>m;
	vector <string> v(n);
	for(int i=0;i<n;i++) cin>>v[i];
	int ans=0;
	vector <int> path(m+n);
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(v[i][j]=='B'&&path[i+j]==1) {cout<<0;return;}
			if(v[i][j]=='R'&&path[i+j]==2) {cout<<0;return;}
			if(v[i][j]=='B') path[i+j]=2;
			else if(v[i][j]=='R') path[i+j]=1;
		}
	}
	for(int i=0;i<n+m-1;i++) ans+=!path[i];
	cout<<exp(2,ans);
}
 
int32_t main(){ 
    send help;
    #ifdef LOCAL
  	freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}