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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <bits/stdc++.h>
using namespace std;

// loops
#define FOR_3(i, a, b) for (int i = (a), bb##i = (b); i <= bb##i; ++i)
#define FORR_3(i, a, b) for (int i = (b), aa##i = (a); i >= aa##i; --i)

#define FOR_2(i, n) FOR_3(i, 0, (n)-1)
#define FORR_2(i, n) FORR_3(i, 0, (n)-1)

#define FOR_1(n) FOR_2(_i##__LINE__, n)
#define FOR_0() for (;;)

#define OVERLOAD(_0, _1, _2, _3, NAME, ...) NAME
#define FOR(...)                                                               \
  OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FOR_3, FOR_2, FOR_1, FOR_0)         \
  (__VA_ARGS__)

#define FORR(...)                                                              \
  OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FORR_3, FORR_2, FORR_1, FOR_0)      \
  (__VA_ARGS__)

// in - expr style
template <class T> T read() {
  T x;
  cin >> x;
  return x;
}
#define II read<int>()
#define ILL read<ll>()
#define ISTR read<string>()

// in - macro style
void _I() {}
template <class ARG, class... ARGS> void _I(ARG &&arg, ARGS &&...args) {
  cin >> arg;
  _I(forward<ARGS>(args)...);
}
#define I(...) _I(__VA_ARGS__);

// out
template <class T> void _print(T x, ostream &os = cout) {
  os << x;
  bool space = true;
  if constexpr (is_same_v<T, char>)
    if (x == '\n')
      space = false;

  if (space)
    os << " ";
}
// template <class T> void _print(const vector<T> &d, ostream &os = cout) {
//   os << '[';
//   for (auto &e : d)
//     os << e << " ";
//   os << ']';
// }
void _O() {}
template <class ARG, class... ARGS> void _O(ARG &&arg, ARGS &&...args) {
  _print(arg);
  _O(forward<ARGS>(args)...);
}
#define O(...) _O(__VA_ARGS__ __VA_OPT__(, ) '\n');
#define OO(...) _O(__VA_ARGS__);

// err
const int _indentSize = 2;
string _indent;

void _E() { cerr << endl; }
template <class ARG, class... ARGS> void _E(ARG &&arg, ARGS &&...args) {
  _print(forward<ARG>(arg), cerr);
  _E(std::forward<ARGS>(args)...);
}

#define ERR(...)                                                               \
  {                                                                            \
    cerr << _indent << "[" << __LINE__ << "] ";                                \
    _E(__VA_ARGS__);                                                           \
  }

#define _V5(x) #x, "==", (x)
#define _V4(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V5(__VA_ARGS__))
#define _V3(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V4(__VA_ARGS__))
#define _V2(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V3(__VA_ARGS__))
#define _V1(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V2(__VA_ARGS__))
#define ERRV(...) ERR(_V1(__VA_ARGS__))
#define INDENT auto _indenter = Indenter();

struct Indenter {
  Indenter() {
    FOR(_indentSize)
    _indent.push_back(' ');
  }
  ~Indenter() {
    FOR(_indentSize)
    _indent.pop_back();
  }
};

#ifdef DEBUG
#define FAIL(str)                                                              \
  {                                                                            \
    cerr << "ASSERT FAILED   " << str << "   " << __FILE__ << ':' << __LINE__  \
         << endl;                                                              \
    abort();                                                                   \
  }
#else
#define FAIL(x)                                                                \
  {}
#endif

// asserts
#define CHECK(cond)                                                            \
  if (!(cond))                                                                 \
  FAIL(#cond)

#define CHECK_OP(op, a, b, res)                                                \
  if (!(res))                                                                  \
  FAIL(#a " " op " " #b "   (" + to_string(a) + " vs " + to_string(b) + ")")

#define CHECK_EQ(a, b) CHECK_OP("==", a, b, (a) == (b))
#define CHECK_NE(a, b) CHECK_OP("!=", a, b, (a) != (b))
#define CHECK_LE(a, b) CHECK_OP("<=", a, b, (a) <= (b))
#define CHECK_GE(a, b) CHECK_OP(">=", a, b, (a) >= (b))
#define CHECK_LT(a, b) CHECK_OP("<", a, b, (a) < (b))
#define CHECK_GT(a, b) CHECK_OP(">", a, b, (a) > (b))

#define CHECK_RANGE(x, a, b)                                                   \
  if (!(a <= x && x < b))                                                      \
  FAIL(#x " in [" #a "," #b ")")

//

#define SZ(v) ((int)(v).size())

using pii = pair<int, int>;

//

template <class T = int> struct Vec : deque<T> {
  int x0 = 0;

  auto &add(const T &el) {
    this->push_back(el);
    return this->back();
  }

  auto &addf(const T &el) {
    this->push_front(el);
    return this->front();
  }

  auto pop() {
    auto r = this->back();
    this->pop_back();
    return r;
  }

  auto popf() {
    auto r = this->front();
    this->pop_front();
    return r;
  }

  auto &operator[](int x) {
    if (x < x0) {
      this->insert(this->begin(), x0 - x, T());
      x0 = x;
    }
    x -= x0;
    if (x >= (int)this->size()) {
      this->insert(this->end(), x - this->size() + 1, T());
    }
    return deque<T>::operator[](x);
  }

  //

  auto &read(int n) {
    FOR(n)
    this->add(::read<T>());
    return *this;
  }
};

template <class T = int> Vec<T> readVec(int n) { return Vec<T>().read(n); }

////////////////////////////////////////////////////////////////////////////////

using ll = long long;

//

int in_n, in_m;

int nums[2009 * 2][26];

vector<string> res;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  in_n = II;
  in_m = II;

  FOR(y, in_n) {
    auto str = ISTR;

    FOR(x, in_m) {
      auto c = str[x] - 'A';
      nums[y][c]++;
      nums[in_n + x][c]++;
    }
  }

  auto curr_n = in_n;
  auto curr_m = in_m;

  FOR(iter, in_n + in_m) {
    int idx = -1;
    int max = -1;
    int best_c = 0;

    FOR(i, in_n + in_m) {
      FOR(c, 26) {
        auto cand = nums[i][c] - (i < in_n ? curr_m : curr_n);

        if (cand > max) {
          max = cand;
          idx = i;
          best_c = c;
        }
      }
    }

    if (idx == -1 || curr_n == 0 || curr_m == 0)
      break;

    nums[idx][best_c] = 0;

    if (idx < in_n) {
      FOR(x, in_m) { nums[in_n + x][best_c] -= 1; }
    } else {
      FOR(y, in_n) { nums[y][best_c] -= 1; }
    }

    if (idx < in_n) {
      curr_n -= 1;
      res.push_back("R " + to_string(idx + 1) + " " + char('A' + best_c));
    } else {
      curr_m -= 1;
      res.push_back("K " + to_string(idx - in_n + 1) + " " +
                    char('A' + best_c));
    }
  }

  O(SZ(res));
  FORR(i, SZ(res)) { O(res[i]); }

  return 0;
}