#include <bits/stdc++.h>
using namespace std;
const long long N = 70 + 2;
long long n, m, x, ar[N][N], dp[2][N][N][N];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> x;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) cin >> ar[i][j];
  for (long long i = 0; i < 2; i++)
    for (long long j = 0; j < N; j++)
      for (long long k = 0; k < N; k++)
        for (long long l = 0; l < N; l++) dp[i][j][k][l] = -(long long)1e9;
  dp[0][0][ar[0][0] % x][1] = ar[0][0];
  dp[0][0][0][0] = 0;
  for (long long j = 1; j < m; j++) {
    dp[0][j][0][0] = 0;
    for (long long k = 0; k < x; k++) {
      dp[0][j][k][0] = max(dp[0][j][k][0], dp[0][j - 1][k][0]);
      for (long long l = 1; l <= m / 2; l++) {
        long long tmp = (((k - ar[0][j]) % x) + x) % x;
        dp[0][j][k][l] = max(dp[0][j][k][l], dp[0][j - 1][k][l]);
        dp[0][j][k][l] =
            max(dp[0][j][k][l], ar[0][j] + dp[0][j - 1][tmp][l - 1]);
      }
    }
  }
  for (long long i = 1; i < n; i++) {
    long long now = (i & 1), prv = (now ^ 1);
    dp[now][0][0][0] = 0;
    for (long long k = 0; k < x; k++)
      for (long long l = 0; l <= m / 2; l++) {
        long long tmp = (((k - ar[i][0]) % x) + x) % x;
        dp[now][0][k][0] = max(dp[now][0][k][0], dp[prv][m - 1][k][l]);
        dp[now][0][k][1] =
            max(dp[now][0][k][1], ar[i][0] + dp[prv][m - 1][tmp][l]);
      }
    for (long long j = 1; j < m; j++) {
      dp[now][j][0][0] = max(dp[now][j][0][0], 0LL);
      for (long long k = 0; k < x; k++) {
        dp[now][j][k][0] = max(dp[now][j][k][0], dp[now][j - 1][k][0]);
        for (long long l = 1; l <= m / 2; l++) {
          long long tmp = (((k - ar[i][j]) % x) + x) % x;
          dp[now][j][k][l] = max(dp[now][j][k][l], dp[now][j - 1][k][l]);
          dp[now][j][k][l] =
              max(dp[now][j][k][l], ar[i][j] + dp[now][j - 1][tmp][l - 1]);
        }
      }
    }
  }
  long long mx = 0;
  for (long long l = 0; l <= m / 2; l++)
    mx = max(mx, dp[(n - 1) & 1][m - 1][0][l]);
  cout << mx << endl;
}
