#include <bits/stdc++.h>
using namespace std;
const int N = 71;
int a[N][N], dp1[N][N][N], dp2[N][N];
int main() {
  int n, m, mm, kk;
  scanf("%d%d%d", &n, &m, &kk);
  mm = m >> 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) scanf("%d", &a[i][j]);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      for (int j2 = min(mm, j) - 1; j2 >= 0; --j2) {
        for (int k = 0; k < kk; ++k) {
          if (dp1[i][j2][k] % kk != k) continue;
          dp1[i][j2 + 1][(k + a[i][j]) % kk] =
              max(dp1[i][j2 + 1][(k + a[i][j]) % kk], dp1[i][j2][k] + a[i][j]);
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int k = 0; k < kk; ++k) {
      int mx = 0;
      for (int j2 = 0; j2 <= mm; ++j2) {
        if (dp1[i][j2][k] % kk != k) continue;
        mx = max(mx, dp1[i][j2][k]);
      }
      if (mx % kk != k) continue;
      for (int k2 = 0; k2 < kk; ++k2) {
        if (dp2[i - 1][k2] % kk != k2) continue;
        dp2[i][(k2 + k) % kk] = max(dp2[i][(k2 + k) % kk], dp2[i - 1][k2] + mx);
      }
    }
  }
  printf("%d\n", dp2[n][0]);
  return 0;
}
