CF BUDDY
← Problems·

1461B · Find the Spruce

1400 · brute force, dp, implementation

Problem: Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an n×mn \times m matrix consisting of "*" and ".".

To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height kk with origin at point (x,y)(x, y) if:

  • All cells in the set contain an "*".
  • For each 1ik1 \le i \le k all cells with the row number x+i1x+i-1 and columns in range [yi+1,y+i1][y - i + 1, y + i - 1] must be a part of the set. All other cells cannot belong to the set.

Examples of correct and incorrect spruce trees:

Now Rick wants to know how many spruces his n×mn \times m matrix contains. Help Rick solve this problem.

Input Format: Each test contains one or more test cases. The first line contains the number of test cases tt (1t101 \le t \le 10).

The first line of each test case contains two integers nn and mm (1n,m5001 \le n, m \le 500) — matrix size.

Next nn lines of each test case contain mm characters ci,jc_{i, j} — matrix contents. It is guaranteed that ci,jc_{i, j} is either a "." or an "*".

It is guaranteed that the sum of nmn \cdot m over all test cases does not exceed 5002500^2 (nm5002\sum n \cdot m \le 500^2).

Output Format: For each test case, print single integer — the total number of spruces in the matrix.

Note: In the first test case the first spruce of height 22 has its origin at point (1,2)(1, 2), the second spruce of height 11 has its origin at point (1,2)(1, 2), the third spruce of height 11 has its origin at point (2,1)(2, 1), the fourth spruce of height 11 has its origin at point (2,2)(2, 2), the fifth spruce of height 11 has its origin at point (2,3)(2, 3).

In the second test case the first spruce of height 11 has its origin at point (1,2)(1, 2), the second spruce of height 11 has its origin at point (2,1)(2, 1), the third spruce of height 11 has its origin at point (2,2)(2, 2).

Sample Cases

Case 1

Input

4
2 3
.*.
***
2 3
.*.
**.
4 5
.***.
*****
*****
*.*.*
5 7
..*.*..
.*****.
*******
.*****.
..*.*..

Output

5
3
23
34

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback