#include #define ll long long #define mp make_pair #define pb push_back #define pii pair #define pll pair #define vi vector #define vll vector #define X first #define Y second using namespace std; int n; char tip; char a[111][111]; map > gdje; map , int> koji; vector susjedi[111*111]; vector > sol; bool bio[111*111]; void rek(int cvor) { bio[cvor] = 1; for (int i = 0; i < susjedi[cvor].size(); i++) { int nxt = susjedi[cvor][i]; if (!bio[nxt]) { rek(nxt); sol.push_back(make_pair(nxt, cvor)); } } return; } void traziGlavnaDijagonala(int x, int y, int br) { int minb = min(x, y); int nx = x - minb + 1; int ny = y - minb + 1; while (nx <= n and ny <= n) { if (nx == x and ny == y) {nx++; ny++; continue;} if (a[nx][ny] == tip) susjedi[br].push_back(koji[make_pair(nx, ny)]); nx++; ny++; } } void traziSporednaDijagonala(int x, int y, int br) { int nx; int ny; if (x + y >= n + 1) { nx = n; ny = y - (n - x); while (ny <= n) { if (nx == x and ny == y) {nx--; ny++; continue;} if (a[nx][ny] == tip) susjedi[br].push_back(koji[make_pair(nx, ny)]); nx--; ny++; } } else { ny = 1; nx = x + (y - 1); while (nx >= 1) { if (nx == x and ny == y) {nx--; ny++; continue;} if (a[nx][ny] == tip) susjedi[br].push_back(koji[make_pair(nx, ny)]); nx--; ny++; } } } void traziVertikalno(int x, int y, int br) { for (int i = 1; i <= n; i++) { if (i == x) continue; if (a[i][y] == tip) { susjedi[br].push_back(koji[make_pair(i, y)]); } } } void traziHorizontalno(int x, int y, int br) { for (int i = 1; i <= n; i++) { if (i == y) continue; if (a[x][i] == tip) { susjedi[br].push_back(koji[make_pair(x, i)]); } } } void traziSusjede(int x, int y, int br) { if (tip == 'K') { for (int i = max(1, x - 1); i <= min(n, x + 1); i++) { for (int j = max(1, y - 1); j <= min(n, y + 1); j++) { if (a[i][j] == tip and !(i == x and j == y)) { susjedi[br].push_back(koji[make_pair(i, j)]); } } } } else if (tip == 'B') { traziGlavnaDijagonala(x, y, br); traziSporednaDijagonala(x, y, br); } else if (tip == 'Q') { traziGlavnaDijagonala(x, y, br); traziSporednaDijagonala(x, y, br); traziVertikalno(x, y, br); traziHorizontalno(x, y, br); } else if (tip == 'R') { traziHorizontalno(x, y, br); traziVertikalno(x, y, br); } else { if (x - 2 >= 1 and y - 1 >= 1 and a[x - 2][y - 1] == tip) susjedi[br].push_back(koji[make_pair(x - 2, y - 1)]); if (x - 2 >= 1 and y + 1 <= n and a[x - 2][y + 1] == tip) susjedi[br].push_back(koji[make_pair(x - 2, y + 1)]); if (x - 1 >= 1 and y - 2 >= 1 and a[x - 1][y - 2] == tip) susjedi[br].push_back(koji[make_pair(x - 1, y - 2)]); if (x - 1 >= 1 and y + 2 <= n and a[x - 1][y + 2] == tip) susjedi[br].push_back(koji[make_pair(x - 1, y + 2)]); if (x + 2 <= n and y - 1 >= 1 and a[x + 2][y - 1] == tip) susjedi[br].push_back(koji[make_pair(x + 2, y - 1)]); if (x + 2 <= n and y + 1 <= n and a[x + 2][y + 1] == tip) susjedi[br].push_back(koji[make_pair(x + 2, y + 1)]); if (x + 1 <= n and y - 2 >= 1 and a[x + 1][y - 2] == tip) susjedi[br].push_back(koji[make_pair(x + 1, y - 2)]); if (x + 1 <= n and y + 2 <= n and a[x + 1][y + 2] == tip) susjedi[br].push_back(koji[make_pair(x + 1, y + 2)]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> tip; int cnt = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; if (a[i][j] == tip) { cnt++; gdje[cnt] = make_pair(i, j); koji[make_pair(i, j)] = cnt; } } } cnt = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (a[i][j] == tip) { cnt++; traziSusjede(i, j, cnt); } } } /*for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (a[i][j] == tip) { cout << i << ' ' << j << " -> " << koji[make_pair(i, j)] << " : " << endl; for (int k = 0; k < susjedi[koji[make_pair(i, j)]].size(); k++) { cout << " " << susjedi[koji[make_pair(i, j)]][k] << endl; } } } }*/ rek(1); bool da = true; for (int i = 1; i <= cnt; i++) if (!bio[i]) da = false; if (!da) cout << "NO" << endl; else { cout << "YES" << endl; for (int i = 0; i < sol.size(); i++) { cout << gdje[sol[i].X].X << ' ' << gdje[sol[i].X].Y << ' ' << gdje[sol[i].Y].X << ' ' << gdje[sol[i].Y].Y << endl; } } }