Under construction

Still being built. Things may change

Cat Goose and the Random Matrix

Time limit3sMemory limit1024 MB

Summary
Given a random n by m table with entries modulo p, find the largest-sum submatrix whose sum is divisible by p.
Level

Hard8 of 10

Topics
Matrix, Prefix sum, Hash map, Math
Solved
No attempts yet

Statement

Cat Goose prepared a rectangular table aa of size n×mn \times m for Nick Fury. Each entry is an integer from 00 to p−1p-1.

Nick Fury immediately realized that every number in the table was chosen uniformly at random from 00 to p−1p-1, independently of the others.

Find a rectangular submatrix of the table whose sum is divisible by pp, and among all such submatrices, output the one with the largest sum. Formally, find 1≤i1≤i2≤n1 \leq i_1 \leq i_2 \leq n and 1≤j1≤j2≤m1 \leq j_1 \leq j_2 \leq m such that the sum of ax,ya_{x,y} over i1≤x≤i2i_1 \leq x \leq i_2 and j1≤y≤j2j_1 \leq y \leq j_2 is divisible by pp, and this sum is maximal among all such submatrices.

Input

The first line contains three integers nn, mm, pp (1≤n⋅m,p≤1 000 0001 \leq n \cdot m, p \leq 1\,000\,000): the table dimensions and the number that the submatrix sum must be divisible by.

Each of the next nn lines contains mm integers, where the jj-th number in the ii-th line is ai,ja_{i,j} (0≤ai,j≤p−10 \leq a_{i,j} \leq p - 1).

It is guaranteed that every number in aa was chosen independently and uniformly at random from 00 to p−1p-1.

Output

Print one integer: the maximum sum of a rectangular submatrix whose sum is divisible by pp. If no such submatrix exists, print 00.

Examples1

  1. Example 1

    Input
    6 7 5
    0 0 3 0 1 0 4
    0 2 3 0 2 2 1
    2 4 1 4 4 0 3
    1 1 0 2 0 3 2
    3 0 3 1 0 1 2
    1 2 0 0 3 3 1
    
    Expected output
    65