A grid

5

1 votes
Introduction to Dynamic Programming-2, Algorithms, Dynamic Programming
Problem

You are given a grid A that consists of N rows and M columns. Each number in the grid is either 0 or 1

Calculate the number of such triples (i,j,h) where for all the pairs (x,y), both x and y belong to [1,h] if y>=x and A[i+x1][j+y1] equals to 1. Of course, the square (i,j,i+h1,j+h1) should be inside of this grid. In other words, calculate the count of square submatrices of the given grid A which have their value equal to 1 for every element present on and above their main diagonal.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each testcase contains two space-separated integers N and M denoting the size of the grid A.
  • The following N lines describe the grid. Each line consists of M characters that are either 0 or 1.

Output format

For each test case, print the count of square submatrices in a new line.

Constraints

1T101N,M3000

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first testcase, there are total 6 submatrices which satisfy the given conditions. These triples (i,j,h) are (1,2,1),(1,2,2),(1,3,1),(2,1,1),(2,2,1) & (2,3,1).

For the second testcase, there are 5 such submatrices.

For the third testcase, there are no such submatrices possible.

Editor Image

?