#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K;
  static long Imos[2010][2010] = {}, Cum[2000][2000] = {}, ans = 0;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'B') {
      x = x % (K * 2);
      y = y % (K * 2);
    } else {
      x = (x + K) % (K * 2);
      y = y % (K * 2);
    }
    if (x < K == y < K) {
      x = x % K;
      y = y % K;
      Imos[0][0]++;
      Imos[0][y]--;
      Imos[x][0]--;
      Imos[x][y]++;
      Imos[x][y]++;
      Imos[x][y + K]--;
      Imos[x + K][y]--;
      Imos[x + K][y + K]++;
      Imos[x + K][0]++;
      Imos[x + K][y]--;
      Imos[K * 2 - 1][0]--;
      Imos[K * 2 - 1][y]++;
      Imos[0][y + K]++;
      Imos[0][K * 2 - 1]--;
      Imos[x][y + K]--;
      Imos[x][K * 2 - 1]++;
      Imos[x + K][y + K]++;
      Imos[x + K][K * 2 - 1]--;
      Imos[K * 2 - 1][y + K]--;
      Imos[K * 2 - 1][y + K]++;
    } else {
      x = x % K;
      y = y % K;
      Imos[x][0]++;
      Imos[x][y]--;
      Imos[x + K][0]--;
      Imos[x + K][y]++;
      Imos[0][y]++;
      Imos[0][y + K]--;
      Imos[x][y]--;
      Imos[x][y + K]++;
      Imos[x + K][y]++;
      Imos[x + K][y + K]--;
      Imos[K * 2 - 1][y]--;
      Imos[K * 2 - 1][y + K]++;
      Imos[x][y + K]++;
      Imos[x][K * 2 - 1]--;
      Imos[x + K][y + K]--;
      Imos[x + K][K * 2 - 1]++;
    }
  }
  Cum[0][0] = Imos[0][0];
  ans = Cum[0][0];
  for (int i = 1; i < K * 2; i++) {
    Cum[0][i] = Cum[0][i - 1] + Imos[0][i];
    Cum[i][0] = Cum[i - 1][0] + Imos[i][0];
    ans = max(ans, Cum[0][i]);
    ans = max(ans, Cum[i][0]);
  }
  for (int i = 1; i < K * 2; i++) {
    for (int j = 1; j < K * 2; j++) {
      Cum[i][j] =
          Cum[i][j - 1] + Cum[i - 1][j] - Cum[i - 1][j - 1] + Imos[i][j];
      ans = max(ans, Cum[i][j]);
    }
  }
  for (int i = 0; i < K * 2; i++) {
    for (int j = 0; j < K * 2; j++) {
      cout << Cum[i][j] << " ";
    }
    cout << endl;
  }
  cout << ans << endl;
}
