#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int a[71][71];
int ans[71][71];
int fans[71];
int lastans[71];
int n, m, k;
int cho[71][71];
void cal(int x, int arr[]) {
  memset(cho, 0, sizeof cho);
  for (int j = 1; j <= m; ++j) {
    int tp = arr[j];
    for (int i = m / 2 - 1; i >= 1; --i) {
      for (int z = 0; z < k; ++z) {
        if (cho[i][z] == 0) continue;
        cho[i + 1][(z + tp) % k] =
            max(cho[i + 1][(z + tp) % k], cho[i][z] + tp);
      }
    }
    cho[1][tp % k] = max(cho[1][tp % k], tp);
  }
  for (int i = 0; i <= m / 2; ++i) {
    for (int j = 0; j < k; ++j) {
      ans[x][j] = max(ans[x][j], cho[i][j]);
    }
  }
}
int main() {
  memset(ans, -inf, sizeof ans);
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf("%d", &a[i][j]);
    }
  }
  for (int i = 1; i <= n; ++i) {
    cal(i, a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    if (i == 1) {
      for (int j = 0; j < k; ++j) {
        fans[j] = ans[1][j];
      }
    } else {
      for (int j = 0; j < k; ++j) lastans[j] = fans[j];
      for (int j = 0; j < k; ++j) {
        for (int z = 0; z < k; ++z) {
          if ((j && ans[i][j] == 0) || (z && lastans[z] == 0)) continue;
          fans[(j + z) % k] = max(fans[(j + z) % k], ans[i][j] + lastans[z]);
        }
      }
    }
  }
  printf("%d\n", fans[0]);
  return 0;
}
