#include <bits/stdc++.h>
using namespace std;
const int N = 75;
int n, m, p;
int a[N][N], f[N][N / 2][N];
int main() {
  cin >> n >> m >> p;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> a[i][j];
  for (int q = 1; q <= n; q++) {
    for (int k = 0; k < p; k++)
      for (int j = 0; j <= m / 2; j++)
        f[q][0][k] = max(f[q][0][k], f[q - 1][j][k]);
    for (int i = 1; i <= m; i++) {
      for (int j = m / 2; j >= 1; j--)
        for (int k = p - 1; k >= 0; k--)
          if (f[q][j - 1][k]) {
            int t = (k + a[q][i]) % p;
            f[q][j][t] = max(f[q][j][t], f[q][j - 1][k] + a[q][i]);
          }
      f[q][1][a[q][i] % p] = max(f[q][1][a[q][i] % p], a[q][i]);
    }
  }
  int res = 0;
  for (int j = 0; j <= m / 2; j++) res = max(res, f[n][j][0]);
  cout << res << endl;
  return 0;
}
