#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 9;
const long long MX = 1e5 + 5;
long long n, m, k;
long long inp[75][75];
long long dp[75][75][40];
long long pre[75], f[75], curr[75];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n >> m >> k;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cin >> inp[i][j];
    }
  }
  long long h = m / 2;
  memset(pre, 0, sizeof pre);
  for (long long i = 1; i <= n; i++) {
    memset(dp, -1, sizeof dp);
    for (long long j = 1; j <= m; j++) {
      dp[j][0][0] = 0;
    }
    for (long long j = 1; j <= m; j++) {
      for (long long r = 0; r < k; r++) {
        for (long long x = 0; x <= h; x++) {
          if (dp[j][r][x] == -1) continue;
          if (x != h) {
            long long nr = (r + inp[i][j]) % k;
            dp[j + 1][nr][x + 1] =
                max(dp[j + 1][nr][x + 1], dp[j][r][x] + inp[i][j]);
          }
          dp[j + 1][r][x] = max(dp[j + 1][r][x], dp[j][r][x]);
        }
      }
    }
    for (long long r = 0; r < k; r++) {
      f[r] = 0;
      for (long long x = 0; x <= h; x++) {
        f[r] = max(f[r], dp[m + 1][r][x]);
      }
    }
    memset(curr, 0, sizeof curr);
    for (long long r1 = 0; r1 < k; r1++) {
      for (long long r2 = 0; r2 < k; r2++) {
        long long r = (pre[r1] + f[r2]) % k;
        curr[r] = max(curr[r], pre[r1] + f[r2]);
      }
    }
    swap(pre, curr);
  }
  cout << pre[0] << endl;
  return 0;
}
