#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  long long n, m, k;
  cin >> n >> m >> k;
  long long a[n][m];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) cin >> a[i][j];
  }
  if (m == 1) {
    cout << 0;
    return 0;
  }
  long long dp[n][k];
  for (long long i = 0; i < n; i++) {
    long long sum[m][k][m / 2 + 1];
    for (long long j = 0; j < m; j++) {
      for (long long l = 0; l < k; l++) {
        for (long long p = 0; p < m / 2 + 1; p++) sum[j][l][p] = INT_MIN;
      }
    }
    for (long long i = 0; i < m; i++) sum[i][0][0] = 0;
    sum[0][a[i][0] % k][1] = a[i][0];
    for (long long j = 0; j < k; j++) {
      for (long long l = 1; l < m / 2 + 1; l++)
        sum[0][j][l] = max(sum[0][j][l], sum[0][j][l - 1]);
    }
    for (long long j = 1; j < m; j++) {
      long long z = a[i][j];
      for (long long l = 0; l < k; l++) {
        for (long long p = 1; p < m / 2 + 1; p++) {
          sum[j][l][p] = max(sum[j][l][p], sum[j - 1][l][p - 1]);
          sum[j][l][p] = max(sum[j][l][p], sum[j - 1][l][p]);
          sum[j][(l + z) % k][p] =
              max(sum[j][(l + z) % k][p], sum[j - 1][l][p - 1] + z);
        }
      }
    }
    for (long long j = 0; j < k; j++) dp[i][j] = sum[m - 1][j][m / 2];
  }
  long long f[n][k];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < k; j++) f[i][j] = INT_MIN;
  }
  for (long long i = 0; i < k; i++) f[0][i] = dp[0][i];
  for (long long i = 1; i < n; i++) {
    for (long long j = 0; j < k; j++) {
      for (long long l = 0; l < k; l++) {
        f[i][l] = max(f[i][l], f[i - 1][l]);
        f[i][(l + j) % k] = max(f[i][(l + j) % k], f[i - 1][l] + dp[i][j]);
      }
    }
  }
  cout << f[n - 1][0];
}
