#include using namespace std; constexpr int maxn = 101; int n; char type; bool board[maxn][maxn]; vector> graph[maxn][maxn]; bool valid(int x, int y) { return 0 <= x && x < n && 0 <= y && y < n; } void add(int x, int y, int i, int j) { graph[x][y].push_back({i, j}); } void rook(int x, int y) { for (int i = x + 1; i < n; i++) { if (board[i][y]) { add(i, y, x, y); break; } } for (int i = x - 1; i >= 0; i--) { if (board[i][y]) { add(i, y, x, y); break; } } for (int i = y + 1; i < n; i++) { if (board[x][i]) { add(x, i, x, y); break; } } for (int i = y - 1; i >= 0; i--) { if (board[x][i]) { add(x, i, x, y); break; } } } void knight(int x, int y) { for (auto dx : {-1, 1}) { for (auto dy : {-2, 2}) { if (valid(x + dx, y + dy) && board[x + dx][y + dy]) { add(x + dx, y + dy, x, y); } } } for (auto dy : {-1, 1}) { for (auto dx : {-2, 2}) { if (valid(x + dx, y + dy) && board[x + dx][y + dy]) { add(x + dx, y + dy, x, y); } } } } void bishop(int x, int y) { for (auto fx : {-1, 1}) { for (auto fy : {-1, 1}) { for (int i = 1; valid(x + i * fx, y + i * fy); i++) { if (board[x + i * fx][y + i * fy]) { add(x + i * fx, y + i * fy, x, y); break; } } } } } void king(int x, int y) { for (auto dx : {-1, 0, 1}) { for (auto dy : {-1, 0, 1}) { if (dx == 0 && dy == 0) continue; if (valid(x + dx, y + dy) && board[x + dx][y + dy]) { add(x + dx, y + dy, x, y); } } } } void queen(int x, int y) { bishop(x, y); rook(x, y); } pair from[maxn][maxn]; bool vis[maxn][maxn]; stack> moves; void clear_graph() { moves = stack>(); for (int i = 0; i < n; i++) { fill_n(vis[i], n, false); } } int dfs(int x, int y) { int count = 1; vis[x][y] = true; for (auto p : graph[x][y]) { int a, b; tie(a, b) = p; if (!vis[a][b]) { from[a][b] = {x, y}; moves.push({a, b}); count += dfs(a, b); } } return count; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; cin >> type; int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { char c; cin >> c; board[i][j] = (c != '.'); count += board[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (board[i][j]) { switch (type) { case 'R': rook(i, j); break; case 'Q': queen(i, j); break; case 'B': bishop(i, j); break; case 'N': knight(i, j); break; case 'K': king(i, j); break; default: assert(false); } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (!board[i][j]) continue; clear_graph(); if (dfs(i, j) == count) { cout << "YES\n"; while (!moves.empty()) { auto p = moves.top(); moves.pop(); int x, y; tie(x, y) = p; cout << x + 1 << " " << y + 1 << " " << from[x][y].first + 1 << " " << from[x][y].second + 1 << "\n"; } return 0; } } } cout << "NO\n"; }