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
114
// LAM: Łamigłówka [C] | 2024-03-14 | Solution by dkgl
// https://sio2.mimuw.edu.pl/c/pa-2024-1/p/lam/

#include <bits/stdc++.h>

using namespace std;

struct ColorCount {
  private:
    string values;
    array<int, 26> count;
    int different_colors = 0;

    inline void reset(int n) {
      values = string(n, ' ');
      for (int i = 0; i < 26; ++i) count[i] = 0;
      different_colors = 0;
    }

  public:
    ColorCount(int n) {
      reset(n);
    };

    void paint(int i, char c) {
      if (count[c-'A']++ == 0) ++different_colors;
      values[i] = c;
    };

    void unpaint(int i) {
      char &c = values[i];
      if (c == ' ') return;
      if (--count[c-'A'] == 0) --different_colors;
      c = ' ';
    }

    inline bool is_single_color() const {
      return different_colors == 1;
    }

    inline char get_single_color() const {
      for (char c = 'A'; c <= 'Z'; ++c) {
        if (count[c-'A'] != 0) return c;
      }
      return ' ';
    }

    inline void clear() {
      reset(values.size());
    }
};

#define Type char
const Type TYPE_ROW = 'R';
const Type TYPE_COLUMN = 'K';

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

  int n, m;
  cin >> n >> m;

  vector<ColorCount> rows(n, ColorCount(m));
  vector<ColorCount> columns(m, ColorCount(n));

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      char color;
      cin >> color;
      rows[i].paint(j, color);
      columns[j].paint(i, color);
    }
  }

  stack<pair<int, Type>> clear_queue;
  for (int i = 0; i < n; ++i) {
    if (rows[i].is_single_color()) {
      clear_queue.push({ i, TYPE_ROW });
    }
  }
  for (int j = 0; j < m; ++j) {
    if (columns[j].is_single_color()) {
      clear_queue.push({ j, TYPE_COLUMN });
    }
  }
  vector<string> result;
  while (!clear_queue.empty()) {
    auto [index, type] = clear_queue.top();
    clear_queue.pop();
    auto &colors = (type == TYPE_ROW ? rows : columns)[index];
    if (!colors.is_single_color()) continue;
    char color = colors.get_single_color();
    colors.clear();
    result.push_back(string() + type + " " + to_string(index + 1) + ' ' + color);
    if (type == TYPE_ROW) {
      for (int j = 0; j < m; ++j) {
        columns[j].unpaint(index);
        if (columns[j].is_single_color()) clear_queue.push({ j, TYPE_COLUMN });
      }
    } else {
      for (int i = 0; i < n; ++i) {
        rows[i].unpaint(index);
        if (rows[i].is_single_color()) clear_queue.push({ i, TYPE_ROW });
      }
    }
  }
  reverse(result.begin(), result.end());
  cout << result.size() << '\n';
  for (auto &row: result) cout << row << '\n';

  cout.flush();
  return 0;
}