#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 505
#define ll long long
#define mo 998244353
using namespace std;

int n,m,i,j,k,a[maxn][maxn],c[3];

int main(){
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){
		char ch=getchar();
		while (ch!='R'&&ch!='B'&&ch!='.') ch=getchar();
		for(j=1;j<=m;j++) a[i][j]=(ch=='.')?0:((ch=='R')?1:2),ch=getchar();
	}
	ll ans=1;
	for(i=2;i<=n+m;i++){
		c[0]=c[1]=c[2]=0;
		for(j=1;j<i;j++) if (j>=1&&j<=n&&i-j>=1&&i-j<=m)
			c[a[j][i-j]]++;
		if (c[1]&&c[2]) printf("0\n"),exit(0);
		if (!c[1]&&!c[2]) ans=ans*2%mo;
	}
	printf("%lld\n",ans);
}
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>
// #include "atcoder/all"

using namespace std;

using i64 = long long;

const long long MOD = 1000000007;
const i64 INF = i64(1e18) + 7;


template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}


// doc: https://shibh308.github.io/library/library/lib/classes/modint.cpp.html
template <i64 mod = MOD>
struct ModInt{
    i64 p;

    ModInt() : p(0){}
    ModInt(i64 x){p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod;}

    ModInt& operator+=(const ModInt& y){p = p + *y - ((p + *y) >= mod ? mod : 0); return *this;}
    ModInt& operator-=(const ModInt& y){p = p - *y + (p - *y < 0 ? mod : 0); return *this;}
    ModInt& operator*=(const ModInt& y){p = (p * *y) % mod; return *this;}
    ModInt& operator%=(const ModInt& y){if(y)p %= *y; return *this;}

    ModInt operator+(const ModInt& y) const{ModInt x = *this; return x += y;}
    ModInt operator-(const ModInt& y) const{ModInt x = *this; return x -= y;}
    ModInt operator*(const ModInt& y) const{ModInt x = *this; return x *= y;}
    ModInt operator%(const ModInt& y) const{ModInt x = *this; return x %= y;}

    friend ostream& operator<<(ostream& stream, const ModInt<mod>& x){
        stream << *x;
        return stream;
    }

    friend ostream& operator>>(ostream& stream, const ModInt<mod>& x){
        stream >> *x;
        return stream;
    }

    ModInt& operator++(){p = (p + 1) % mod; return *this;}
    ModInt& operator--(){p = (p - 1 + mod) % mod; return *this;}

    bool operator==(const ModInt& y) const{return p == *y;}
    bool operator!=(const ModInt& y) const{return p != *y;}

    const i64& operator*() const{return p;}
    i64& operator*(){return p;}

};

using mint = ModInt<>;


void solve(long long n, long long p){
    // doc: https://shibh308.github.io/library/library/lib/functions/modpow.cpp.html
    auto mpow = [](auto x, i64 y){
     auto z = x;
     decltype(x) val = y & 1 ? x : decltype(x)(1);
     while(z *= z, y >>= 1)
      if(y & 1)
       val *= z;
     return val;
    };

    cout << (mint(p - 1) * mpow(mint(p - 2), n - 1)) << endl;
}


signed main(){
    long long N;
    scanf("%lld",&N);
    long long P;
    scanf("%lld",&P);
    solve(N, P);
    return 0;
}
