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


hide comments
sdnr1: 2016-03-03 14:23:54

The following test case is mentioned in an earlier comment. Why is it said to be an invalid test case
8 15
..******
*.......
*.......
*.......
*.......
*.......
*.......
*.......
*.......
*.......
*.#####.
....***.
.......*
...####*
...****.
Moreover my solution got accepted even though it give 15 as the answer on this test case while the correct answer seems to be 22.

Last edit: 2016-03-03 20:44:15
rjgames: 2016-02-23 05:14:56

4 4
..*#
..#.
....
....
ans = 0
This test case should be added. I could get AC even though my algo gave 1 as answer.

vijay77794: 2016-01-11 06:10:17

atlast done....... :0

ihak: 2015-07-28 14:16:34

Test Case Incoming:
1
4 4
....
....
.*.*
....

.This turned my solution from O(N^3) to O(N^4) , hence out of the time limit !!

Fabián Gómez González: 2014-01-06 02:28:18

For those who get WA in this problem, you should consider the next testcase:
Input:
1
5 4
.....
####.
****.
****.
Output:
0

Ghost Of Perdition: 2013-05-20 08:09:39

add this testcase..
4 4
*#..
#***
****
****
and a lot of solutions will get WA :)

Edit: statement says that you have a walkable path between the start and the destination, so your test case is wrong

Last edit: 2013-07-14 20:45:37
যোবায়ের: 2011-11-04 06:17:31

Two more cases:
2
1
6 6
*.....
*####.
*####.
*####.
*####.
*####.
8 15
..******
*.......
*.......
*.......
*.......
*.......
*.......
*.......
*.......
*.......
*.#####.
....***.
.......*
...####*
...****.
Output should be:
1
22

Edit: problem says that all paths are H+W-2 this cases does not meet that requierment

Last edit: 2013-07-14 22:43:21

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