#include using namespace std; const int N = 105; #define mp make_pair int n; char c; char S[N][N]; bool odw[N][N]; int dis[N][N]; vector > print_order[N * N]; struct ruch { pair from, to; }; ruch slowa[N][N]; bool correct_field(pair pole) { return 0 <= pole.first && pole.first < n && 0 <= pole.second && pole.second < n && !odw[pole.first][pole.second] && S[pole.first][pole.second] == c; } void handle_adding(pair pol1, pair pol2) { odw[pol2.first][pol2.second] = true; dis[pol2.first][pol2.second] = dis[pol1.first][pol1.second] + 1; ruch new_ruch; new_ruch.from = pol2; new_ruch.to = pol1; slowa[pol2.first][pol2.second] = new_ruch; if (S[pol2.first][pol2.second] == c) { print_order[dis[pol2.first][pol2.second]].push_back(pol2); } } void bfs(pair start) { odw[start.first][start.second] = true; queue > order; order.push(start); while (!order.empty()) { pair x = order.front(); order.pop(); if (c == 'B') { for (int i = 1; i <= n; i++) { pair new_pole = mp(x.first + i, x.second + i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } new_pole = mp(x.first + i, x.second - i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } new_pole = mp(x.first - i, x.second + i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } new_pole = mp(x.first - i, x.second - i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } } } if (c == 'N') { for (auto v : {mp(1, 1), mp(1, -1), mp(-1, 1), mp(-1, -1)}) { for (auto w : {mp(1, 2), mp(2, 1)}) { pair new_pole = mp(x.first + w.first * v.first, x.second + w.second * v.second); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } } } } if (c == 'R') { for (auto v : {mp(0, 1), mp(1, 0), mp(0, -1), mp(-1, 0)}) { for (int i = 1; i <= n; i++) { pair new_pole = make_pair(x.first + v.first * i, x.second + v.second * i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } } } } if (c == 'K') { for (int v : {-1, 0, 1}) { for (int w : {-1, 0, 1}) { pair new_pole = make_pair(x.first + v, x.second + w); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } } } } if (c == 'Q') { for (auto v : {mp(0, 1), mp(1, 0), mp(0, -1), mp(-1, 0)}) { for (int i = 1; i <= n; i++) { pair new_pole = make_pair(x.first + v.first * i, x.second + v.second * i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } } } for (int i = 1; i <= n; i++) { pair new_pole = mp(x.first + i, x.second + i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } new_pole = mp(x.first + i, x.second - i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } new_pole = mp(x.first - i, x.second + i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } new_pole = mp(x.first - i, x.second - i); if (correct_field(new_pole)) { handle_adding(x, new_pole); order.push(new_pole); } } } } } bool initial_check(vector > &pos) { if (n == 1) { printf("YES\n"); return true; } if (pos.size() == 1) { printf("YES\n"); return true; } // if (c == 'R') { // return false; // } // if (c == 'Q') { // return false; // } // if (c == 'B') { // int curr = (pos.front().first + pos.front().second) % 2; // for (auto v : pos) { // if ((v.first + v.second) % 2 != curr) { // printf("NO\n"); // return true; // } // } // } // if (c == 'N') { // if (n == 2) { // printf("NO\n"); // return true; // } // if (n == 3 && S[1][1] == c) { // printf("NO\n"); // return true; // } // } // if (c == 'K') { // return false; // } return false; } int main() { scanf("%d %c", &n, &c); vector > pos; for (int i = 0; i < n; i++) { scanf("%s", &S[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (S[i][j] == c) { pos.push_back(make_pair(i, j)); } } } if (initial_check(pos)) { return 0; } bfs(pos.front()); for (auto v : pos) { if (!odw[v.first][v.second]) { printf("NO\n"); return 0; } } printf("YES\n"); for (int i = n * n; i > 0; i--) { for (auto v : print_order[i]) { printf("%d %d %d %d\n", slowa[v.first][v.second].from.first + 1, slowa[v.first][v.second].from.second + 1, slowa[v.first][v.second].to.first + 1, slowa[v.first][v.second].to.second + 1); } } }