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
165
166
167
168
169
170
171
172
//
// Mateusz Pietrowcow
//
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
 
#define MOD 1000000007
#define INF 1000000000
#define INFL 1000000000000000000LL
#define Tak cout << "TAK" << '\n'
#define Nie cout << "NIE" << '\n'
#define min3(x, y, z) min(x, min(y, z))
#define max3(x, y, z) max(x, max(y, z))
 
using namespace std;
 
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<long long, long long> pll;
typedef pair<unsigned long long, unsigned long long> pull;

struct info
{
    bool isW;
    int pos, znak;
};

const int N = 2e3 + 5;

char mapa[N][N];
set<int> zw[N], zk[N];
int w[N][26], k[N][26];

int n, m;

void read()
{
    cin >> n >> m;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            char a;
            cin >> a;
            mapa[i][j] = a;

            w[i][a - 'A']++;
            k[j][a - 'A']++;

            zw[i].insert(a - 'A');
            zk[j].insert(a - 'A');
        }
    }
}
 
stack<info> r;

void solve()
{
    queue<info> q;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j < 26; j++)
            if (w[i][j] == 0)
                w[i][j] = -1;
    }

    for (int i = 1; i <= m; i++)
    {
        for (int j = 0; j < 26; j++)
            if (k[i][j] == 0)
                k[i][j] = -1;
    }

    for (int i = 1; i <= n; i++)
    {
        if (zw[i].size() == 1)
        {
            q.push({true, i, *zw[i].begin()});
            zw[i].clear();
        }
    }

    for (int i = 1; i <= m; i++)
    {
        if (zk[i].size() == 1)
        {
            q.push({false, i, *zk[i].begin()});
            zk[i].clear();
        }  
    }

    while (!q.empty())
    {
        auto acc = q.front();
        q.pop();

        r.push(acc);

        if (acc.isW)
        {
            for (int i = 1; i <= m; i++)
            {
                if (mapa[acc.pos][i] != 0)
                {
                    k[i][acc.znak]--;
                    mapa[acc.pos][i] = 0;

                    if (k[i][acc.znak] == 0)
                    {
                        zk[i].erase(acc.znak);
                        k[i][acc.znak]--;

                        if (zk[i].size() == 1)
                        {
                            q.push({false, i, *zk[i].begin()});
                            zk[i].clear();
                        }
                    }
                }
            }
        }
        else
        {
            for (int i = 1; i <= n; i++)
            {
                if (mapa[i][acc.pos] != 0)
                {
                    w[i][acc.znak]--;
                    mapa[i][acc.pos] = 0;
                
                    if (w[i][acc.znak] == 0)
                    {
                        zw[i].erase(acc.znak);
                        w[i][acc.znak]--;

                        if (zw[i].size() == 1)
                        {
                            q.push({true, i, *zw[i].begin()});
                            zw[i].clear();
                        }
                    }
                }
            }
        }
    }

    cout << r.size() << '\n';

    while (!r.empty())
    {
        auto acc = r.top();
        r.pop();

        cout << (acc.isW ? 'R' : 'K') << ' ' << acc.pos << ' ' << char(acc.znak + 'A') << '\n';
    }
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
 
    read();
    solve();
}