#include <bits/stdc++.h>
using namespace std;
int n, m, k;
const int MAXS = 71;
vector<vector<int>> ma(MAXS, vector<int>(MAXS));
int dp[MAXS][MAXS][MAXS][37];
int f(int r, int c, int mod, int rem) {
  if (c == m) return f(r + 1, 0, mod, m / 2);
  if (r == n)
    if (mod == 0)
      return 0;
    else
      return -2;
  if (dp[r][c][mod][rem] != -1) return dp[r][c][mod][rem];
  int take, ntake;
  take = ntake = -2;
  if (rem > 0) {
    take = f(r, c + 1, (mod + ma[r][c]) % k, rem - 1);
  }
  ntake = f(r, c + 1, mod, rem);
  if (take != -2) take += ma[r][c];
  int ans = max(take, ntake);
  return dp[r][c][mod][rem] = ans;
}
void solve() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> ma[i][j];
  memset(dp, -1, sizeof dp);
  cout << f(0, 0, 0, m / 2) << '\n';
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  int t;
  t = 1;
  while (t--) solve();
  return 0;
}
