Problem:
Score : 1200 points

Problem Statement

In Takaha-shi, the capital of Republic of AtCoder, there are N roads extending east and west, and M roads extending north and south. There are no other roads.
The i-th east-west road from the north and the j-th north-south road from the west cross at the intersection (i, j).
Two east-west roads do not cross, nor do two north-south roads.
The distance between two adjacent roads in the same direction is 1.

Each road is one-way; one can only walk in one direction. The permitted direction for each road is described by a string S of length N and a string T of length M, as follows:

If the i-th character in S is W, one can only walk westward along the i-th east-west road from the north;

If the i-th character in S is E, one can only walk eastward along the i-th east-west road from the north;

If the i-th character in T is N, one can only walk northward along the i-th north-south road from the west;

If the i-th character in T is S, one can only walk southward along the i-th south-west road from the west.

Process the following Q queries:

In the i-th query, a_i, b_i, c_i and d_i are given. What is the minimum distance to travel to reach the intersection (c_i, d_i) from the intersection (a_i, b_i) by walking along the roads?

Constraints

2 \leq N \leq 100000

2 \leq M \leq 100000

2 \leq Q \leq 200000

|S| = N

S consists of W and E.

|T| = M

T consists of N and S.

1 \leq a_i \leq N

1 \leq b_i \leq M

1 \leq c_i \leq N

1 \leq d_i \leq M

(a_i, b_i) \neq (c_i, d_i)

Input

Input is given from Standard Input in the following format:

N M Q
S
T
a_1 b_1 c_1 d_1
a_2 b_2 c_2 d_2
:
a_Q b_Q c_Q d_Q

Output

In the i-th line, print the response to the i-th query. If the intersection (c_i, d_i) cannot be reached from the intersection (a_i, b_i) by walking along the roads, print -1 instead.

Sample Input 1

4 5 4
EEWW
NSNNS
4 1 1 4
1 3 1 2
4 2 3 2
3 3 3 5

Sample Output 1

6
11
5
4

The permitted direction for each road is shown in the following figure (north upward):

For each of the four queries, a route that achieves the minimum travel distance is as follows:

Sample Input 2

3 3 2
EEE
SSS
1 1 3 3
3 3 1 1

Sample Output 2

4
-1

The travel may be impossible.

Sample Input 3

9 7 10
EEEEEWEWW
NSSSNSN
4 6 9 2
3 7 6 7
7 5 3 5
1 1 8 1
4 3 5 4
7 4 6 4
2 5 8 6
6 6 2 7
2 4 7 5
7 2 9 7

Sample Output 3

