#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sz(a) int(a.size())
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; }
template <typename T, size_t N> ostream& operator <<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; }
template <typename T> ostream& operator <<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; }
template <typename T, class Cmp> ostream& operator <<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}"; return out; }
template <typename U, typename T, class Cmp> ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}"; return out; }
template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; }
template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); }

#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif

void solve() {
	int h,w,x,y;
	cin >> h >> w >> x >> y;
	vector<string> v(h);
	for(int i=0; i<h; i++) {
		cin >> v[i];
	}
	--x;
	--y;
	int ret = 0;
	int dir[] = {0, 1, 0, -1, 0};
	for(int d=0; d<4; d++) {
		int i = x, j = y;
		while(i >= 0 and i < h and j >= 0 and j < w and v[i][j] == '.') {
			++ret;
			i += dir[d];
			j += dir[d+1];
		}
	}
	ret -= 3;
	cout << ret << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	solve();
	return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
//using namespace __gnu_pbds;

#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define fp(i,a,b) for(int i=a ; i<b ; i++)
#define fn(i,a,b) for(int i=a ; i>=b ; i--)
#define ones(x) __builtin_popcount(x)
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define dbg(x) cerr << #x << " = " << x << " "
#define fin cout << "\n";
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
/*
typedef tree<
pii,
null_type,
less<pii>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/
const int M=5e3+7;
const int inf=2e9;
const int mod=998244353;

int h,w,k;
ll dp[M][M];
char mat[M][M];
ll row[M][M],col[M][M];
ll pot3[M];

void go(){
	cin >> h >> w >> k;

	pot3[0] = 1;
	fp(i,1,M) pot3[i] = (pot3[i-1] * 3LL) % mod;

	memset(mat, '0', sizeof mat);
	while (k--){
		int i,j; cin >> i >> j;
		char c; cin >> c;
		mat[i][j] = c;
	}

	fp(i,1,h+1)
		fp(j,1,w+1) row[i][j] = row[i][j-1] + ((mat[i][j] == '0') ? 1 : 0);

	fp(j,1,w+1)
		fp(i,1,h+1) col[j][i] = col[j][i-1] + ((mat[i][j] == '0') ? 1 : 0);

	dp[1][1] = 1;
	fp(i,1,h+1)
		fp(j,1,w+1){
			char c = mat[i][j];
			if (c == 'D' || c == 'X') dp[i+1][j] = (dp[i+1][j] + (pot3[row[i+1][j-1]] * dp[i][j]) % mod) % mod;
			if (c == 'R' || c == 'X') dp[i][j+1] = (dp[i][j+1] + (pot3[col[j+1][i-1]] * dp[i][j]) % mod) % mod;
			if (c == '0'){
				dp[i+1][j] = (dp[i+1][j] + (2LL * pot3[row[i+1][j-1]] * dp[i][j]) % mod) % mod;
				dp[i][j+1] = (dp[i][j+1] + (2LL * pot3[col[j+1][i-1]] * dp[i][j]) % mod) % mod;
			}
		}

	/*fp(i,1,h+1){
		cout << i << " : ";
		fp(j,1,w+1) cout << dp[i][j] << " ";
		fin;
	}*/
	if (mat[h][w] == '0') dp[h][w] *= 3;
	cout << dp[h][w] % mod << "\n";
}
 
int main(){	
 
	fastio;
	int tst=1;
	//cin >> tst;
	fp(i,1,tst+1) go();
 
	return 0;
}