#include <bits/stdc++.h>
using namespace std;
int a[100][100], f[100][100][100], c[100][100], g[100][100];
int main() {
  int n, m, k, mx = 0;
  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 = 0; i <= n; i++) {
    for (int j = 0; j <= k; j++) c[i][j] = -1e9;
  }
  int t = m / 2;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      for (int x = 0; x <= t; x++) {
        for (int y = 0; y < k; y++) {
          f[j][x][y] = -1e9;
        }
      }
    }
    f[0][0][0] = 0;
    for (int j = 1; j <= m; j++) {
      f[j][0][0] = 0;
      for (int x = t; x >= 1; x--) {
        for (int y = 0; y < k; y++) {
          f[j][x][y] = max(f[j][x][y], f[j - 1][x][y]);
          int b = y - a[i][j];
          b %= k;
          if (b < 0) b += k;
          f[j][x][y] = max(f[j][x][y], f[j - 1][x - 1][b] + a[i][j]);
        }
      }
    }
    for (int j = 0; j < k; j++) {
      for (int x = 0; x <= t; x++) {
        c[i][j] = max(c[i][j], f[m][x][j]);
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= k; j++) g[i][j] = -1e9;
  }
  g[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      for (int x = 0; x < k; x++) {
        int b = j - x;
        if (b < 0) b += k;
        g[i][j] = max(g[i][j], g[i - 1][x] + c[i][b]);
      }
    }
  }
  printf("%d\n", g[n][0]);
  return 0;
}
