Frames

4.5

4 votes
Approved, Combinatorics, Data Structures, Fenwick Tree, Fenwick tree, Hard, Open, Trees
Problem

You are given a NxM matrix consisting of 0s and 1s. Find the number of square frames with no 1s (each cell is 0) in this matrix. A frame of a fixed square is a group of cells each of them is one of the leftmost, rightmost, topmost or bottommost cells. Thus a square frame with the side length 1 contains 1 cell, with the side length 2 contains 4, with the side length 3 - 8 cells, with the side length 4 - 12 cells.

Input
The first line contains two integers N and M.
Next N lines contain M characters each. Every character is either 1 or 0.

Output
Output one integer - the number of square frames consisting only of 0s.

Constraints

  • 1N, M2000
  • 1N, M10 in 40% of the test data
  • 1N, M100 in 70% of the test data
Time Limit: 2
Memory Limit: 256
Source Limit:
Editor Image

?