PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000215. Đếm hình chữ nhật có tổng bằng K (Number of Submatrices That Sum to Target)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm hình chữ nhật có tổng bằng K (Number of Submatrices That Sum to Target)

Number of Submatrices That Sum to Target

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer matrix aa with mm rows and nn columns, count nonempty rectangular submatrices with cell sum exactly KK. Each rectangle contains all cells from row r1r_1 to r2r_2 and column c1c_1 to c2c_2, inclusive. Rectangles with different boundary coordinates count separately, even if their sums coincide.

Input

The first line contains three integers m,n,Km,n,K. Each of the next mm lines contains exactly nn integers ai,1,…,ai,na_{i,1},\ldots,a_{i,n}.

Output

Print the number of valid rectangles.

Subtasks

  • Subtask 1 (20 points): 1≤m,n≤81\le m,n\le 8; −1000≤ai,j≤1000-1000\le a_{i,j}\le 1000; −100000000≤K≤100000000-100000000\le K\le 100000000.
  • Subtask 2 (30 points): 1≤m,n≤1001\le m,n\le 100; −1000≤ai,j≤1000-1000\le a_{i,j}\le 1000; −100000000≤K≤100000000-100000000\le K\le 100000000; m=1m=1.
  • Subtask 3 (50 points): 1≤m,n≤1001\le m,n\le 100; −1000≤ai,j≤1000-1000\le a_{i,j}\le 1000; −100000000≤K≤100000000-100000000\le K\le 100000000.

Example 1

Input

3 3 0
0 1 0
1 1 1
0 1 0

Output

4

Explanation

Only the four zero-valued corner cells are valid rectangles.

Example 2

Input

2 2 0
1 -1
-1 1

Output

5

Explanation

Both rows, both columns, and the whole matrix sum to 0.

Example 3

Input

1 1 0
904

Output

0

Explanation

The sole cell is 904, not 0.