#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long res = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') res = res * 10 + ch - 48, ch = getchar();
  return res * f;
}
const long long maxn = 75;
long long f[maxn][maxn][maxn][maxn];
long long a[maxn][maxn];
long long n, m, k;
signed main() {
  n = read();
  m = read();
  k = read();
  for (register long long i = 1; i <= n; i++) {
    for (register long long j = 1; j <= m; j++) {
      a[i][j] = read();
    }
  }
  memset(f, -1, sizeof f);
  f[0][0][0][0] = 0;
  f[1][0][0][0] = 0;
  f[1][1][0][0] = 0;
  for (register long long i = 0; i <= n; i++) {
    for (register long long j = 0; j <= m; j++) {
      for (register long long c = 0; c <= m / 2; c++) {
        for (register long long r = 0; r < k; r++) {
          if (f[i][j][c][r] == -1) continue;
          long long ni = (j == m ? i + 1 : i);
          long long nj = (j == m ? 1 : j + 1);
          if (i != ni) {
            f[ni][nj][0][r] = max(f[ni][nj][0][r], f[i][j][c][r]);
          } else {
            f[ni][nj][c][r] = max(f[ni][nj][c][r], f[i][j][c][r]);
          }
          if (c < m / 2) {
            long long t = (r + a[i][j]) % k;
            if (i != ni)
              f[ni][nj][0][t] = max(f[ni][nj][0][t], f[i][j][c][r] + a[i][j]);
            else
              f[ni][nj][c + 1][t] =
                  max(f[ni][nj][c + 1][t], f[i][j][c][r] + a[i][j]);
          }
        }
      }
    }
  }
  cout << max(0ll, f[n + 1][1][0][0]);
  return 0;
}
