#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=998244353;

int n,m,ans=1;
char s[508][508];

signed main() {
	scanf("%lld%lld",&n,&m);
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
			cin>>s[i][j];
	for(int j=2; j<=n+m; j++) {
		int flag=0,ok=1,sum=1;
		for(int i=1; i<j; i++) {
			if(i>n||j-i>m) continue;
			if(flag!=0){
				if(s[i][j-i]=='B'&&flag==1) ok=0;
				if(s[i][j-i]=='R'&&flag==-1) ok=0;
			}
			if(s[i][j-i]=='R'&&flag==0) flag=1;
			if(s[i][j-i]=='B'&&flag==0) flag=-1;
			if(s[i][j-i]!='.') sum=0;
		}
		if(ok==0) ans=0;
		else if(sum) ans=ans*2%mod;
	}
	printf("%lld\n",ans);
	return 0;
}
#include <bits/stdc++.h>
 
#define f(i,a,b) for( ll i = a; i < b ; i++ ) 
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define inter ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
 
// policy-based
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;	
 
 
using namespace std;
 
 
typedef long long ll; // int or long long
typedef long double ld;
typedef pair<ll,ll> ii ;
typedef vector<ll>  vi ;
typedef vector<ii> vii ;
 
/*
typedef tree<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
*/
 
const ll MAX = 1e3 + 100;
const ll inf = 1e9;
const ll mod = 998244353;

ll c[100][100];

struct DSU{
	ll p[200],sz[200];
	void ini(ll n){
		f(i,0,n) p[i] = i,sz[i] = 1;
 	}
	ll find(ll u){
		return (p[u]==u?u:p[u]=find(p[u]));
	}
	void uni(ll u,ll v){
		ll x = find(u),y = find(v);
		if(x == y) return;
		p[y] = x;
		sz[x] += sz[y];
	}
};

DSU a,b;

ll fac[MAX];

int main(){
	fastio;
	fac[0] = 1;
	f(i,1,MAX) fac[i] = (fac[i-1]*i) % mod;
	ll n,k,ans = 1;
	cin >> n >> k;
	f(i,0,n) f(j,0,n) cin >> c[i][j];
	a.ini(n);
	b.ini(n);
	f(x,0,n){
		f(y,x+1,n){
			bool go = true;
			f(i,0,n) if(c[i][x] + c[i][y] > k) go = false; 
			if(go) a.uni(x,y);
		}
	}
	f(x,0,n){
		f(y,x+1,n){
			bool go = true;
			f(i,0,n) if(c[x][i] + c[y][i] > k) go = false; 
			if(go) b.uni(x,y);
		}
	}
	
	f(i,0,n) if(a.find(i) == i) (ans *=  fac[a.sz[i]] )%=mod;
	f(i,0,n) if(b.find(i) == i) (ans *=  fac[b.sz[i]] )%=mod; 
	cout << ans << endl;
}