#include <bits/stdc++.h>
using namespace std;
void printMat(vector<vector<long long> > mat) {
  cout << "--------------------" << '\n';
  for (auto vec : mat) {
    for (auto v : vec) {
      cout << v << " ";
    }
    cout << '\n';
  }
  cout << "--------------------" << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, m, k;
  cin >> n >> m >> k;
  if (m < 2) {
    cout << 0 << '\n';
    return 0;
  }
  long long qtt = m / 2;
  vector<vector<long long> > mat(n, vector<long long>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> mat[i][j];
    }
    sort(mat[i].rbegin(), mat[i].rend());
  }
  vector<vector<long long> > temp;
  vector<vector<long long> > ans;
  for (int p = 0; p < n; p++) {
    temp.clear();
    temp.resize(qtt, vector<long long>(k));
    for (auto v : mat[p]) {
      for (int i = qtt - 2; i >= 0; i--) {
        for (int j = 0; j < k; j++) {
          if (temp[i][j] == 0) {
            continue;
          }
          temp[i + 1][(j + v) % k] =
              max(temp[i + 1][(j + v) % k], temp[i][j] + v);
        }
      }
      temp[0][v % k] = max(temp[0][v % k], v);
      for (int i = 1; i < qtt; i++) {
        for (int j = 0; j < k; j++) {
          temp[i][j] = max(temp[i][j], temp[i - 1][j]);
        }
      }
    }
    ans.push_back(temp.back());
  }
  vector<vector<long long> > dp(n, vector<long long>(k));
  dp = ans;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      if (ans[i][j] == 0) continue;
      for (int p = 0; p < k; p++) {
        if (dp[i - 1][p] == 0) continue;
        dp[i][(j + p) % k] = max(dp[i][(j + p) % k], ans[i][j] + dp[i - 1][p]);
      }
    }
    for (int j = 0; j < k; j++) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
    }
  }
  cout << dp[n - 1][0] << '\n';
  return 0;
}
