#include <bits/stdc++.h>
using namespace std;
const int maxn = 77;
int saveori[maxn][maxn];
int dp[maxn][maxn][maxn][maxn];
int save[maxn][maxn];
int n, m, k;
void solve(int hang) {
  int mxc = m / 2;
  dp[hang][0][0][0] = 0;
  save[hang][0] = 0;
  for (int i = 1; i <= m; i++) {
    int nowval = saveori[hang][i];
    dp[hang][i][0][0] = 0;
    for (int chose = 1; chose <= mxc; chose++) {
      for (int nowmod = 0; nowmod < k; nowmod++) {
        for (int prepos = 0; prepos < i; prepos++) {
          int preval =
              dp[hang][prepos][chose - 1][(nowmod - (nowval % k) + k) % k];
          if (preval == -1) continue;
          dp[hang][i][chose][nowmod] =
              max(dp[hang][i][chose][nowmod], preval + nowval);
          save[hang][nowmod] =
              max(save[hang][nowmod], dp[hang][i][chose][nowmod]);
        }
      }
    }
  }
}
int DP[maxn][maxn];
int main() {
  memset(dp, -1, sizeof(dp));
  memset(save, -1, sizeof(save));
  memset(DP, -1, sizeof(DP));
  cin >> n >> m >> k;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &saveori[i][j]);
    }
    solve(i);
  }
  DP[0][0] = 0;
  for (int hang = 1; hang <= n; hang++) {
    for (int totmod = 0; totmod < k; totmod++) {
      for (int nowmod = 0; nowmod < k; nowmod++) {
        if (save[hang][nowmod] == -1) continue;
        if (DP[hang - 1][(totmod - nowmod + k) % k] == -1) {
          continue;
        }
        DP[hang][totmod] =
            max(DP[hang][totmod],
                DP[hang - 1][(totmod - nowmod + k) % k] + save[hang][nowmod]);
      }
    }
  }
  cout << DP[n][0] << endl;
}
