#include <bits/stdc++.h>
using namespace std;
int n, m, k;
const int N = 75;
int dp[N][N][N][N];
int dpc[N][N], dps[N][N];
int a[N][N];
int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  while (cin >> n >> m >> k) {
    for (int i = (1); i < (n + 1); i++)
      for (int j = (1); j < (m + 1); j++) cin >> a[i][j];
    memset(dp, -1, sizeof(dp));
    memset(dpc, -1, sizeof(dpc));
    for (int i = (1); i < (n + 1); i++) {
      dp[i][0][0][0] = 0;
      for (int j = (1); j < (m + 1); j++) {
        for (int cnt = (0); cnt < (m / 2 + 1); cnt++)
          for (int res = (0); res < (k); res++) {
            dp[i][j][cnt][res] = dp[i][j - 1][cnt][res];
            int tt = res - a[i][j] % k;
            if (tt < 0) tt += k;
            if (cnt != 0 && dp[i][j - 1][cnt - 1][tt] != -1)
              dp[i][j][cnt][res] =
                  max(dp[i][j][cnt][res], dp[i][j - 1][cnt - 1][tt] + a[i][j]);
          }
      }
      for (int res = (0); res < (k); res++) {
        for (int cnt = (0); cnt < (m / 2 + 1); cnt++)
          dpc[i][res] = max(dpc[i][res], dp[i][m][cnt][res]);
      }
    }
    memset(dps, -1, sizeof(dps));
    dps[0][0] = 0;
    for (int i = (1); i < (n + 1); i++) {
      for (int j = (0); j < (k); j++)
        for (int res = (0); res < (k); res++) {
          int tt = j - res;
          if (tt < 0) tt += k;
          if (dps[i - 1][res] != -1 && dpc[i][tt] != -1)
            dps[i][j] = max(dps[i][j], dps[i - 1][res] + dpc[i][tt]);
        }
    }
    cout << dps[n][0] << '\n';
  }
  return 0;
}
