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)

// Dmitry _kun_ Sayutin (2019)

#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::cerr;

using std::vector;
using std::map;
using std::array;
using std::set;
using std::string;

using std::pair;
using std::make_pair;

using std::tuple;
using std::make_tuple;
using std::get;

using std::min;
using std::abs;
using std::max;
using std::swap;

using std::unique;
using std::sort;
using std::generate;
using std::reverse;
using std::min_element;
using std::max_element;

#ifdef LOCAL
#define LASSERT(X) assert(X)
#else
#define LASSERT(X) {}
#endif

template <typename T>
T input() {
    T res;
    cin >> res;
    LASSERT(cin);
    return res;
}

template <typename IT>
void input_seq(IT b, IT e) {
    std::generate(b, e, input<typename std::remove_reference<decltype(*b)>::type>);
}

#define SZ(vec)         int((vec).size())
#define ALL(data)       data.begin(),data.end()
#define RALL(data)      data.rbegin(),data.rend()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()

int main() {
    std::iostream::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // code here
    int n, m, q;
    string S, T;

    cin >> n >> m >> q >> S >> T;

    
    vector<int> prevW(n, -1), prevE(n, -1), prevN(m, -1), prevS(m, -1);
    vector<int> nextW(n, -1), nextE(n, -1), nextN(m, -1), nextS(m, -1);

    for (int i = 0; i != n; ++i) {
        if (i != 0)
            prevW[i] = prevW[i - 1], prevE[i] = prevE[i - 1];

        if (S[i] == 'W')
            prevW[i] = i;
        else
            prevE[i] = i;
    }
    
    for (int i = n - 1; i >= 0; --i) {
        if (i != n - 1)
            nextW[i] = nextW[i + 1], nextE[i] = nextE[i + 1];

        if (S[i] == 'W')
            nextW[i] = i;
        else
            nextE[i] = i;
    }
    

    for (int i = 0; i != m; ++i) {
        if (i != 0)
            prevN[i] = prevN[i - 1], prevS[i] = prevS[i - 1];

        if (T[i] == 'N')
            prevN[i] = i;
        else
            prevS[i] = i;
    }
    
    for (int i = m - 1; i >= 0; --i) {
        if (i != m - 1)
            nextN[i] = nextN[i + 1], nextS[i] = nextS[i + 1];

        if (T[i] == 'N')
            nextN[i] = i;
        else
            nextS[i] = i;
    }
    
    for (int i = 0; i != q; ++i) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        --a; --b; --c; --d;

        std::function<int(int, int, int)> fuck = [&](int x, int y, int rec) {
            if (x == -1 or y == -1)
                return TYPEMAX(int) / 3;
            
            if (x == c and y == d)
                return 0;
                        
            int ans = TYPEMAX(int) / 3;
            if (rec == 0)
                return ans;

            // if (x == c) {
            //     if (bool(y < d) == bool(S[x] == 'E')) {
            //         return abs(d - y);
            //     }
            // }

            // if (y == d) {
            //     if (bool(x < c) == bool(T[y] == 'S')) {
            //         return abs(x - c);
            //     }
            // }
            
            if (bool(y < d) == bool(S[x] == 'E')) {
                ans = min(ans, abs(d - y) + fuck(x, d, rec - 1));
            }
            if (bool(x < c) == bool(T[y] == 'S')) {
                ans = min(ans, abs(x - c) + fuck(c, y, rec - 1));
            }
            
            auto& nxtN = (S[x] == 'W' ? prevN : nextN);
            auto& nxtS = (S[x] == 'W' ? prevS : nextS);

            ans = min(ans, abs(nxtN[y] - y) + fuck(x, nxtN[y], rec - 1));
            ans = min(ans, abs(nxtS[y] - y) + fuck(x, nxtS[y], rec - 1));

            auto& nxtW = (T[y] == 'N' ? prevW : nextW);
            auto& nxtE = (T[y] == 'N' ? prevE : nextE);

            ans = min(ans, abs(nxtW[x] - x) + fuck(nxtW[x], y, rec - 1));
            ans = min(ans, abs(nxtE[x] - x) + fuck(nxtE[x], y, rec - 1));

            return ans;
        };

        int ans = TYPEMAX(int) / 3;
        ans = min(ans, fuck(a, b, 5));
        
        if (ans == TYPEMAX(int) / 3)
            ans = -1;

        cout << ans << "\n";
    }
    
    return 0;
}