#include "bits/stdc++.h"
using namespace std;
typedef int ll;
typedef pair< int ,int > pii;
typedef pair< ll ,ll> pll;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
#define sll(n) scanf("%lld",&n)
#define sll2(x,y) scanf("%lld%lld",&x,&y)
#define sll3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define REP(i,x,y) for(ll i = x;i <= y;++i)
#define debug(x) cerr<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define debug4(x,y,z,w) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<"\n"
ll power( ll x,ll y,ll z) {
ll ret = 1 ;
while ( y > 0 ) {
if ( y & 1 ) ret = ( ret* x) % z;
x = ( x* x) % z;
y >>= 1 ;
}
return ret;
}
const ll N = 505 , M = 105 , LG = 32 ;
pll dp[ N] [ N] [ M] [ LG] ;
string s, A[ N] ;
ll n,m;
pll move( ll x,ll y, char ch) {
ll dx, dy;
if ( ch == 'L' ) dx = 0 , dy = - 1 ;
else if ( ch == 'R' ) dx = 0 , dy = 1 ;
else if ( ch == 'U' ) dx = - 1 , dy = 0 ;
else if ( ch == 'D' ) dx = 1 , dy = 0 ;
else assert ( 0 ) ;
ll nx = x + dx, ny = y + dy;
if ( nx >= 0 && nx < n && ny >= 0 && ny < m && A[ nx] [ ny] == '.' ) return mp( nx, ny) ;
return mp( x, y) ;
}
int main( ) {
ll q;
cin >> n >> m >> q;
REP( i,0 ,n - 1 ) cin >> A[ i] ;
cin >> s;
ll sz = s.size ( ) ;
REP( i,0 ,n - 1 ) {
REP( j,0 ,m - 1 ) {
if ( A[ i] [ j] == '.' ) {
REP( k,0 ,sz - 1 ) {
dp[ i] [ j] [ k] [ 0 ] = move( i, j, s[ k] ) ;
}
}
}
}
REP( l,1 ,LG - 1 ) {
REP( i,0 ,n - 1 ) {
REP( j,0 ,m - 1 ) {
REP( k,0 ,sz - 1 ) {
ll nk = ( ( 1LL << ( l - 1 ) ) + k) % sz, ni = dp[ i] [ j] [ k] [ l - 1 ] .F , nj = dp[ i] [ j] [ k] [ l - 1 ] .S ;
dp[ i] [ j] [ k] [ l] = dp[ ni] [ nj] [ nk] [ l - 1 ] ;
}
}
}
}
while ( q-- ) {
ll x,y,z,k = 0 ;
cin >> x >> y >> z;
-- x,-- y;
for ( ll i = 0 ; i < LG; ++ i) {
if ( ( 1LL << i) & z) {
auto pp = dp[ x] [ y] [ k] [ i] ;
x = pp.F , y = pp.S ;
k = ( k + ( 1LL << i) ) % sz;
}
}
cout << x + 1 << " " << y + 1 << "\n " ;
}
return 0 ;
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgaW50IGxsOwp0eXBlZGVmIHBhaXI8aW50ICxpbnQ+IHBpaTsKdHlwZWRlZiBwYWlyPGxsICxsbD4gcGxsOwojZGVmaW5lIEYgZmlyc3QKI2RlZmluZSBTIHNlY29uZAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIGFsbChYKSAoWCkuYmVnaW4oKSwgKFgpLmVuZCgpCiNkZWZpbmUgc2xsKG4pIHNjYW5mKCIlbGxkIiwmbikKI2RlZmluZSBzbGwyKHgseSkgc2NhbmYoIiVsbGQlbGxkIiwmeCwmeSkKI2RlZmluZSBzbGwzKHgseSx6KSBzY2FuZigiJWxsZCVsbGQlbGxkIiwmeCwmeSwmeikKI2RlZmluZSBSRVAoaSx4LHkpIGZvcihsbCBpID0geDtpIDw9IHk7KytpKQojZGVmaW5lIGRlYnVnKHgpIGNlcnI8PCN4PDwiOjoiPDx4PDxlbmRsCiN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