Given a grid of size n \times m, where each cell in the grid contains either 0 or 1. Write a program to count the number of Z-shaped figures consisting entirely of 1's that appear in the grid.
An Z-shaped figure consists of:
Two horizontal lines consisting entirely of 1's, containing at least two 1's and of the same length. These two lines are located in two different rows in the grid.
A diagonal line consisting entirely of 1's connecting the rightmost cell of the upper horizontal line to the leftmost cell of the lower horizontal line. This diagonal line forms a 45-degree angle with the two horizontal lines.
The following are some valid Z-shaped figures:
The following are some invalid Z-shaped figures:
A single line containing a single integer, which is the number of Z-shaped figures in the given grid.
Note that this value may be larger than the limit of the int type in C++ or longint type in Pascal.
Dữ liệu vào Sao chép |
3 3 011 011 000 |
Dữ liệu ra Sao chép |
1 |
Dữ liệu vào Sao chép |
3 3 011 011 011 |
Dữ liệu ra Sao chép |
2 |
Dữ liệu vào Sao chép |
3 3 111 111 111 |
Dữ liệu ra Sao chép |
6 |
The following are 6 Z-shaped figures that appear in the 3^th example: