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
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

class Info
{
    public:
    LL count[31];
    bool active;
    Info() { active=true; FOR(i,0,30) count[i]=0; }
    LL colorInAmount(LL am) { FOR(i,0,30) if (count[i]==am) return i; return -1; }
};

class Move
{
    public:
    bool horiz;
    LL num;
    LL color;
};


LL n; // number of rows
LL m; // number of columns
Info rows[2024];
Info cols[2024];
Move moves[10000];
LL mc;

void readData()
{
    cin >> n; cin >> m;
    string s;
    FOR(i,1,n)
    {
        cin >> s;
        FOR(j,1,m)
        {
            char ch = s[j-1];
            LL c = (LL)ch - 65;
            rows[i].count[c]++;
            cols[j].count[c]++;
        }
    }
}

void prepareMoves()
{
    mc=0;
    LL rc=n; LL cc=m;
    LL color;
    while (rc*cc>0)
    {
        //cout << "rc=" << rc << " cc=" << cc << "\n"; cout.flush();
        if (cc>0)
        {
            FOR(i,1,n) if (rows[i].active)
            {
                color = rows[i].colorInAmount(cc);
                if (color != -1)
                {
                    rows[i].active=false;
                    rc--;
                    mc++;
                    moves[mc].color = color;
                    moves[mc].horiz = true;
                    moves[mc].num = i;
                    FOR(j,1,m) if (cols[j].active) cols[j].count[color]--;
                    if (rc==0) return;
                }
            }
            FOR(j,1,m) if (cols[j].active)
            {
                color = cols[j].colorInAmount(rc);
                if (color != -1)
                {
                    cols[j].active=false;
                    cc--;
                    mc++;
                    moves[mc].color = color;
                    moves[mc].horiz = false;
                    moves[mc].num = j;
                    FOR(i,1,n) if (rows[i].active) rows[i].count[color]--;
                    if (cc==0) return;
                }
            }
        }
    }
}

void showMoves()
{
    cout << mc << "\n";
    FORD(i,mc,1)
    {
        if (moves[i].horiz) cout << "R "; else cout << "K ";
        cout << moves[i].num << " " << char(moves[i].color+65) << "\n";
    }
}

/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);
	readData();
	prepareMoves();
	showMoves();
	return 0;
}