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++ (Clang 10.0.0) (judge verdict: Wrong Answer)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m,q;
    cin >> n >> m >> q;
    string s,t;
    cin >> s >> t;
    vector<int> L,R;
    vector<int> U,D;
    rep(i,n){
        if(s[i]=='W'){
            L.push_back(i);
        }else{
            R.push_back(i);
        }
    }
    rep(i,m){
        if(t[i]=='N'){
            U.push_back(i);
        }else{
            D.push_back(i);
        }
    }
    rep(zz,q){
        int a,b,c,d;
        cin >> a >> b >> c >> d;
        a--;b--;c--;d--;
        queue<pair<pair<pair<int,int>,pair<int,int> >,pair<int,int> > >q;
        if(t[b]=='N'){
            q.push(MP(MP(MP(0,0),MP(a,b)),MP(-1,1)));
        }else{
            q.push(MP(MP(MP(0,0),MP(a,b)),MP(1,1)));
        }
        if(s[a]=='W'){
            q.push(MP(MP(MP(0,0),MP(a,b)),MP(-1,0)));
        }else{
            q.push(MP(MP(MP(0,0),MP(a,b)),MP(1,0)));
        }
        int mi = inf;
        while(!q.empty()){
            auto x = q.front();
            q.pop();
            int dist = x.first.first.first;
            int cnt = x.first.first.second;
            int na = x.first.second.first;
            int nb = x.first.second.second;
            int pl = x.second.first;
            int dir = x.second.second;
            // cerr << na << " " << nb << " " << dist << " " << cnt << " " << pl << " " << dir << endl;
            if(na==c&&nb==d){
                chmin(mi,dist);
                continue;
            }
            if(cnt==5)continue;
            if(dir==1){// UP DOWN
                if(pl==-1){//UP
                    if(c<na&&nb==d){
                        chmin(mi,dist+na-c);
                        continue;
                    }        
                    if(nb<=d){
                        int tar = min(na,c);
                        auto x = lower_bound(all(R),tar);
                        if(x!=R.end()){
                            int nna = *x;
                            if(nna<na)q.push(MP(MP(MP(dist+abs(nna-na),cnt+1),MP(nna,nb)),MP(1,0)));
                        }
                        if(x!=R.begin()){
                            x--;
                            int nna = *x;
                            if(nna<na)q.push(MP(MP(MP(dist+abs(nna-na),cnt+1),MP(nna,nb)),MP(1,0)));
                        }
                    }
                    if(nb>=d){
                        int tar = min(na,c);
                        auto x = lower_bound(all(L),tar);
                        if(x!=L.end()){
                            int nna = *x;
                            if(nna<na)q.push(MP(MP(MP(dist+abs(nna-na),cnt+1),MP(nna,nb)),MP(-1,0)));
                        }
                        if(x!=L.begin()){
                            x--;
                            int nna = *x;
                            if(nna<na)q.push(MP(MP(MP(dist+abs(nna-na),cnt+1),MP(nna,nb)),MP(-1,0)));
                        }
                    }
                }else{//DOWN
                    if(c>na&&nb==d){
                        chmin(mi,dist+abs(c-na));
                        continue;
                    }        
                    if(nb<=d){
                        int tar = max(na,c);
                        auto x = lower_bound(all(R),tar);
                        if(x!=R.end()){
                            int nna = *x;
                            if(nna>na)q.push(MP(MP(MP(dist+abs(nna-na),cnt+1),MP(nna,nb)),MP(1,0)));
                        }
                        if(x!=R.begin()){
                            x--;
                            int nna = *x;
                            if(nna>na)q.push(MP(MP(MP(dist+abs(nna-na),cnt+1),MP(nna,nb)),MP(1,0)));
                        }
                    }
                    if(nb>=d){
                        int tar = max(na,c);
                        auto x = lower_bound(all(L),tar);
                        if(x!=L.end()){
                            int nna = *x;
                            if(nna>na)q.push(MP(MP(MP(dist+abs(nna-na),cnt+1),MP(nna,nb)),MP(-1,0)));
                        }
                        if(x!=L.begin()){
                            x--;
                            int nna = *x;
                            if(nna>na)q.push(MP(MP(MP(dist+abs(nna-na),cnt+1),MP(nna,nb)),MP(-1,0)));
                        }
                    }
                }

            }else{// LEFT RIGHT
                if(pl==-1){//LEFT
                    if(d<nb&&na==c){
                        chmin(mi,dist+abs(nb-d));
                        continue;
                    }        
                    if(na<=c){
                        int tar = min(nb,d);
                        auto x = lower_bound(all(D),tar);
                        if(x!=D.end()){
                            int nnb = *x;
                            if(nnb<nb)q.push(MP(MP(MP(dist+abs(nnb-nb),cnt+1),MP(na,nnb)),MP(1,1)));
                        }
                        if(x!=D.begin()){
                            x--;
                            int nnb = *x;
                            if(nnb<nb)q.push(MP(MP(MP(dist+abs(nnb-nb),cnt+1),MP(na,nnb)),MP(1,1)));
                        }
                    }
                    if(na>=c){
                        int tar = min(nb,d);
                        auto x = lower_bound(all(U),tar);
                        if(x!=U.end()){
                            int nnb = *x;
                            if(nnb<nb)q.push(MP(MP(MP(dist+abs(nnb-nb),cnt+1),MP(na,nnb)),MP(-1,1)));
                        }
                        if(x!=U.begin()){
                            x--;
                            int nnb = *x;
                            if(nnb<nb)q.push(MP(MP(MP(dist+abs(nnb-nb),cnt+1),MP(na,nnb)),MP(-1,1)));
                        }
                    }
                }else{//RIGHT
                    if(d>nb&&na==c){
                        chmin(mi,dist+abs(d-nb));
                        continue;
                    }        
                    if(na<=c){
                        int tar = max(nb,d);
                        auto x = lower_bound(all(D),tar);
                        if(x!=D.end()){
                            int nnb = *x;
                            if(nnb>nb)q.push(MP(MP(MP(dist+abs(nnb-nb),cnt+1),MP(na,nnb)),MP(1,1)));
                        }
                        if(x!=D.begin()){
                            x--;
                            int nnb = *x;
                            if(nnb>nb)q.push(MP(MP(MP(dist+abs(nnb-nb),cnt+1),MP(na,nnb)),MP(1,1)));
                        }
                    }
                    if(na>=c){
                        int tar = max(nb,d);
                        auto x = lower_bound(all(U),tar);
                        if(x!=U.end()){
                            int nnb = *x;
                            if(nnb>nb)q.push(MP(MP(MP(dist+abs(nnb-nb),cnt+1),MP(na,nnb)),MP(-1,1)));
                        }
                        if(x!=U.begin()){
                            x--;
                            int nnb = *x;
                            if(nnb>nb)q.push(MP(MP(MP(dist+abs(nnb-nb),cnt+1),MP(na,nnb)),MP(-1,1)));
                        }
                    }
                }
            }
        }
        if(mi!=inf){
            cout << mi << "\n";
        }else{
            cout << -1 << "\n";
        }
    }
    return 0;
}