#include <bits/stdc++.h>
const long long LL_INF = (long long)2e18 + 5;
using namespace std;
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  long long f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const long long N = 72, mod = 1e9 + 7;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long k, lim = m / 2;
  cin >> k;
  vector<vector<long long>> v(n, vector<long long>(m));
  for (long long i = (long long)(0); i < (long long)(n); i++)
    for (long long j = (long long)(0); j < (long long)(m); j++) cin >> v[i][j];
  vector<long long> dp(k, -0x3f3f3f3f);
  dp[0] = 0;
  for (long long i = (long long)(0); i < (long long)(n); i++) {
    vector<vector<long long>> row_dp(lim + 1,
                                     vector<long long>(k, -0x3f3f3f3f));
    row_dp[0][0] = 0;
    for (long long j = (long long)(0); j < (long long)(m); j++)
      for (long long use = (long long)(lim - 1); use >= (long long)(0); use--)
        for (long long rem = (long long)(0); rem < (long long)(k); rem++) {
          long long new_rem = (rem + v[i][j]) % k;
          long long new_use = use + 1;
          row_dp[new_use][new_rem] =
              max(row_dp[new_use][new_rem], row_dp[use][rem] + v[i][j]);
        }
    vector<long long> fdp = dp;
    for (long long rem = (long long)(0); rem < (long long)(k); rem++)
      for (long long use = (long long)(0); use < (long long)(lim + 1); use++)
        for (long long remm = (long long)(0); remm < (long long)(k); remm++) {
          long long new_rem = (rem + remm) % k;
          fdp[new_rem] = max(fdp[new_rem], dp[rem] + row_dp[use][remm]);
        }
    dp = fdp;
  }
  cout << dp[0] << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
