output_description: 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.
  
  * * *
input_description: 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
attempt: from collections import defaultdict
  from heapq import heappop, heappush
  
  
  class Graph(object):
      def __init__(self):
          self.graph = defaultdict(list)
  
      def __len__(self):
          return len(self.graph)
  
      def add_edge(self, src, dst, weight=1):
          self.graph[src].append((dst, weight))
  
      def get_nodes(self):
          return self.graph.keys()
  
  
  class Dijkstra(object):
      def __init__(self, graph, start):
          self.g = graph.graph
          self.dist = defaultdict(lambda: float("inf"))
          self.dist[start] = 0
  
          self.prev = defaultdict(lambda: None)
  
          self.Q = []
          heappush(self.Q, (self.dist[start], start))
  
          while self.Q:
              dist_u, u = heappop(self.Q)
              if self.dist[u] < dist_u:
                  continue
              for v, weight in self.g[u]:
                  alt = dist_u + weight
                  if self.dist[v] > alt:
                      self.dist[v] = alt
                      self.prev[v] = u
                      heappush(self.Q, (alt, v))
  
      def shortest_distance(self, goal):
          return self.dist[goal]
  
  
  n, m, q = list(map(int, input().split()))
  s = list(input())
  t = list(input())
  
  inputs = []
  for i, s_ in enumerate(s):
      if s_ == "E":
          inputs += [(i * m + j, i * m + j + 1, 1) for j in range(0, m - 1)]
      else:
          inputs += [(i * m + j, i * m + j - 1, 1) for j in range(m - 1, 0, -1)]
  for i, t_ in enumerate(t):
      if t_ == "N":
          inputs += [(j * m + i, (j - 1) * m + i, 1) for j in range(m - 1, 0, -1)]
      else:
          inputs += [(j * m + i, (j + 1) * m + i, 1) for j in range(0, m - 1)]
  g = Graph()
  for src, dst, weight in inputs:
      g.add_edge(src, dst, weight)
  
  for i in range(q):
      a, b, c, d = list(map(int, input().split()))
      dij = Dijkstra(g, (a - 1) * m + b - 1)
      dist = dij.shortest_distance((c - 1) * m + d - 1)
      if dist != float("inf"):
          print(dist)
      else:
          print(-1)
  
problem_description: 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?
samples: [{"input": "4 5 4\n    EEWW\n    NSNNS\n    4 1 1 4\n    1 3 1 2\n    4 2 3 2\n    3 3 3 5", "output": "6\n    11\n    5\n    4\n    \n\nThe permitted direction for each road is shown in the following figure (north\nupward):\n\n![](https://img.atcoder.jp/exawizards2019/bfb8c54cc4098353946320d8c263807e.png)\n\nFor each of the four queries, a route that achieves the minimum travel\ndistance is as follows:\n\n![](https://img.atcoder.jp/exawizards2019/d1918596004a23a20aa138e591e0ee99.png)\n\n* * *"}, {"input": "3 3 2\n    EEE\n    SSS\n    1 1 3 3\n    3 3 1 1", "output": "4\n    -1\n    \n\nThe travel may be impossible.\n\n* * *"}, {"input": "9 7 10\n    EEEEEWEWW\n    NSSSNSN\n    4 6 9 2\n    3 7 6 7\n    7 5 3 5\n    1 1 8 1\n    4 3 5 4\n    7 4 6 4\n    2 5 8 6\n    6 6 2 7\n    2 4 7 5\n    7 2 9 7", "output": "9\n    -1\n    4\n    9\n    2\n    3\n    7\n    7\n    6\n    -1"}]