#include <bits/stdc++.h>
using namespace std;
const int MAX = 7e1 + 5;
int dp[MAX][MAX][MAX][MAX / 2];
int mat[MAX][MAX];
int n, m, k;
int solve(int i, int j, int rem, int tk) {
  if (i == n || j == m) {
    return -1e9;
  }
  if (i == n - 1 && j == m - 1) {
    if (tk < m / 2) {
      if (!((rem + mat[i][j]) % k)) {
        return mat[i][j];
      }
    }
    if (!rem) {
      return 0;
    } else {
      return -1e9;
    }
  }
  int &ans = dp[i][j][rem][tk];
  if (~ans) {
    return ans;
  }
  ans = max(solve(i, j + 1, rem, tk), solve(i + 1, 0, rem, 0));
  if (tk < m / 2) {
    ans = max(ans, max(solve(i, j + 1, (rem + mat[i][j]) % k, tk + 1),
                       solve(i + 1, 0, (rem + mat[i][j]) % k, 0)) +
                       mat[i][j]);
  }
  return ans;
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &mat[i][j]);
    }
  }
  memset(dp, -1, sizeof dp);
  printf("%d", solve(0, 0, 0, 0));
}
