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

char tab[2007][2007];

map<char, int> col[2007];
map<char, int> row[2007];

queue<pair<pair<char, int>, char>> ready;
stack<pair<pair<char, int>, char>> done;

void print_tab(int n, int m) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << tab[i][j];
        }
        cout << "\n";
    }
}

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

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char c;
            cin >> c;
            tab[i][j] = c;
            row[i][c]++;
            col[j][c]++;
        }
    }

    for (int i = 1; i <= n; i++) {
        // cout << "row " << i << " size " << row[i].size() << "\n";
        if (row[i].size() == 1) {
            ready.push({{'R', i}, row[i].begin()->first});
            // cout << "row " << i << " ready\n";

        }
    }
    for (int j = 1; j <= m; j++) {
        // cout << "col " << j << " size " << col[j].size() << "\n";
        if (col[j].size() == 1) {
            ready.push({{'K', j}, col[j].begin()->first});
            // cout << "column " << j << " ready\n";
        }
    }

    // return 0;

    while (!ready.empty()) {
        auto [op, k] = ready.front().first;

        // cout << "delete " << ready.front().first.first << " " << ready.front().first.second << " " << ready.front().second << "\n";

        done.push(ready.front());
        ready.pop();

        if (op == 'R') {
            for (int j = 1; j <= m; j++) {
                char c = tab[k][j];
                if (c != '0') {
                    col[j][c]--;
                    if (col[j][c] == 0) {
                        col[j].erase(c);
                        if (col[j].size() == 1) {
                            // cout << "column " << j << " ready\n";
                            ready.push({{'K', j}, col[j].begin()->first});
                        }   
                    }
                }
                tab[k][j] = '0';
            }
        }
        else {
            for (int i = 1; i <= n; i++) {
                char c = tab[i][k];
                if (c != '0') {
                    row[i][c]--;
                    if (row[i][c] == 0) {
                        row[i].erase(c);
                        if (row[i].size() == 1) {
                            // cout << "row " << i << " ready\n";
                            ready.push({{'R', i}, row[i].begin()->first});
                        }
                    }
                }
                tab[i][k] = '0';
            }
        }

        // print_tab(n, m);
    }

    cout << done.size() << "\n";
    while (!done.empty()) { 
        cout << done.top().first.first << " " << done.top().first.second << " " << done.top().second << "\n";
        done.pop();
    }
}