#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int inf = 2e9;
const long long mod = 1e9 + 7;
const int N = 70 + 11;
const long long INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);
int n, dp[N][N][N][N], m, k, a[N][N];
void solve() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      for (int cnt = 0; cnt <= m / 2; cnt++) {
        for (int r = 0; r < k; r++) {
          dp[i][j][cnt][r] = -inf;
        }
      }
    }
  }
  dp[0][m][0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int cnt = 0; cnt <= m / 2; cnt++) {
      for (int r = 0; r < k; r++) {
        dp[i][0][0][r] = max(dp[i][0][0][r], dp[i - 1][m][cnt][r]);
      }
    }
    for (int j = 1; j <= m; j++) {
      for (int cnt = 0; cnt <= m / 2; cnt++) {
        for (int r = 0; r < k; r++) {
          dp[i][j][cnt][r] = max(dp[i][j][cnt][r], dp[i][j - 1][cnt][r]);
          if (cnt)
            dp[i][j][cnt][r] = max(
                dp[i][j][cnt][r],
                dp[i][j - 1][cnt - 1][(k + r - (a[i][j] % k)) % k] + a[i][j]);
        }
      }
    }
  }
  int ans = 0;
  for (int cnt = 0; cnt <= m / 2; cnt++) {
    ans = max(ans, dp[n][m][cnt][0]);
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  while (T--) {
    solve();
  }
}
