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

#PS0000236. Đếm hình chữ nhật có tổng chia hết M (Count Submatrices Divisible by M)

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 chia hết M (Count Submatrices Divisible by M)

Count Submatrices Divisible by M

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer matrix of size n×mn\times m and a positive integer MM, count nonempty axis-aligned subrectangles whose sum is divisible by MM, including zero and negative multiples. Different boundaries define distinct rectangles.

Input

The first line contains n,m,Mn,m,M, followed by nn rows of mm integers.

Output

Print the number of valid rectangles.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤151\le n,m\le 15; ∣ai,j∣≤106|a_{i,j}|\le 10^6; 1≤M≤1091\le M\le10^9.
  • Subtask 2 (30 points): 1≤n,m≤1501\le n,m\le 150; ∣ai,j∣≤106|a_{i,j}|\le 10^6; 1≤M≤1091\le M\le10^9; M=1M=1.
  • Subtask 3 (50 points): 1≤n,m≤1501\le n,m\le 150; ∣ai,j∣≤106|a_{i,j}|\le 10^6; 1≤M≤1091\le M\le10^9.

Example 1

Input

2 2 3
1 2
3 4

Output

3

Explanation

The three valid rectangles are the cell 3, the top row (sum 3), and the right column (sum 6).

Example 2

Input

1 3 3
-3 1 2

Output

3

Explanation

The valid segments are [-3], [1,2], and the full row (sum zero).