/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 61;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll conbinationMemo[201][12];

void cmemoInit(){
    rep(i, 201){
        rep(j, 12){
            conbinationMemo[i][j] = -1;
        }
    }
}
 
ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
ll h, w, k;
vector<string> s;
#define MOD 998244353
ll inv3;
ll memo[5100][5100];
ll extGCD(ll a, ll b, ll &x, ll &y){
	if (b == 0){
		x = 1;
		y = 0;
		return a;
	}
 
	ll d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
ll modpow(ll a, ll b){
    ll res = 1;
    while(b > 0){
        if(b & 1){
            res *= a;
            res %= MOD;
        }
        a *= a;
        a %= MOD;
        b >>= 1;
    }
    return res;
}
ll getinv(ll n){
    ll x, y;
    ll g = extGCD(n, MOD, x, y);
    while(x < 0) x += MOD;
    return x % MOD;
}
ll rec(ll i, ll j){
    if(i == 0 && j == 0){
        ll res = modpow(3, h * w - k);
        if(s[i][j] == '.'){
            return res * inv3 * 2 % MOD;
        } else {
            return res;
        }
    }

    if(memo[i][j] != -1) return memo[i][j];

    ll res = 0;
    if(i - 1 >= 0){
        if(s[i - 1][j] != 'R') {
            res += rec(i - 1, j);
            res %= MOD;
        }
    }
    if(j - 1 >= 0){
        if(s[i][j - 1] != 'D') {
            res += rec(i, j - 1);
            res %= MOD;
        }
    }

    if(s[i][j] == '.' && !(i == h - 1 && j == w - 1)){
        res *= 2;
        res %= MOD;
        res *= inv3;
        res %= MOD;
    }
    return memo[i][j] = res;
}
int main(void){
    cin >> h >> w >> k;
    s.resize(h);
    rep(i, h) s[i].resize(w, '.');
    rep(i, k){
        ll a, b;
        cin >> a >> b;
        a--, b--;
        char c;
        cin >> c;
        s[a][b] = c;
    }
    inv3 = getinv(3);
    rep(i, 5100) rep(j, 5100) memo[i][j] = -1;
    cout << rec(h - 1, w - 1) << endl;
}
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;

typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9+10;
const ll LINF = 1LL*INF*INF;
const int MAXN = 2e5+10;
const int MAXM = 5e3+10;

priority_queue<int> pq;
vector<vector<int> > graph;
queue<int> que;

int A[110][110];
ll fac[110];
int par[110];
int rnk[110];

int root(int x)
{
    if(x==par[x])return x;
    return par[x] = root(par[x]);
}

bool mymerge(int x,int y)
{
    x = root(x);
    y = root(y);

    if(x==y)return false;

    rnk[x]+=rnk[y];
    par[y] = x;

    return true;
}

int main()
{
    int n,m,k,a,b,x,y,q;
    int sum = 0;
    int cnt = 0;
    int mx = 0;
    int mn = INF;
    int cur = 0, idx = -1;
    int tc;

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>n>>k;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>A[i][j];
        }
    }

    fac[0] = 1;

    for(int i=1;i<=n;i++){
        fac[i] = fac[i-1]*(ll)i;
        fac[i] %= PMOD;
    }

    for(int i=1;i<=n;i++){
        par[i] = i;
        rnk[i] = 1;
    }

    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            cnt = 0;
            for(int t=1;t<=n;t++){
                if(A[i][t]+A[j][t]<=k){
                    cnt++;
                }
            }
            if(cnt==n)mymerge(i,j);
        }
    }

    ll res = 1;

    for(int i=1;i<=n;i++){
        if(par[i]==i){
            res*=fac[rnk[i]];
            res%=PMOD;
        }
    }

    for(int i=1;i<=n;i++){
        par[i] = i;
        rnk[i] = 1;
    }

    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            cnt = 0;
            for(int t=1;t<=n;t++){
                if(A[t][i]+A[t][j]<=k){
                    cnt++;
                }
            }
            if(cnt==n)mymerge(i,j);
        }
    }

    for(int i=1;i<=n;i++){
        if(par[i]==i){
            res*=fac[rnk[i]];
            res%=PMOD;
        }
    }


    cout<<res<<"\n";

    return 0;
}
