#include <bits/stdc++.h>
using namespace std;
const int N = 70 + 1, inf = 1e8 + 1;
int n, m, k;
int a[N][N], b[N][N];
int dp[N][N][N][N], dp2[N][N];
int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) cin >> a[i][j];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j)
      for (int c = 0; c <= m / 2; ++c)
        for (int r = 0; r < k; ++r) dp[i][j][c][r] = -inf;
    dp[i][0][0][0] = 0;
    dp[i][0][1][a[i][0] % k] = a[i][0];
    for (int j = 1; j < m; ++j) {
      for (int c = 0; c <= m / 2; ++c)
        for (int r = 0; r < k; ++r) dp[i][j][c][r] = dp[i][j - 1][c][r];
      for (int c = 1; c <= m / 2; ++c)
        for (int r = 0; r < k; ++r)
          dp[i][j][c][r] =
              max(dp[i][j][c][r],
                  dp[i][j - 1][c - 1][(r - a[i][j] % k + k) % k] + a[i][j]);
    }
  }
  for (int i = 0; i < n; ++i)
    for (int r = 0; r < k; ++r) {
      b[i][r] = -inf;
      for (int c = 0; c <= m / 2; ++c)
        b[i][r] = max(b[i][r], dp[i][m - 1][c][r]);
    }
  for (int r = 0; r < k; ++r) dp2[0][r] = b[0][r];
  for (int i = 1; i < n; ++i)
    for (int r = 0; r < k; ++r) {
      dp2[i][r] = dp2[i - 1][r] + b[i][0];
      for (int t = 1; t < k; ++t)
        dp2[i][r] = max(dp2[i][r], dp2[i - 1][(r - t + k) % k] + b[i][t]);
    }
  cout << dp2[n - 1][0] << "\n";
}
