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
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <tuple>

const int A = 26;

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

    int N, M;
    std::cin >> N >> M;

    std::vector<std::string> board(N);
    std::vector<std::vector<int>> colors_row(N, std::vector<int>(A, 0)), colors_col(M, std::vector<int>(A, 0));
    std::vector<int> cnt_row(N, 0), cnt_col(M, 0);
    for (int i = 0; i < N; i++) {
        std::cin >> board[i];
        for (int j = 0; j < M; j++) {
            auto update = [&](std::vector<int>& cnt, std::vector<int>& colors, int id, char C) {
                if (colors[C - 'A'] == 0) {
                    cnt[id]++;
                }
                colors[C - 'A']++;
            };
            update(cnt_row, colors_row[i], i, board[i][j]);
            update(cnt_col, colors_col[j], j, board[i][j]);
        }
    }

    std::vector<bool> visited_row(N, false), visited_col(M, false);
    std::vector<std::tuple<int, int, char>> result;
    std::queue<std::tuple<int, int, char>> Q;

    for(int i = 0; i < N; i++) {
        if (cnt_row[i] <= 1) {
            visited_row[i] = true;
            Q.push({0, i, board[i][0]});
        }
    }
    if(Q.empty()) {
        for(int i = 0; i < M; i++) {
            if (cnt_col[i] <= 1) {
                visited_col[i] = true;
                Q.push({1, i, board[0][i]});
            }
        }
    }
    
    auto print = [&]() {
        for (auto& row : board) {
            std::cout << row << "\n";
        }
        std::cout << "\n";
    };

    auto find_color = [&](std::vector<int>& colors) {
        for (int i = 0; i < A; i++) {
            if (colors[i] > 0) {
                return (char)('A' + i);
            }
        }
        return 'A';
    };

    //print();
    while (!Q.empty()) {
        auto [type, id, col] = Q.front();
        Q.pop();
        result.push_back({type, id, col});

        if (type == 0) { // row
            for (int i = 0; i < M; i++) {
                char C = board[id][i];
                if(C == '#') {
                    continue;
                }
                board[id][i] = '#';
                colors_col[i][C - 'A']--;
                if (colors_col[i][C - 'A'] == 0) {
                    cnt_col[i]--;
                }
                if(cnt_col[i] <= 1 && !visited_col[i]) {
                    visited_col[i] = true;
                    Q.push({1, i, find_color(colors_col[i])});
                }
            }
        } else { // col
            for (int i = 0; i < N; i++) {
                char C = board[i][id];
                if(C == '#') {
                    continue;
                }
                board[i][id] = '#';
                colors_row[i][C - 'A']--;
                if (colors_row[i][C - 'A'] == 0) {
                    cnt_row[i]--;
                }
                if(cnt_row[i] <= 1 && !visited_row[i]) {
                    visited_row[i] = true;
                    Q.push({0, i, find_color(colors_row[i])});
                }
            }
        }

        //std::cout << "type: " << type << " id: " << id << " col: " << col << "\n";
        //print();
    }

    std::reverse(result.begin(), result.end());
    std::cout << result.size() << "\n";
    for (auto [type, id, col] : result) {
        std::cout << (type == 0 ? 'R' : 'K') << ' ' << id + 1 << ' ' << col << '\n';
    }

    return 0;
}