P21926 [HackerEarth] The middle earth is in danger as Sauron has prepared an army of Orcs to destroy the men. To kill Sauron and save the world we need to destroy his Ring and it is only possible if the ring
普及
题目描述

The middle earth is in danger as Sauron has prepared an army of Orcs to destroy the men. To kill Sauron and save the world we need to destroy his Ring and it is only possible if the ring is thrown in the lava ( Magma ). Gandalf and Frodo accepted this challenge and decided to save the world at any cost. Gandalf found one map which shows the way to lava. The Map is represented as a matrix which shows the locations of Dragons, Trees and Lava (located at bottom-right corner). Frodo can enter in the matrix only at the cell ( 1 , 1)(top-left corner) and he has to reach the cell ( N , M ).

The matrix has N rows and M columns. It starts with ( 1 , 1). Lava is in
( N , M)th cell. Rest of the cells are occupied either by Trees or Dragons. He can only move right or down from current position.

He starts with zero energy. If he enters in a cell that has Tree (T), he can eat some fruits and gain 150 units energy. But If he enters in Dragon's (D) cell, then he needs 100 units energy to kill Dragon. If he doesn't have the required energy then Dragon will kill Frodo.

Gandalf and Frodo have not studied Algorithm Techniques from Sir Zaveri ( :P ). But they know you studied from him. So help Frodo and Gandalf in destroying the Ring and save the world.

INPUT :
First line contains T , No. of test cases.
In each Test case - First line contains space separated integers N and M representing no. of rows and columns. Next N lines contain M space separated characters representing Tree (as T ) or Dragon (as D ) or Lava (as L).

OUTPUT :
For each test case, print the value of maximum energy in Frodo when he reaches the lava. If it is not possible print -1.

CONSTRAINTS :
1≤ T ≤ 10^3
1 ≤ N , M ≤ 200
Map[i][j] = { T , D , L }

Large input files. Use faster I/O.

Problem Setter : Rajan Kasodariya

SAMPLE INPUT
1
4 4
T T D T
D T T T
T D T D
D T D L

SAMPLE OUTPUT
650

Explanation

One possible root is :
( 1 , 1) , ( 1 , 2) , ( 2 , 2 ) , ( 2 , 3 ) , ( 3 , 3 ) , ( 4 , 3 ) , ( 4 , 4)

输入格式

见题面

输出格式

见题面

