#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  vector<vector<vector<int>>> dp(
      n, vector<vector<int>>((m / 2) + 1, vector<int>(k, -INF)));
  for (int i = 0; i < n; i++) {
    dp[i][0][0] = 0;
    for (int j = 0; j < m; j++) {
      vector<vector<int>> new_dp = dp[i];
      for (int l = 0; l <= (m / 2); l++) {
        for (int o = 0; o < k; o++) {
          if (l == 0) {
            continue;
          }
          new_dp[l][o] =
              max(new_dp[l][o],
                  dp[i][l - 1][(o + k - (a[i][j] % k)) % k] + a[i][j]);
        }
      }
      swap(dp[i], new_dp);
    }
  }
  vector<vector<int>> mx(n, vector<int>(k, -INF));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= (m / 2); j++) {
      for (int l = 0; l < k; l++) {
        mx[i][l] = max(mx[i][l], dp[i][j][l]);
      }
    }
  }
  vector<int> dp2(k, -INF);
  dp2[0] = 0;
  for (int i = 0; i < n; i++) {
    vector<int> new_dp = dp2;
    for (int j = 0; j < k; j++) {
      for (int l = 0; l < k; l++) {
        new_dp[j] = max(new_dp[j], dp2[(j + k - l) % k] + mx[i][l]);
      }
    }
    swap(dp2, new_dp);
  }
  cout << dp2[0] << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
