1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <bits/stdc++.h>
#include <iterator>

using namespace std;

int n, m;

struct cumsum {
  int idx, col[26];
};

const int UNKNOWN = -1;

bool is_transposed = false;
void transpose(vector<cumsum> &rows, vector<cumsum> &cols) {
  swap(rows, cols);
  swap(n, m);
  is_transposed = !is_transposed;
}

struct op {
  char dir, color;
  int num;
};

stack<op> ops;

void add_op(int num, char color) {
  ops.push({is_transposed ? 'K' : 'R', static_cast<char>(color + 'A'), num});
}

vector<pair<int, int>> find_mono_rows(vector<cumsum> &rows) {
  vector<pair<int, int>> res;
  for (int i = 0; i < n; ++i) {
    int col = UNKNOWN;
    bool is_mono = true;
    for (int c = 0; c < 26; ++c) {
      if (rows[i].col[c] > 0) {
        if (col != UNKNOWN) {
          is_mono = false;
          break;
        }
        col = c;
      }
    }
    if (is_mono && col != UNKNOWN)
      res.push_back({i, col});
  }
  return res;
}

void unpaint_rows(const vector<pair<int, int>> &mono,
                  const vector<cumsum> &rows, vector<cumsum> &cols) {
  for (auto [row, col] : mono) {
    add_op(rows[row].idx, col);
    for (int j = 0; j < m; ++j)
      --cols[j].col[col];
  }
}

void remove_rows(const vector<pair<int, int>> &mono, vector<cumsum> &rows) {
  vector<cumsum> res;
  int j = 0;
  for (int i = 0; i < n; ++i) {
    if (j < mono.size() && i == mono[j].first)
      ++j;
    else
      res.push_back(rows[i]);
  }
  rows = res;
  n = rows.size();
}

void undo(vector<cumsum> &rows, vector<cumsum> &cols) {
  if (rows.empty() || cols.empty())
    return;
  const vector<pair<int, int>> mono = find_mono_rows(rows);
  unpaint_rows(mono, rows, cols);
  remove_rows(mono, rows);
  transpose(rows, cols);
  return undo(rows, cols);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> m;

  vector<string> board(n);
  for (string &row : board)
    cin >> row;

  vector<cumsum> rows(n), cols(m);
  for (int i = 0; i < n; ++i)
    rows[i].idx = i;
  for (int j = 0; j < m; ++j)
    cols[j].idx = j;

  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      ++rows[i].col[board[i][j] - 'A'];
      ++cols[j].col[board[i][j] - 'A'];
    }

  undo(rows, cols);
  cout << ops.size() << '\n';
  while (!ops.empty()) {
    auto [dir, col, num] = ops.top();
    ops.pop();
    cout << dir << ' ' << num + 1 << ' ' << col << '\n';
  }
}