样例输入
11 3 3 T T T T T T T T L 3 3 T T D T D T D T L 3 3 T T D T D D D D L 3 3 T D D D D D D D L 3 3 T D T D D D D D L 3 3 T D D D D T D D L 3 3 T D D D T T D D L 3 3 T T D D D D D D L 4 4 T T D D D D D D D D D D D D D L 3 4 T T D D D D D D T D D L 4 4 T T D D D D D D D D D D D D T L
样例输出
-1 38050 12900 -1 21450 21850 24800 4650 32000 34750 42700 19000 13850 28000 28200 3100 15650 35750 11550 18250 19400 12150 42650 24100 29600 33300 40650 7550 39900 18750 4700 42150 18150 34650 18050 20400 12200 22850 16400 32150 31450 16400 22800 17450 6350 14850 37550 43400 15750 27150 31350 21050 43150 -1 15800 35100 -1 30950 23700 26950 25800 31550 43350 19350 17700 18400 37000 8600 26300 24200 1500 24350 15600 19250 6750 9650 34900 36100 18200 29400 6850 10450 44000 27600 31850 30000 13150 18950 31950 2050 20750 11250 45050 20600 27900 -1 5500 43250 18000 14300 9850 8050 12600 39050 24700 41200 2650 12500 20100 4400 31050 14400 22550 22900 12050 19550 10800 26700 12100 29700 18300 23700 31550 39800 22450 39000 25150 21900 43550 29550 22100 20950 8050 40300 25850 24900 11500 7950 36500 41550 25350 19050 -1 22600 19950 1900 32400 19400 13000 25800 34500 -1 18200 19650 40100 26550 13650 33850 22650 15750 28750 13450 28900 4650 23250 3550 3700 34500 43550 26950 34850 22300 33500 46350 11900 28550 -1 10700 14000 41100 17400 30350 20050 16900 38750 35050 18500 27700 35050 -1 32700 27800 22250 29600 35200 41100 26350 37850 15900 21700 15800 40250 -1 8100 8650 22600 30250 20700 29550 7350 38000 21800 19250 41750 36300 37850 38600 22900 40950 12450 15400 16300 19350 35100 24900 38000 24950 17400 22700 27200 15050 20850 27800 31150 14250 16050 23250 20700 26150 20650 25050 7800 21200 34200 24450 36150 40300 14050 38900 26450 26450 35850 19800 8800 28200 9500 5900 27950 35750 36850 23550 30850 24050 22900 31200 36050 -1 27500 30850 16200 27650 10200 18650 6450 4400 19250 29550 18400 16750 750 20100 26450 11350 24050 31900 21400 26950 12250 14950 24100 6300 26000 26800 45600 13100 23300 42100 20950 3900 34100 23650 30300 19500 24800 22100 37000 29450 10950 30400 36100 14950 42650 41900 19950 29500 13850 11950 23000 31150 39600 11850 21400 14800 30350 14650 27650 34000 39600 30050 26600 16000 25750 20800 47300 31400 13350 16600 8850 22400 41250 -1 19300 15850 15600 21250 17000 37100 14200 5950 14000 18350 17200 -1 9350 -1 31000 19100 18700 26400 8850 40800 2700 23700 11700 31450 26500 -1 42850 10400 2800 5850 19150 24250 17550 15000 11900 16300 31100 40650 9200 18650 17600 16750 27600 36000 10000 26400 21350 7600 22500 7100 13800 28000 24550 -1 32050 11050 20950 8450 21850 38750 13950 34600 18700 32300 23250 34400 10250 19900 28700 23350 31200 29750 31100 11400 29100 18200 33000 21200 32300 18150 31700 12950 31350 33050 22500 17300 26150 -1 42650 32200 16600 24550 30200 24900 19550 29250 15100 -1 28200 34200 25900 2250 28450 12350 20700 31800 36600 30050 11650 39700 38900 -1 9650 19850 5600 25400 12900 33200 20600 38050 38750 27700 15550 3750 5300 22150 22350 36750 18250 23250 19300 43050 32300 24700 44850 17050 11750 41650 11950 30700 7700 5650 13750 38050 29650 20150 30050 6150 25200 33750 19350 38050 9750 17600 27900 10750 40250 -1 15150 32700 12350 9950 11900 2650 16550 15650 41950 18050 42450 7950 16300 26400 32750 31200 45400 15500 26150 38150 16700 27250 19850 17600 37000 21600 20100 36300 3950 28850 33500 25450 33050 21650 16150 17600 44300 12750 19450 5600 18600 -1 18850 18250 26850 14600 39350 27650 16200 10050 17650 15150 9750 30600 15500 21600 19300 33700 6550 8200 5100 8600 32200 11550 39150 23050 12350 15750 15000 13700 34950 14250 34650 4750 23150 26550 31650 19600 13450 10850 6400 12150 26000 11350 20350 25400 16600 13350 7200 -1 32300 12200 4450 34250 -1 14850 31150 3450 15400 35700 21000 27850 32550 35100 23750 28950 10550 24850 43000 22700 29600 -1 25300 30800 23800 25650 36700 37300 18200 23650 -1 27150 19900 26850 1700 4500 8200 16050 23400 11550 12350 4700 16800 4300 -1 19000 29500 11800 40400 11250 28550 23150 8700 43750 42700 21650 35600 39050 26100 25250 41650 28000 28200 -1 15550 13050 40950 44450 20950 21100 7300 28200 29650 23200 11950 16650 7900 27150 34800 37700 -1 33500 33250 38550 20500 33550 31400 26600 25100 19250 4700 21550 3500 45750 29000 18300 27350 26100 33950 4050 19350 12850 -1 5200 12850 23100 38450 10350 18050 22450 12950 34850 18000 20450 17850 27900 37650 25800 9500 16850 37400 24700 7100 17350 1250 22600 41500 22600 25850 21000 20750 32150 30950 23400 42650 23350 12150 33550 -1 8000 4050 23800 21550 23600 -1 13200 19550 32800 34450 20600 19900 40350 12750 23950 33800 39050 26800 14950 10350 11400 23400 10100 5950 30250 -1 8900 16450 8600 16500 8700 8900 22950 24150 7350 26950 13850 2400 18950 28200 28900 31050 23100 7000 34000 24800 1750 40300 17750 26150 29600 24950 16500 21050 -1 16950 15550 13750 30650 33750 30400 17850 34600 14550 9900 19150 32200 33150 21700 36950 35700 23600 35350 46900 27450 9800 25000 20450 31750 11050 23250 14350 30900 14350 41250 33950 44300 24500 14500 27950 16500 35600 24500 30850 24850 21400 23700 8000 13850 30850 9400 10500 10500 34950 2550 29300 11000 18450 6000 20450 11100 42350 17850 43050 47850 17250 37250 11650 -1 40150 27750 11250 18750 10450 47500 24350 31450 34600 6600 2950 13500 19750 29000 8200 35650 33700 14750 20100 800 9950 39900 23050 14100 28050 16650 45150 23600 32850 16300 22800 26850 17100 10400 25400 6750 20800 11300 -1 47500 24900 8650 4500 13300 38500 20500 11150 27600 -1 17500 28550 24000 3700 14950 17550 5250 41300 27550 6300 9400 27550 24500 20800 4600 16950 43700 21150 43300 28050 10550 38300 41800 28600 7550 15650 22050 47000 41400 -1 15650 15400 12500 28600 17600 41600 12250 24550 -1 25350 24850 17500 22650 9650 21950 10650 12250 3350 7750 19950 30650 750 12750 26500 9400 29500 25650 17950 6850 7850 42000 14150 11650 12650 29350 29400 24250 45700 13550 44050 26250 30700 7350 32250 8400 23300 30650 34650 28900 3000 7350 26550 -1 17350 12400 2300 32050 -1 20400 13850 4100 24600 11400 35600 12600 16900 1300 24450 9750 32650 48950 50 3950 33150