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

using namespace std;

int n, m, kolumny[2010][2012], wiersze[2010][2012], ilew[2*2010], ilewwierszu[2010];
string s;
char plansza[2010][2010];
set<int> bylo;
vector<int> odp, kolory;

int main(){
    cin >> n >> m;
    for(int i=0;i<n;i++){
        cin >> s;
        for(int j=0;j<m;j++){
            plansza[i][j] = s[j];
            kolumny[j][(int)(s[j]-'A')]++;
            wiersze[i][(int)(s[j]-'A')]++;
            if(kolumny[j][(int)(s[j]-'A')] == 1)ilew[j]++;
            if(wiersze[i][(int)(s[j]-'A')] == 1)ilew[i+2010]++;
        }
    }
    vector<int> kolejka;
    int it=0;
    for(int i=0;i<n;i++){
        //cout << ilew[i+2010] <<endl;
        if(ilew[i+2010]==1)kolejka.push_back(i+2010);
    }
    //cout << "fr" << endl;
    for(int i=0;i<m;i++){
        //cout << ilew[i] << endl;
        if(ilew[i]==1)kolejka.push_back(i);
    }
    //cout << "kon" << endl;
    while(it<kolejka.size()){
        int j;
        if(!bylo.count(kolejka[it])){
        //cout << kolejka[it] << endl;
        bylo.insert(kolejka[it]);
        if(kolejka[it]>2009){
            j=kolejka[it]-2010;
            int h=0;
            while(!wiersze[j][h] && h<2010){h++;};
            kolory.push_back(h);
            odp.push_back(kolejka[it]);
            for(int i=0;i<m;i++){
                kolumny[i][(int)(plansza[j][i]-'A')]--;
                wiersze[j][(int)(plansza[j][i]-'A')]--;
                if(kolumny[i][(int)(plansza[j][i]-'A')] == 0)ilew[i]--;
                if(wiersze[j][(int)(plansza[j][i]-'A')] == 0)ilew[j+2010]--;
                if(ilew[i]==1){
                    kolejka.push_back(i);
                    //odp.push_back(i);
                    ilew[i]=10000;
                }
            }
        }else{

            j=kolejka[it];
            int h=0;
            while(!kolumny[j][h] && h<2010){h++;};
            kolory.push_back(h);
            odp.push_back(kolejka[it]);
            for(int i=0;i<n;i++){
                kolumny[j][(int)(plansza[i][j]-'A')]--;
                wiersze[i][(int)(plansza[i][j]-'A')]--;
                if(kolumny[j][(int)(plansza[i][j]-'A')] == 0)ilew[j]--;
                if(wiersze[i][(int)(plansza[i][j]-'A')] == 0)ilew[i+2010]--;
                if(ilew[i+2010]==1){
                    kolejka.push_back(i+2010);
                    ilew[i+2010]=10000;
                    //odp.push_back(i+2010);
                }
            }
        }}
        it++;
    }
    reverse(odp.begin(), odp.end());
    reverse(kolory.begin(), kolory.end());
    /*for(int i:odp)cout << i << " ";
    cout << endl;
    for(int i:kolory)cout << i << " ";*/
    int ile=0;
    for(int i=0;i<kolory.size();i++)if(kolory[i]!=2010)ile++;
    cout << ile << endl;
    for(int i=0;i<odp.size();i++){
        //cout << odp[i] << " " << kolory[i] << endl;
        if(odp[i]>2009){
            if(kolory[i]!=2010)cout << "R " << odp[i]-2010+1 << " " << (char)(kolory[i]+'A') << endl;
        }else{
            if(kolory[i]!=2010)cout << "K " << odp[i]+1 << " " << (char)(kolory[i]+'A') << endl;
        }
    }

}