CODECHEF
You are given a binary square matrix
of size . Let the value at cell be denoted by .
Your task is to count the number of square frames present in the grid. A square frame is defined to be a square submatrix of whose border elements are all '1'.
Formally,
- A square submatrix of of size with top-left corner is defined to be the set of cells . Note that this requires and .
- A square frame of size with top-left corner is defined to be a square submatrix of size such that
1
whenever or or or . There is no constraint on the values of elements strictly inside the frame.
Refer to the sample explanation for more details.
Input Format
The first line of input contains a single integer denoting the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer denoting the size of the grid.
Each of the next lines contains a string consisting of characters. The -th such string represents the -th row of . Each character of each string is either 0
or 1
.
Output Format
For each test case, output a single integer denoting the count of square frames in the grid.
Constraints
- The grid is binary, i.e,
0
or1
for every . - Sum of over all test cases does not exceed .
Subtasks
Subtask 1(100 points): Original constraints
Sample Input 1
2
2
10
00
4
1111
1101
1011
1111
Sample Output 1
1
17
Explanation
Test Case : There is square frame, the submatrix containing index .
Test Case : There are square frames of size , of size , and of size .
Some of the square frames are :
- The frame of size containing point .
- The frame of size with corner points .
- The frame of size with corner points .
- The frame of size with corner points .
ConversionConversion EmoticonEmoticon