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

constexpr int MAXN = 2000;

using namespace std;

int ok(array<int, 26> &cnts) {
    int nz = 0;
    for (int i = 0; i < 26; i++) if (cnts[i]) {
        if (nz) return 0;
        nz = i + 1;
    }
    return nz;
}

void solve() {
    int h, w;
    cin >> h >> w;
    array<array<int, 26>, MAXN> rcnt, ccnt;
    for (auto &a : rcnt) fill(a.begin(), a.end(), 0);
    for (auto &a : ccnt) fill(a.begin(), a.end(), 0);
    string s;
    for (int r = 0; r < h; r++) {
        cin >> s;
        for (int c = 0; c < w; c++) {
            int ix = s[c] - 'A';
            rcnt[r][ix]++;
            ccnt[c][ix]++;
        }
    }
    queue<array<int, 2>> rs, cs;
    for (int r = 0; r < h; r++) {
        int ix = ok(rcnt[r]);
        if (!ix) continue;
        ix--;
        rs.push({r, ix});
    }
    for (int c = 0; c < w; c++) {
        int ix = ok(ccnt[c]);
        if (!ix) continue;
        ix--;
        cs.push({c, ix});
    }
    vector<array<int, 3>> res;
    while (rs.size() || cs.size()) {
        if (rs.size()) {
            auto [r, ix] = rs.front();
            rs.pop();
            rcnt[r][ix] = 0;
            res.push_back({'R', r + 1, 'A' + ix});
            for (int c = 0; c < w; c++) {
                if (ccnt[c][ix]) if (!--ccnt[c][ix]) if (int nix; (nix = ok(ccnt[c]))) {
                    cs.push({c, nix - 1});
                }
            }
        } else {
            auto [c, ix] = cs.front();
            cs.pop();
            ccnt[c][ix] = 0;
            res.push_back({'K', c + 1, 'A' + ix});
            for (int r = 0; r < h; r++) {
                if (rcnt[r][ix]) if (!--rcnt[r][ix]) if (int nix; (nix = ok(rcnt[r]))) {
                    rs.push({r, nix - 1});
                }
            }
        }
    }
    reverse(res.begin(), res.end());
    cout << res.size() << '\n';
    for (auto &e : res) {
        cout << char(e[0]) << " " << e[1] << " " << char(e[2]) << '\n';
    }
}

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

// #ifdef LOCAL
//     int t; cin >> t; while (t--)
// #endif

    solve();

    cout.flush();
}