#include <iostream>

using namespace std;

typedef long long ll;

const int MOD = 998244353;

struct Modint {
  ll val;
  
  Modint (ll _val = 0)
    : val(_val % MOD) {}

  Modint operator+ (Modint other) const {
    return Modint(val + other.val);
  }

  void operator+= (Modint other) {
    val += other.val;
    val %= MOD;
  }

  Modint operator- () const {
    return Modint(MOD - val);
  }

  Modint operator- (Modint other) const {
    return Modint(val + MOD - other.val);
  }

  void operator-= (Modint other) {
    val += MOD - other.val;
    val %= MOD;
  }

  Modint operator* (Modint other) const {
    return Modint(val * other.val);
  }

  void operator*= (Modint other) {
    val *= other.val;
    val %= MOD;
  }

  bool operator== (Modint other) const {
    return val == other.val;
  }

  bool operator!= (Modint other) const {
    return val != other.val;
  }
};

Modint exp (Modint a, int k) {
  if (k == 0) {
    return Modint(1);
  } else if (k % 2 == 0) {
    Modint half = exp(a, k / 2);
    return half * half;
  } else {
    return a * exp(a, k - 1);
  }
}

Modint inv (Modint a) {
  return exp(a, MOD - 2);
}

ostream& operator<< (ostream& out, Modint p) {
  out << p.val;
  return out;
}

int main () {
  int n, m;
  cin >> n >> m;

  Modint ans = Modint(n) * exp(Modint(m), n);
  Modint im = inv(Modint(m));
  for (int j = 1; j <= m; j++) {
    Modint emj (1); // (m - j) ^ (v - 1)
    Modint emnv (0);
    if (n - 2 >= 0) {
      emnv = exp(Modint(m), n - 2);
    }
    for (int v = 1; v < n; v++) {
      ans -= Modint(n - v) * emj * emnv;

      emj *= Modint(m - j);
      emnv *= im;
    }
  }
  
  cout << ans << endl;
}

//g++  7.4.0

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 998244353

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    ll N,M; cin>>N>>M;
    string s[N];
    ll ans = 1;
    
    for(ll i=0;i<N;++i)
        cin>>s[i];
    
    for(ll j=0;j<M;++j)
    {
        ll i = 0;
        ll jj = j;
        ll tot_R = 0;
        ll tot_B = 0;
        while(i >= 0 && jj >= 0 && i < N && jj < M)
        {
            //cout<<i<<" "<<jj<<endl;
            if(s[i][jj] == 'R')
                tot_R++;
            else if(s[i][jj] == 'B')
                tot_B++;
            --jj;
            ++i;
            
            
        }
        
        //cout<<"----------"<<endl;
        
        if(tot_R != 0 && tot_B != 0)
        {
            cout<<0<<endl;
            return(0);
        }
        
        if(!tot_R && !tot_B)
            ans *= 2LL;
        
        ans %= MOD;
    }
    
    for(ll i=1;i<N;++i)
    {
        ll ii = i;
        ll j = M - 1;
        ll tot_R = 0;
        ll tot_B = 0;
        while(j >= 0 && ii >= 0 && j < M && ii < N)
        {

            if(s[ii][j] == 'R')
                tot_R++;
            else if(s[ii][j] == 'B')
                tot_B++;
                
            j--;
            ii++;
        }
        
        if(tot_R != 0 && tot_B != 0)
        {
            cout<<0<<endl;
            return(0);
        }
        
        if(!tot_R && !tot_B)
            ans *= 2LL;
        
        ans %= MOD;
    }
    
    cout<<ans<<endl;
}