#include <bits/stdc++.h>
using namespace std;
const int maxn = 120;
const int mod = 998244353;
int a[maxn][maxn], dp[maxn][maxn], tmp1[maxn], tmp2[maxn];
int main() {
  int n, m, k, i, j, l;
  scanf("%d %d %d", &n, &m, &k);
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) scanf("%d", &a[i][j]);
  memset(tmp1, -1, sizeof(tmp1));
  tmp1[0] = 0;
  for (i = 0; i < n; i++) {
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for (j = 0; j < m; j++) {
      for (l = j; l >= 0; l--)
        for (int z = 0; z < k; z++)
          if (dp[l][z] != -1)
            dp[l + 1][(z + a[i][j]) % k] =
                max(dp[l + 1][(z + a[i][j]) % k], dp[l][z] + a[i][j]);
    }
    for (j = 1; j < m; j++)
      for (l = 0; l < k; l++) dp[j][l] = max(dp[j][l], dp[j - 1][l]);
    memset(tmp2, -1, sizeof(tmp2));
    for (j = 0; j < k; j++)
      for (l = 0; l < k; l++)
        if (tmp1[j] != -1 && dp[m / 2][l] != -1)
          tmp2[(j + l) % k] = max(tmp2[(j + l) % k], tmp1[j] + dp[m / 2][l]);
    for (j = 0; j < k; j++) tmp1[j] = max(tmp1[j], tmp2[j]);
  }
  printf("%d\n", tmp1[0]);
  return 0;
}
