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
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
typedef long double ld;
const int maxN = 2005;
char a[maxN][maxN];
int n, m;
int cnt_row[maxN][26];
int cnt_col[maxN][26];
bool is_good_row(int row) {
    return *max_element(cnt_row[row], cnt_row[row] + 26) == accumulate(cnt_row[row], cnt_row[row] + 26, 0);
}
bool is_good_col(int col) {
    return *max_element(cnt_col[col], cnt_col[col] + 26) == accumulate(cnt_col[col], cnt_col[col] + 26, 0);
}
bool used_row[maxN], used_col[maxN];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            cnt_row[i][a[i][j] - 'A'] += 1;
            cnt_col[j][a[i][j] - 'A'] += 1;
        }
    }
    vector<pair<int,char>> ops;
    for (int z = 1; z <= n + m; z++) {
        int who = -1;
        int let = -1;
        for (int p = 1; p <= n; p++) {
            if (!used_row[p] && is_good_row(p)) {
                who = p;
                let = max_element(cnt_row[p], cnt_row[p] + 26) - cnt_row[p];
                break;
            }
        }
        if (who == -1) {
            for (int p = 1; p <= m; p++) {
                if ( !used_col[p] && is_good_col(p)) {
                    who = p + n;
                    let = max_element(cnt_col[p], cnt_col[p] + 26) - cnt_col[p];
                    break;
                }
            }
        }
        assert(who != -1);
        ops.emplace_back(who, (char)(let + 'A'));
        if (who <= n) {
            used_row[who] = true;
            for (int p = 1; p <= m; p++) {
                cnt_col[p][a[who][p] - 'A']--;
            }
        }
        else {
            used_col[who - n] = true;
            for (int p = 1; p <= n; p++) {
                cnt_row[p][a[p][who - n] - 'A']--;
            }
        }
    }
    reverse(ops.begin(), ops.end());
    cout << ops.size() << '\n';
    for (auto& it : ops) {
        if (it.first <= n) cout << "R " << it.first << " " << it.second << '\n';
        else cout << "K " << it.first - n << " " << it.second << '\n';
    }
    return 0;
}