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
#include <bits/stdc++.h>
using namespace std;
int n, m;
char tab[2005][2005];
int row[2005];
int col[2005];
int row_l[2005][30];
int col_l[2005][30];
stack <pair <char, pair <int, char>>> res;
queue <pair <bool, int>> q;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            cin >> tab[i][j];
            ++row_l[i][tab[i][j] - 'A'];
            if (row_l[i][tab[i][j] - 'A'] == 1)
            {
                ++row[i];
            }
            ++col_l[j][tab[i][j] - 'A'];
            if (col_l[j][tab[i][j] - 'A'] == 1)
            {
                ++col[j];
            }
        }
    }
    for (int i = 1; i <= n; ++i)
    {
        if (row[i] == 1)
        {
            q.push({true, i});
        }
    }
    for (int i = 1; i <= m; ++i)
    {
        if (col[i] == 1)
        {
            q.push({false, i});
        }
    }
    while (!q.empty())
    {
        char c;
        auto top = q.front();
        q.pop();
        if (top.first)
        {
            for (int i = 1; i <= m; ++i)
            {
                if (tab[top.second][i] != '.')
                {
                    c = tab[top.second][i];
                    tab[top.second][i] = '.';
                    --col_l[i][c - 'A'];
                    if (col_l[i][c - 'A'] == 0)
                    {
                        --col[i];
                        if (col[i] == 1)
                        {
                            q.push({false, i});
                        }
                    }
                }
            }
        }
        else
        {
            for (int i = 1; i <= n; ++i)
            {
                if (tab[i][top.second] != '.')
                {
                    c = tab[i][top.second];
                    tab[i][top.second] = '.';
                    --row_l[i][c - 'A'];
                    if (row_l[i][c - 'A'] == 0)
                    {
                        --row[i];
                        if (row[i] == 1)
                        {
                            q.push({true, i});
                        }
                    }
                }
            }
        }
        pair <char, pair <int, char>> t;
        if (top.first) t.first = 'R';
        else t.first = 'K';
        t.second = {top.second, c};
        res.push(t);
    }
    cout << res.size();
    while (!res.empty())
    {
        cout << "\n" << res.top().first << " " << res.top().second.first << " " << res.top().second.second;
        res.pop();
    }
}