#include <bits/stdc++.h>
using namespace std;
int a[100][100], dp[75][75][75][75];
int n, m, k;
int solve(int i, int j, int c, int r) {
  if (i == 0 || j == 0) {
    if (r == 0) return 0;
    return -10000000;
  }
  int &ret = dp[i][j][c][r];
  if (ret != -1) return ret;
  if (j == 1) {
    int x = solve(i - 1, m, 0, r);
    int y = -10000000;
    if (c + 1 <= m / 2) {
      y = solve(i - 1, m, 0, ((r - a[i][j]) % k + k) % k) + a[i][j];
    }
    ret = max(x, y);
  } else {
    int x = solve(i, j - 1, c, r);
    int y = -10000000;
    if (c + 1 <= m / 2) {
      y = solve(i, j - 1, c + 1, ((r - a[i][j]) % k + k) % k) + a[i][j];
    }
    ret = max(x, y);
  }
  return ret;
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) cin >> a[i][j];
  }
  memset(dp, -1, sizeof dp);
  cout << solve(n, m, 0, 0) << endl;
  return 0;
}
