TOURIST - Tourist

A lazy tourist wants to visit as many interesting locations in a city as possible without going one step further than necessary. Starting from his hotel, located in the north-west corner of city, he intends to take a walk to the south-east corner of the city and then walk back. When walking to the south-east corner, he will only walk east or south, and when walking back to the north-west corner, he will only walk north or west. After studying the city map he realizes that the task is not so simple because some areas are blocked. Therefore he has kindly asked you to write a program to solve his problem.

Given the city map (a 2D grid) where the interesting locations and blocked areas are marked, determine the maximum number of interesting locations he can visit. Locations visited twice are only counted once.

Input

The first line in the input contains the number of test cases (at most 20). Then follow the cases. Each case starts with a line containing two integers, W and H (2 ≤ W , H ≤ 100), the width and the height of the city map. Then follow H lines, each containing a string with W characters with the following meaning:

. Walkable area
* Interesting location (also walkable area)
# Blocked area

You may assume that the upper-left corner (start and end point) and lower-right corner (turning point) are walkable, and that a walkable path of length H + W − 2 exists between them.

Output

For each test case, output a line containing a single integer: the maximum number of interesting locations the lazy tourist can visit.

Example

Input:
2
9 7
*........
.....**#.
..**...#*
..####*#.
.*.#*.*#.
...#**...
*........
5 5
.*.*.
*###.
*.*.*
.###*
.*.*.

Output:
7
8

Added by:Daniel Gómez Didier
Date:2008-11-18
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:2007 PUJ - Circuito de Maratones ACIS / REDIS

hide comments
2024-11-06 15:37:31
you can try taking two tourist starting from top left and get max for all posiible combination as second tourist mimic the coming back tourist
2022-02-03 13:08:01
Take number of column first & then no .of rows. i.e for first test case 9 is number of column and 7 is number of rows.

If you take number of column 7 and number of rows 9 in input and enter the same 2d vector you will get output as 10 & 8 as per hetp111 query

Wasted almost 3hr in the silly mistake :(


Last edit: 2022-02-03 13:12:28
2020-04-21 11:36:10
This question is such a beauty... Worth spending time on
2019-11-19 20:18:01
getting 10,8 for example tc.. idk why.
2018-06-23 01:07:15 shubham
A very interesting problem, kudos to the setter.
2018-06-14 07:45:29
use dp[x1][y1][x2] and dp==-1 => unmarked, dp==-INF => blocked
2018-02-02 12:15:22
very good question:)
2017-09-29 11:22:40
Nice problem.Learnt something new.
2017-03-04 08:59:36
Can anyone explain how the answer to following test case could be 4????
Please really need help

1
3 6
..*
**#
...
.*.
..#
.*.

Last edit: 2017-03-04 09:00:06
2016-10-04 02:33:08
Very interesting problem,huge upgrade on the problem in which you just have to go from the top-left to the bottom-right corner.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.