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
#include <iostream>
#include <queue>
#include <vector>
#include <string>
using namespace std;

int mat[2003][2003], row[2003][30], col[2003][30], b[2][2003];
queue < pair < int, char > > q0, q1;
vector < pair < string, int > > v;

int main() {
    ios_base::sync_with_stdio(false);
    cout.tie(NULL);
    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            char c;
            cin >> c;
            mat[i][j] = c - 'A';
            row[i][c - 'A']++;
            if (row[i][c - 'A'] == m) {
                q0.push({i, c});
                b[1][i] = 1;
            }
            col[j][c - 'A']++;
            if (col[j][c - 'A'] == n) {
                q1.push({j, c});
                b[0][j] = 1;
            }
        }
    }

    while (!q0.empty() || !q1.empty()) {
        if (!q0.empty()) {
            int a = q0.front().first;
            char c = q0.front().second;
            string c1; c1 += c;
            q0.pop();
            v.push_back({"R" + c1, a}); 
            for (int i = 0; i < m; ++i) {
                for (char ch = 'A'; ch <= 'Z'; ++ch) {
                    if (mat[a][i] == ch - 'A') continue;
                    ++col[i][ch - 'A'];
                    if (col[i][ch - 'A'] == n && !b[0][i]) {
                        q1.push({i, ch});
                        b[0][i] = 1;
                    }
                }
            }
        } else {
            int a = q1.front().first;
            char c = q1.front().second;
            string c1; c1 += c;
            q1.pop();
            v.push_back({"K" + c1, a}); 
            for (int i = 0; i < n; ++i) {
                for (char ch = 'A'; ch <= 'Z'; ++ch) { 
                    if (mat[i][a] == ch - 'A') continue;
                    ++row[i][ch - 'A'];
                    if (row[i][ch - 'A'] == m && !b[1][i]) {
                        q0.push({i, ch});
                        b[1][i] = 1;
                    }
                }
            }
        }
    }
    cout << v.size() << endl;
    for (int i = v.size() - 1; i > -1; --i) {
        cout << v[i].first[0] << ' ' << v[i].second + 1 << ' ' << v[i].first[1] << endl;
    }
    return 0;
}