#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define ar array

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int h, w;
	cin >> h >> w;
	string grid[h];
	for (int i=0; i<h; i++) {
		cin >> grid[i];
	}
	int c=0;
	for (int i=1; i<h; i++) {
		bool ok=0;
		for (int j=0; j<w; j++) {
			if (grid[i-1][j]=='.'&&grid[i][j]=='#') {
				if (!ok) c++, ok=1;
			} else ok=0;
		}
	}
	for (int i=h-2; i>=0; i--) {
		bool ok=0;
		for (int j=0; j<w; j++) {
			if (grid[i+1][j]=='.'&&grid[i][j]=='#') {
				if (!ok) c++, ok=1;
			} else ok=0;
		}
	}
	for (int j=1; j<w; j++) {
		bool ok=0;
		for (int i=0; i<h; i++) {
			if (grid[i][j-1]=='.'&&grid[i][j]=='#') {
				if (!ok) c++, ok=1;
			} else ok=0;
		}
	}
	for (int j=w-2; j>=0; j--) {
		bool ok=0;
		for (int i=0; i<h; i++) {
			if (grid[i][j+1]=='.'&&grid[i][j]=='#') {
				if (!ok) c++, ok=1;
			} else ok=0;
		}
	}
	cout << c;
	return 0;
}
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 998244353;
const ll INF = 1e16;
 
int main() {
    ll h, w;
    cin >> h >> w;

    vector<string> board(h);
    rep(i, h) {
        cin >> board[i];
    }

    ll ans = 1;
    exrep(x, 0, h + w - 2) {
        vl cnt(3);
        rep(i, h) {
            ll j = x - i;
            if(j < 0 || w <= j) {
                continue;
            }
            if(board[i][j] == 'R') {
                cnt[1]++;
            }
            else if(board[i][j] == 'B') {
                cnt[2]++;
            }
            else if(board[i][j] == '.') {
                cnt[0]++;
            }
        }
        if(cnt[1] > 0 && cnt[2] > 0) {
            ans = 0;
        } 
        else if(cnt[1] == 0 && cnt[2] == 0) {
            ans = ans * 2LL % mod;
        }
    }

    out(ans);
    re0;
}