There are $M$ levels for a building numbered from $1$ to $M$ from top to bottom, each level having $N$ parking spots numbered from $1$ to $N$ from left to right. Some spots might have a car while other may be empty, the information of which is given in form of two dimensional character array $C$ ($C_{i, j}$ denote parking spot at $j$-th position on $i$-th level).
There is a thief who wants to unlock all the cars. Now, he is skilled such that for the first time, he can directly reach in any parking spot in no time. Basically he can reach the first car to be stolen in 0 time.
Now, he can move within the parking lot only in following manner, each taking 1 unit of time:
- Move down a level. That is, if current position is $(i, j)$, then he reaches $(i+1, j)$
- If current position is $(i, j)$ and if
- $i$ is odd, then he can move from $(i, j)$ to $(i, j+1)$
- $i$ is even, then he can move from $(i, j)$ to $(i, j-1)$
Note that he wants to unlock the cars in minimum time and the car is unlocked as soon as the thief reaches that parking spot.If the parking lot is empty, then the time taken is considered to be 0.  
Find the minimum time when all the cars would be unlocked. Note that once all cars are unlocked, the thief can escape instantly, so this time does not count.

-----Input :-----
- The first line of input contains a single integer $T$ (number of test cases).
- First liine of each test case contains $M$ and $N$(number of levels and spots per each level)  
- Next $M$ line contains $N$ space separated characters $C_{i, 1}, C_{i, 2} \ldots C_{i, N}$  where $(1\leq i\leq M)$ and $C_{i, j}$ is either $'P'$ or $'N'$ (without quotes).
If the spot contains $'P'$, then a car is parked there. else, it’s not parked.

-----Output :-----
For each test case print a single integer, the minimum time in which thief can unlock all cars.

-----Constraints :-----
- $1\leq T \leq100.$ 
- $1\leq M,N \leq300$ 

-----Subtasks :-----
- 
Subtask 1 (20 points): $1\leq M \leq2.$ 
- 
Subtask 2 (80 points): Original Constraints

-----Sample Input :-----
2
4 5
N P N N P
N N P N N
N P N N N
P N N N N
3 3
N P P
P P P 
P P N

-----Sample Output :-----
10
6

-----Explanation:-----
In the first case, He will select the spot $(1,2)$ and the path he takes will be $(1,2)→(1,3)→(1,4)→(1,5)→(2,5)→(2,4)→(2,3)→(2,2)→(3,2)→(4,2)→(4,1)$

So, he takes 10 steps to unlock all the cars.
In the second case, He will select the spot $(1,2)$ and the path he takes will be $(1,2)→(1,3)→(2,3)→(2,2)→(2,1)→(3,1)→(3,2)$.
So, he takes 6 steps.

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '# cook your dish here\nT = int(input())\n\nfor i in range(T):\n m, n = input().split()\n m = int(m)\n n = int(n)\n c = []\n # m = 4\n # n  = 5\n # c = [[\'N\', \'P\', \'N\', \'N\', \'P\'], [\'N\', \'N\', \'P\', \'N\', \'N\'], [\'N\', \'P\', \'N\', \xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\'N\', \'N\'], [\'P\', \'N\', \'N\', \'N\', \'N\']]\n start_pos_m = -1\n start_pos_n = -1\n for j in range(m):\n  c.append(input().split())\n  if(\'P\' in c[-1] and start_pos_m == -1):\n   start_pos_n = c[-1].index(\'P\')\n   start_pos_m = j\n # print(c)\n cur_pos_m = start_pos_m\n cur_pos_n = start_pos_n\n next_pos_m = cur_pos_m\n number_of_steps = 0\n next_pos_n = start_pos_n\n for j in range(start_pos_m, m - 1):\n  if(\'P\' not in c[j]):\n   if(j % 2 == 0):\n    if(\'P\' in c[j + 1][next_pos_n + 1: n]):\n     print(1)\n     rev = c[j + 1][: : -1]\n     next_pos_n = n - 1 - rev.index(\'P\')\n     number_of_steps += next_pos_n - cur_pos_n\n     cur_pos_n = next_pos_n\n   else:\n    if(\'P\' in c[j + 1][0 : next_pos_n]):\n     next_pos_n = c[j + 1].index(\'P\')\n     number_of_steps += cur_pos_n - next_pos_n\n     cur_pos_n = next_pos_n\n   if(\'P\' in c[j + 1]):\n    number_of_steps += j - cur_pos_m \n   # print(c[j])\n\n  elif(j % 2 == 0):    \n   rev = c[j][: : -1]\n   next_pos_n = n - 1 - rev.index(\'P\')\n   if(\'P\' in c[j + 1][next_pos_n : n]):\n    rev = c[j + 1][: : -1]\n    next_pos_n = n - 1 - rev.index(\'P\')\n   number_of_steps += next_pos_n - cur_pos_n\n   cur_pos_n = next_pos_n\n   number_of_steps += 1\n  else:\n   # rev = c[j][:: -1]\n   # print("rev : ", rev)\n   next_pos_n = c[j].index(\'P\')\n   if(\'P\' in c[j + 1][0 : next_pos_n + 1]):\n    next_pos_n = c[j + 1].index(\'P\')\n   number_of_steps += cur_pos_n - next_pos_n\n   cur_pos_n = next_pos_n\n   number_of_steps += 1\n  cur_pos_m = j\n\n if(m % 2 == 0):\n  if(\'P\' in c[-1][0 : cur_pos_n]):\n   number_of_steps += cur_pos_n - c[-1][0 : cur_pos_n].index(\'P\') \n else:\n  if(\'P\' in c[-1][cur_pos_n + 1 : :]):\n   number_of_steps += n - 1 - c[-1][:: -1].index(\'P\') - cur_pos_n\n \n print(number_of_steps)'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 14349