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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long

const int INF = 1e18;

void solve(int _id) {
    int n, m;

    cin >> n >> m;
    vector<string> arr(n);

    for (auto &s : arr)
        cin >> s;

    vector< vector<int> > cols(m, vector<int>(26)), rows(n, vector<int>(26));
    vector<int> col_rozne(m), row_rozne(n);

    for (int i = 0; i < n; i++) {
        for (char c : arr[i]) {
            if (rows[i][c - 'A'] == 0)
                row_rozne[i]++;
            rows[i][c - 'A']++;
        }
    }

    for (int j = 0; j < m; j++) {
        for (int i = 0; i < n; i++) {
            if (cols[j][ arr[i][j] - 'A' ] == 0)
                col_rozne[j]++;

            cols[j][ arr[i][j] - 'A']++;
        }
    }

    vector< tuple<char, int, char> > res;
    vector< pair<char, int> > queue;

    for (int i = 0; i < n; i++) {
        if (row_rozne[i] == 1) {
            queue.emplace_back('R', i);

            int ktore = -1;
            for (int x = 0; x < 26; x++)
                if (rows[i][x])
                    ktore = x;

            res.emplace_back('R', i, 'A' + ktore);
            row_rozne[i] = 0;
        }
    }
    for (int j = 0; j < m; j++) {
        if (col_rozne[j] == 1) {
            queue.emplace_back('K', j);

            int ktore = -1;
            for (int x = 0; x < 26; x++)
                if (cols[j][x]) {
                    ktore = x;
                }

            res.emplace_back('K', j, 'A' + ktore);
            col_rozne[j] = 0;
        }
    }

    while(queue.size()) {
        auto [dim, id] = queue.back();
        queue.pop_back();

        if (dim == 'R') {
            for (int j = 0; j < m; j++) {
                if (arr[id][j] != '.') {
                    if (col_rozne[j] == 0) {
                        continue;
                    }

                    int znak = arr[id][j] - 'A';
                    cols[j][ znak ]--;
                    if (cols[j][znak] == 0) {
                        col_rozne[j]--;
                    }

                    if (col_rozne[j] == 1) {
                        queue.emplace_back('K', j);

                        int ktore = -1;
                        for (int x = 0; x < 26; x++)
                            if (cols[j][x]) {
                                ktore = x;
                            }

                        res.emplace_back('K', j, 'A' + ktore);
                        col_rozne[j] = 0;
                    }
                }
            }
        } else {
            for (int i = 0; i < n; i++) {
                if (arr[i][id] != '.') {
                    if (row_rozne[i] == 0) {
                        continue;
                    }

                    int znak = arr[i][id] - 'A';
                    rows[i][ znak ]--;
                    if (rows[i][znak] == 0) {
                        row_rozne[i]--;
                    }

                    if (row_rozne[i] == 1) {
                        queue.emplace_back('R', i);

                        int ktore = -1;
                        for (int x = 0; x < 26; x++)
                            if (rows[i][x])
                                ktore = x;

                        res.emplace_back('R', i, 'A' + ktore);
                        row_rozne[i] = 0;
                    }
                }
            }
        }
    }

    reverse(res.begin(), res.end());

    cout << res.size() << "\n";
    for (auto [a, b, c] : res) {
        cout << a << " " << b + 1 << " " << c << "\n";
    }
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
//    freopen("../d/1.in","r",stdin);
//    freopen("../wzo.out","w",stdout);

//    cin >> t;

    for (int i = 1; i <= t; i++) {
        solve(i);
    }

    return 0;
}
/*
3 2 6
1 2 1
2 2 2
1 1 5

 */