#include <bits/stdc++.h>
using namespace std;
long long mod = ((long)1e9 + 7);
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int dp[n][m / 2 + 1][k];
  vector<vector<int>> arr(n, vector<int>(m, 0));
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) cin >> arr[i][j];
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m / 2 + 1; j++)
      for (long long l = 0; l < k; l++) {
        dp[i][j][l] = -1;
      }
  for (long long i = 0; i < n; i++) {
    if (i == 0) {
      dp[i][0][0] = 0;
    } else {
      for (long long x = 0; x < k; x++) {
        dp[i][0][x] = dp[i - 1][m / 2][x];
      }
    }
    for (long long j = 0; j < m; j++) {
      for (int p = m / 2 - 1; p >= 0; p--) {
        for (long long q = 0; q < k; q++) {
          if (dp[i][p][q] != -1) {
            dp[i][p + 1][q] = max(dp[i][p][q], dp[i][p + 1][q]);
            dp[i][p + 1][(q + arr[i][j]) % k] =
                max(dp[i][p + 1][(q + arr[i][j]) % k], dp[i][p][q] + arr[i][j]);
          }
        }
      }
    }
  }
  cout << dp[n - 1][m / 2][0] << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  long long t = 1;
  int x = 1;
  while (t--) {
    solve();
    x++;
  }
  return 0;
}