9
-1
4
9
2
3
7
7
6
-1

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define __builtin_popcount __builtin_popcountll

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
    os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
    os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const map<S, T> &t) {
    os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;

struct handler{
    typedef pii val_t;
    typedef int opr_t;
    handler(){}
    val_t def_val(){ return pii(MOD, -MOD); }
    static val_t update(const val_t &l, const opr_t &r){
        return pii(r, r);
    }
    static val_t merge(const val_t &l, const val_t &r){
        return pii(min(l.first, r.first), max(l.second, r.second));
    }
};

template<typename Handler>
struct SegTree{
    typedef typename Handler::val_t val_t;
    typedef typename Handler::opr_t opr_t;
    vector<val_t> val;
    Handler hdl;
    int n;

    SegTree(int size = 0):hdl(){
        n=1;
        while(n<size) n<<=1;
        val=vector<val_t>(2*n, hdl.def_val());
    }
    SegTree(const vector<val_t> &in):hdl(){
        n=1;
        while(n<in.size()) n<<=1;
        val=vector<val_t>(2*n, hdl.def_val());
        for(int i=n-1 + in.size()-1;i>=0;i--){
            if(n-1 <= i) val[i] = in[i - (n-1)];
            else val[i] = hdl.merge(val[i*2+1],val[i*2+2]);
        }
    }
    void update(int i, const opr_t& a){
        i += n-1;
        val[i] = hdl.update(val[i], a);
        while(i > 0){
            i = (i-1)/2;
            val[i] = hdl.merge(val[i*2+1],val[i*2+2]);
        }
    }
    val_t query(int a,int b,int k,int l,int r){
        if(r<=a||b<=l) return hdl.def_val();
        if(a<=l&&r<=b) return val[k];
        return hdl.merge(query(a, b, k*2+1, l, (l+r)/2),
                         query(a, b, k*2+2, (l+r)/2, r)
        );
    }
    val_t query(int a, int b){return query(a, b, 0, 0, n);}
    val_t operator[](size_t i){return query(i, i+1);}
    friend ostream& operator<<(ostream &os, SegTree<Handler> &t){
        REP(i, t.n) os << (i ? ", " : "[") << t.query(i, i+1);
        return os << "]";
    }
};


int T, n, m, q;
string s, t;
SegTree<handler> segE, segW, segN, segS;

int solve2(int sy, int sx, int gy, int gx) {
    int mi = MOD;
    {
        int ans = 0;
        if (sx <= gx && s[sy] == 'E') ans += gx - sx;
        else if (sx >= gx && s[sy] == 'W') ans += sx - gx;
        else ans += MOD;
        if (sy <= gy && t[gx] == 'S') ans += gy - sy;
        else if (sy >= gy && t[gx] == 'N') ans += sy - gy;
        else ans += MOD;
        mi = min(mi, ans);
    }
    {
        int ans = 0;
        if (sx <= gx && s[gy] == 'E') ans += gx - sx;
        else if (sx >= gx && s[gy] == 'W') ans += sx - gx;
        else ans += MOD;
        if (sy <= gy && t[sx] == 'S') ans += gy - sy;
        else if (sy >= gy && t[sx] == 'N') ans += sy - gy;
        else ans += MOD;
        mi = min(mi, ans);
    }

    return mi;
}

int solve(int sy, int sx, int gy, int gx, int step);

int solve3(int sy, int sx, int gy, int gx, int d1, int step) {
    int p;
    int ans = MOD;
    if (d1 == 0) {
        if (t[sx] != 'N') return MOD;
        p = segE.query(0, min(sy, gy) + 1).second;
        chmin(ans, solve(p, sx, gy, gx, step) + abs(sy - p));
        p = segW.query(0, min(sy, gy) + 1).second;
        chmin(ans, solve(p, sx, gy, gx, step) + abs(sy - p));
    }
    if (d1 == 2) {
        if (t[sx] != 'S') return MOD;
        p = segE.query(max(sy, gy), n).first;
        chmin(ans, solve(p, sx, gy, gx, step) + abs(sy - p));
        p = segW.query(max(sy, gy), n).first;
        chmin(ans, solve(p, sx, gy, gx, step) + abs(sy - p));
    }
    if (d1 == 1) {
        if (s[sy] != 'E') return MOD;
        p = segN.query(max(sx, gx), m).first;
        chmin(ans, solve(sy, p, gy, gx, step) + abs(sx - p));
        p = segS.query(max(sx, gx), m).first;
        chmin(ans, solve(sy, p, gy, gx, step) + abs(sx - p));
    }
    if (d1 == 3) {
        if (s[sy] != 'W') return MOD;
        p = segN.query(0, min(sx, gx)).second;
        chmin(ans, solve(sy, p, gy, gx, step) + abs(sx - p));
        p = segS.query(0, min(sx, gx)).second;
        chmin(ans, solve(sy, p, gy, gx, step) + abs(sx - p));
    }
    return ans;
}

int solve(int sy, int sx, int gy, int gx, int step = 3) {
    if (sx < 0 || sy < 0 || n <= sy || m <= sx) return MOD;
    int ans = solve2(sy, sx, gy, gx);
//    cout << "solve " << sy << ", " << sx << ": " << ans << endl;
    if (step > 0) {
        REP(i, 4) {
            ans = min(ans, solve3(sy, sx, gy, gx, i, step - 1));
        }
    }
    return ans;
}

int main(int argc, char *argv[]){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while(cin >> n >> m >> q >> s >> t){
        segE = SegTree<handler>(n);
        segW = SegTree<handler>(n);
        REP(i, n) {
            if (s[i] == 'E') segE.update(i, i);
            if (s[i] == 'W') segW.update(i, i);
        }
        segN = SegTree<handler>(m);
        segS = SegTree<handler>(m);
        REP(i, m) {
            if (t[i] == 'N') segN.update(i, i);
            if (t[i] == 'S') segS.update(i, i);
        }
        REP(i, q) {
            int sy, sx, gy, gx;
            cin >> sy >> sx >> gy >> gx; sx --; sy --; gx --; gy--;
            int ans = solve(sy, sx, gy, gx);
            cout << (ans >= MOD ? -1 : ans) << endl;
        }
    }
    return 0;
}