#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
long long Bpow(long long a, long long s) {
  if (s == 0) {
    return 1;
  }
  long long u = Bpow(a, s / 2);
  if (s & 1)
    return u * u % mod * a % mod;
  else
    return u * u % mod;
}
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<vector<int>> a(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  if (m == 1) {
    cout << "0";
    return 0;
  }
  vector<vector<int>> maxmod(n, vector<int>(k));
  for (int i = 0; i < n; i++) {
    vector<vector<vector<int>>> dp(
        k, vector<vector<int>>(m / 2 + 1, vector<int>(m, -1)));
    dp[a[i][0] % k][1][0] = a[i][0];
    dp[0][0][0] = 0;
    for (int j = 1; j < m; j++) {
      for (int mod = 0; mod < k; mod++) {
        for (int count = m / 2; count >= 0; count--) {
          if (i == 0 && mod == 1 && j == 3) mod = mod;
          int c = (mod - a[i][j] % k + k) % k;
          dp[mod][count][j] = max({dp[mod][count][j - 1],
                                   (count && dp[c][count - 1][j - 1] != -1
                                        ? dp[c][count - 1][j - 1] + a[i][j]
                                        : -1),
                                   dp[mod][count][j]});
        }
      }
    }
    for (int mod = 0; mod < k; mod++) {
      int ma = -1;
      for (int count = 0; count < m / 2 + 1; count++) {
        ma = max(ma, dp[mod][count][m - 1]);
      }
      maxmod[i][mod] = ma;
    }
  }
  vector<vector<int>> dp(k, vector<int>(n, -1));
  for (int i = 0; i < k; i++) {
    dp[i][0] = maxmod[0][i];
  }
  for (int i = 1; i < n; i++) {
    for (int mod = 0; mod < k; mod++) {
      for (int mod2 = 0; mod2 < k; mod2++) {
        if (maxmod[i][mod2] == -1) continue;
        if (i == 1 && mod == 1) mod = mod;
        int c = (mod - mod2 + k) % k;
        dp[mod][i] =
            max({dp[mod][i - 1],
                 (dp[c][i - 1] != -1 ? dp[c][i - 1] + maxmod[i][mod2] : -1),
                 dp[mod][i]});
      }
    }
  }
  cout << dp[0][n - 1];
}
