#include using ll = long long int; #define fr(i, x) for(ll i = 0; i < x; ++i) using namespace std; ll N; char figure; char grid[100][100]; char grid2[100][100]; void add_reepadlo(vector> & to, ll x, ll y){ fr(i, N){ to.push_back({x, i}); to.push_back({i, y}); } } void add_bugger(vector> & to, ll x, ll y){ fr(i, N){ to.push_back({x+i, y+i}); to.push_back({x+i, y-i}); to.push_back({x-i, y+i}); to.push_back({x-i, y-i}); } } vector> get_neighbours(ll x, ll y){ vector> result; switch(figure){ case 'K': result ={ {x+1, y-1}, {x+1, y}, {x+1, y+1}, {x, y-1}, {x, y+1}, {x-1, y-1}, {x-1, y}, {x-1, y+1} }; break; case 'N': result ={ {x+2, y-1}, {x+2, y+1}, {x+1, y+2}, {x+1, y-2}, {x-1, y+2}, {x-1, y-2}, {x-2, y-1}, {x-2, y+1} }; break; case 'R': add_reepadlo(result, x, y); break; case 'B': add_bugger(result, x, y); break; case 'Q': add_reepadlo(result, x, y); add_bugger(result, x, y); break; } vector> result2; for(const pair & p : result){ if(p.first < N && p.first >= 0 && p.second < N && p.second >= 0){ result2.push_back(p); } } return result2; } pair get_neighbour(ll x, ll y){ for(const pair & p: get_neighbours(x, y)){ if(grid[p.first][p.second] != '.'){ return p; } } throw; } vector> get_figures(){ vector> result; fr(i, N)fr(j, N){ if(grid[i][j] != '.'){ result.push_back({i, j}); } } return result; } bool is_connected(){ ll figures = 0; ll x, y; fr(i, N)fr(j, N){ grid2[i][j] = grid[i][j]; if(grid[i][j] != '.'){ x = i; y = j; ++figures; } } queue> q; q.push({x,y}); grid2[x][y] = '.'; --figures; while(!q.empty()){ pair c = q.front(); q.pop(); for(const pair & p : get_neighbours(c.first, c.second)){ if(grid2[p.first][p.second] != '.'){ grid2[p.first][p.second] = '.'; --figures; q.push(p); } } } return figures == 0; } int main(void){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> figure; fr(i, N)fr(j, N){ cin >> grid[i][j]; } if(!is_connected()){ cout << "NO" << endl; return 0; } cout << "YES" << endl; vector> figures = get_figures(); while(figures.size() > 1){ size_t i = 0; for(; i < figures.size(); ++i){ ll x= figures[i].first; ll y= figures[i].second; grid[x][y] = '.'; if(is_connected()){ break; } grid[x][y] = figure; } ll x= figures[i].first; ll y= figures[i].second; cout << x + 1 << " " << y + 1 << " "; figures[i] = figures[figures.size() - 1]; figures.pop_back(); pair neigh = get_neighbour(x, y); cout << neigh.first + 1 << " " << neigh.second + 1 << endl; } return 0; }