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
#include <bits/stdc++.h>

using namespace std;

string board[2020];
vector<tuple<bool, int, char>> ans;

unordered_map<char, int> ctr[7070];
unordered_map<char, int> ctc[7070];

unordered_set<int> rows;
unordered_set<int> cols;

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

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


    for(int i = 0; i < n; i++) {
        cin >> board[i];
    }

    for(int i = 0; i < n; i++) {
        rows.insert(i);
        for(int j = 0; j < m; j++) {
            ctr[i][board[i][j]]++;
            ctc[j][board[i][j]]++;
        }
    }

    for(int i = 0; i < m; i++) {
        cols.insert(i);
    }

    while(rows.size() && cols.size()) {
        vector<int> cols_to_rm;
        for(auto c: cols) {
            char cl = board[*rows.begin()][c];
            if(ctc[c][cl] == rows.size()) {
                ans.push_back({false, c, cl});
                cols_to_rm.push_back(c);

                for(auto r: rows) {
                    ctr[r][cl]--;
                }
            }
        }
        for(auto c: cols_to_rm) {
            cols.erase(c);
        }

        if(rows.size() < 1 ||  cols.size() < 1) {
            break;
        }

        vector<int> rows_to_rm;
        for(auto r: rows) {
            char cl = board[r][*cols.begin()];
            if(ctr[r][cl] == cols.size()) {
                ans.push_back({true, r, cl});
                rows_to_rm.push_back(r);

                for(auto c: cols) {
                    ctc[c][cl]--;
                }
            }
        }
        for(auto r: rows_to_rm) {
            rows.erase(r);
        }
    }

    reverse(ans.begin(), ans.end());
    cout << ans.size() << "\n";
    for(auto a: ans) {
        cout << (get<0>(a) ? "R" : "K") << " " << get<1>(a) + 1 << " " << get<2>(a) << "\n";
    }

    return 0;
}