Counting Cross Shapes in a Grid
InterviewTime limit1sMemory limit1024 MB
Count the cells of an N by M binary grid that center a cross of size K, meaning all cells K steps up, down, left, and right are 1.
- Level
Easy3 of 10
- Topics
- Matrix, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Ku colors cells of an grid in any pattern. While coloring, Ku wondered how many crosses of a given size appear in the grid.
For a cell , a cross of size exists at that cell if the cells from to and the cells from to are all colored in a row.
The crosses of sizes , , and are shown in the figure below. Count the crosses of size .

Input
The first line contains the grid size and , separated by a space.
The second line contains the cross size .
From the third line onward, the next lines give the coloring of the grid, with values separated by spaces. A value of 1 means the cell is colored, and 0 means it is not.
Output
Print the number of crosses of size .