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
/**
 * Patryk Kisielewski
 * 
 * Potyczki Algorytmiczne 2024
 * Zadanie: LAM - Łamigłówka 3 [C]
*/
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <utility>
#include <cstdio>
#include <iostream>
#include <sstream>

using namespace std;

int n, m;

vector<map<char,int>> mapK;
vector<map<char,int>> mapR;
stack<string> steps;
queue<pair<char,int>> q;

void removeK(int i) {
    char c = (*(mapK[i].begin())).first;
    steps.push("K " + to_string(i + 1) + " " + c);
    mapK[i].erase(c);
    for (int i = 0; i < n; ++i) {
        auto it = mapR[i].find(c);
        if (it == mapR[i].end()) continue;
        int current = --(*it).second;
        if  (current == 0) {
            mapR[i].erase(it);
        }
        if (mapR[i].size() == 1) {
            q.push({'R', i});
        }
    }
}

void removeR(int i) {
    char c = (*(mapR[i].begin())).first;
    steps.push("R " + to_string(i + 1) + " " + c);
    mapR[i].erase(c);
    for (int i = 0; i < m; ++i) {
        auto it = mapK[i].find(c);
        if (it == mapK[i].end()) continue;
        int current = --(*it).second;
        if  (current == 0) {
            mapK[i].erase(it);
        }
        if (mapK[i].size() == 1) {
            q.push({'K', i});
        }
    }
}

bool run() {
    vector<int> vK;
    vector<int> vR;
    while (!q.empty()) {
        auto& top = q.front();
        q.pop();
        if (top.first == 'K') {
            vK.push_back(top.second);
        } else {
            vR.push_back(top.second);
        }
    }
    for (int i : vK) {
        if (mapK[i].size() != 1) continue;
        removeK(i);
    }
    for (int i : vR) {
        if (mapR[i].size() != 1) continue;
        removeR(i);
    }
    if (vK.size() || vR.size()) return true;
    return false;
}

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

    cin >> n >> m;

    mapK = vector<map<char,int>>(m);
    mapR = vector<map<char,int>>(n);


    char c;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> c;
            ++mapK[j][c];
            ++mapR[i][c];
        }
    }

    for (int i = 0; i < m; ++i) if (mapK[i].size() == 1) q.push({'K', i});
    for (int i = 0; i < n; ++i)  if (mapR[i].size() == 1) q.push({'R', i});

    while (run());

    cout << steps.size() << endl;
    while (!steps.empty()) {
        cout << steps.top() << endl;
        steps.pop();
    }

    return 0;
}