#include <bits/stdc++.h>
using namespace std;
int N, M, K;
int A[80][80];
int B[80][75];
int dp[80][80];
int mp[75 * 75];
void pre(int x) {
  memset(mp, -1, sizeof(mp));
  mp[0] = 0;
  int lm = M / 2;
  for (int i = 0; i < M; i++) {
    int v = A[x][i];
    for (int j = M * 70; j >= 0; j--) {
      if (mp[j] != -1) {
        if (mp[j + v] == -1) {
          mp[j + v] = mp[j] + 1;
        } else {
          mp[j + v] = min(mp[j + v], mp[j] + 1);
        }
      }
    }
  }
  for (int i = M * 70; i >= 0; i--) {
    if (mp[i] == -1 || mp[i] > lm) continue;
    if (B[x][i % K] == -1) B[x][i % K] = i;
  }
}
int main() {
  scanf("%d %d %d", &N, &M, &K);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      scanf("%d", &A[i][j]);
    }
  }
  memset(B, -1, sizeof(B));
  for (int i = 0; i < N; i++) {
    pre(i);
  }
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < K; i++) {
    dp[0][i] = B[0][i];
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < K; j++) {
      if (B[i][j] == -1) continue;
      for (int ki = 0; ki < K; ki++) {
        if (dp[i - 1][ki] == -1) continue;
        dp[i][(ki + j) % K] = max(dp[i][(ki + j) % K], dp[i - 1][ki] + B[i][j]);
      }
    }
  }
  printf("%d\n", dp[N - 1][0]);
  return 0;
}
