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 Ruby (2.3.3) (judge verdict: Runtime Error)

inputs = (gets).split(" ")
I = inputs[0].to_i
J = inputs[1].to_i
Q = inputs[2].to_i

road_i = []
(gets).chars do |c|
  case c
    when 'W'
      road_i << false
    when 'E'
      road_i << true
    else
      break
  end
end

road_j = []
(gets).chars do |c|
  case c
    when 'N'
      road_j << false
    when 'S'
      road_j << true
    else
      break
  end
end

class Dijkstra
  def initialize(ii, jj)
      @iwe = ii.dup
      @jns = jj.dup
      @blist = []
      @amap = []
      @safe = 0
      #p @iwe
      #p @jns
  end

  def blist_add(p, expand=false)
      i,j,d = p
      @blist << p
      if expand then
          @blist << [i-1, j] if (i > 0 && @jns[j])
          @blist << [i+1, j] if (i < I-1 && !@jns[j])
          @blist << [i, j-1] if (j > 0 && @iwe[i])
          @blist << [i, j+1] if (j < J-1 && !@iwe[i])
      end
  end
  def blist_check(ps)
      rps = []
      ps.each do |p|
          p2 = p[0..1]
          if not @blist.include?(p2) then
              rps << p
              @blist << p2
          end
      end
      #p rps
      #@blist = @blist + rps if not rps.length == 0
      return rps
  end
  def blist_rm(p)
      @blist.reject!{ |i,j| i==p[0] && j==p[1] }
  end
  def blist_clear()
      @blist = []
  end

  def amap_add(i,j,ps)
      if @amap[i*J+j].nil? then
          @amap[i*J+j] = [ps]
      else
          @amap[i*J+j].push(ps)
      end
  end
  def amap_search(p1, p2)
      am = @amap[p2[0]*J+p2[1]]
      gs = [p2]
      ret = -1
      if am.nil? then
          return ret,gs
      end
      am.each do |m|
          if ( ix = m.index(p1) ).nil? then
              gs = gs + m # 直接の経路がなければ途中までの地点すべてをゴールにする
          else
              ret = m.length - ix
              #puts "! find !"
              break
          end
      end
      return ret,gs
  end
  def amap_dist_andExpand(p1, p2, pa)
      am = @amap[p2[0]*J+p2[1]]
      gaps = []
      buf = am.dup
      #puts "---"
      #p buf
      am.each do |m|
          ix = m.index(p1)
          if not ix.nil? then
              m2 = m[ix..(m.length-1)]
              #p m2
              #ixs << ix
              buf.push(pa[2] + m2)
              #p pa[2]
              gaps << m2.length
          end
      end
      am = buf
      #p am
      #puts "---"
      return gaps.max
  end

  def next_get(p)
      rp = []
      buf = p[2].dup
      i = p[0]
      j = p[1]
      buf.push(p[0..1])
      rp[rp.length] = [i-1, j, buf] if (i > 0 && !@jns[j])
      rp[rp.length] = [i+1, j, buf] if (i < I-1 && @jns[j])
      rp[rp.length] = [i, j-1, buf] if (j > 0 && !@iwe[i])
      rp[rp.length] = [i, j+1, buf] if (j < J-1 && @iwe[i])
      #p rp
      rp.each do |i,j|
          amap_add(i, j, buf)
      end
      #puts "(#{i}, #{j})"
      #p rp
      return blist_check(rp)
  end

  def search(p1, p2)
      blist_clear()
      #p p1
      #p p2
      dist,goals = amap_search(p1, p2)
      gap = 0
      if dist >= 0 then
          return dist
      end
      #p goals
      pos = []
      pos << [p1[0], p1[1], []] # pos/poの２つめの要素は通ってきたものを示す
      isGoal = false
      ds = []
      #p pos
      blist_add(p1)
      #blist_add(p2)
      blist_rm(p2)
      #while @safe < 12
      while true
          #@safe = @safe + 1
          buf = []
          if pos.length == 0 then
              #pos << [-1, -1, []]
              #puts "end"
              break
          end
          #p pos
          pos.map! do |po|
              #isReach = false
              nps = next_get(po)
              #if nps.length == 0 then
              #    po[0] = -1
              #    next
              #end
              #p "---"
              #p nps
              goals.each.with_index do |g, ix|
                if not ( arrived = nps.find{ |i,j| (i==g[0] && j==g[1]) } ).nil? then
                  #buf << arrived
                  if ix != 0 then
                      #isReach = true
                      gap = amap_dist_andExpand(g, p2, arrived)
                      ds << (arrived[2].length + gap)
                      #p arrived
                      arrived[0] = -1
                      #puts "gap = #{gap} : (#{g[0]}, #{g[1]})"
                      #p arrived[2]
                 else
                      #puts ">> Goal! <<"
                      isGoal = true
                      ds << (arrived[2].length)
                      break
                  end
                end #find
              end #each
              nps.reject! {|i,j| i == -1}
              #p nps
              buf = buf + nps #if not isReach
          end #pos
          pos = buf.dup
          #puts "---"
          #p pos
          #p @blist
          break if isGoal
      end
      #p pos
      #return isGoal ? pos[0][2].length+gap : -1
      #p ds
      return ds.length > 0 ? ds.min : -1
  end
end

dik = Dijkstra.new(road_i, road_j)
answers = []
Q.times do
    ins = (gets).split(" ").map{|g| g.to_i - 1}
    dist = dik.search([ins[0], ins[1]], [ins[2], ins[3]])
    answers << dist
end

answers.each {|ans| puts ans}