#include <bits/stdc++.h>
using namespace std;
const long long int LINF = 1e18, mod = 1e9 + 7;
void Bl0ck_M0mb0();
signed main() {
  Bl0ck_M0mb0();
  long long int t = 1;
  while (t--) {
    long long int n, m, k;
    cin >> n >> m >> k;
    long long int ip[n + 1][m + 1];
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 1; j <= m; j++) cin >> ip[i][j];
    }
    long long int DP[n + 1][m + 1][m + 1][k];
    memset(DP, -1, sizeof(DP));
    ;
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 0; j <= m / 2; j++) DP[i][0][j][0] = 0;
      for (long long int j = 1; j <= m; j++) {
        DP[i][j][0][0] = 0;
        long long int rem = ip[i][j] % k;
        for (long long int l = 1; l <= m / 2; l++) {
          for (long long int o = 0; o <= k - 1; o++) {
            long long int req = (k - rem + o) % k;
            if (DP[i][j - 1][l - 1][req] != -1)
              DP[i][j][l][o] =
                  max(DP[i][j][l][o], DP[i][j - 1][l - 1][req] + ip[i][j]);
            if (DP[i][j - 1][l][o] != -1)
              DP[i][j][l][o] = max(DP[i][j][l][o], DP[i][j - 1][l][o]);
          }
        }
      }
    }
    long long int OP[n + 1][k];
    long long int val[n + 1][k];
    memset(val, -1, sizeof(val));
    ;
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 0; j <= k - 1; j++) {
        for (long long int l = 0; l <= m / 2; l++) {
          for (long long int a = 0; a <= m; a++) {
            if (DP[i][a][l][j] != -1)
              val[i][j] = max(DP[i][a][l][j], val[i][j]);
          }
        }
      }
    }
    memset(OP, -1, sizeof(OP));
    ;
    OP[0][0] = 0;
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 0; j <= k - 1; j++) {
        for (long long int l = 0; l <= k - 1; l++) {
          long long int req = (k - l + j) % k;
          if (OP[i - 1][req] != -1 && val[i][l] != -1)
            OP[i][j] = max(OP[i][j], OP[i - 1][req] + val[i][l]);
          OP[i][j] = max(OP[i][j], OP[i - 1][j]);
        }
      }
    }
    cout << OP[n][0] << endl;
  }
  return 0;
}
void Bl0ck_M0mb0() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
