#include using namespace std; #define TRACE(x) cerr << __LINE__ << ": " << #x << " = " << x << endl #define _ << " _ " << template struct is_container : false_type {}; template struct is_container> : true_type {}; template struct is_container> : true_type {}; template struct is_container> : true_type {}; template struct is_container> : true_type {}; template struct is_container> : true_type {}; template struct is_container> : true_type {}; template struct is_container> : true_type {}; template::value>::type> ostream& operator<<(ostream &o, T x) { int f = 1; o << "{"; for (auto y : x) { o << (f ? "" : ", ") << y; f = 0; } return o << "}"; } template ostream& operator<<(ostream &o, pair x) { return o << "(" << x.first << ", " << x.second << ")"; } #define fi first #define se second typedef long long ll; typedef long double ld; typedef pair pii; typedef pair pll; typedef vector vi; typedef vector vll; // (x, y) --> (i, j) ? char C; bool go(int x, int y, int i, int j) { if (x == y && i == j) return false; if (C == 'K') return abs(x - i) <= 1 && abs(y - j) <= 1; if (C == 'N') { int a = abs(x - i), b = abs(y - j); return (a == 2 && b == 1) || (a == 1 && b == 2); } if (C == 'B') return (x - y == i - j) || (x + y == i + j); if (C == 'R') return (x == i) || (y == j); if (C == 'Q') return (x - y == i - j) || (x + y == i + j) || (x == i) || (y == j); assert(false); return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; cin >> C; vector s(n); for (int i = 0; i < n; i++) cin >> s[i]; int total = 0, si = -1, sj = -1; vector>> e(n, vector>(n)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (s[i][j] == '.') continue; if (si == -1) { si = i; sj = j; } total++; for (int x = 0; x < n; x++) for (int y = 0; y < n; y++) if (s[x][y] != '.' && go(x, y, i, j)) e[i][j].push_back({x, y}); } queue Q({{si, sj}}); vector> vis(n, vector(n)); vis[si][sj] = true; int cnt = 0; vector sol; while (!Q.empty()) { auto [i, j] = Q.front(); Q.pop(); cnt++; for (auto p : e[i][j]) { if (vis[p.fi][p.se]) continue; vis[p.fi][p.se] = true; Q.push(p); sol.push_back({p.fi, p.se, i, j}); } } if (cnt != total) { cout << "NO\n"; return 0; } reverse(sol.begin(), sol.end()); cout << "YES\n"; for (auto& vec : sol) { for (int x : vec) cout << x + 1 << ' '; cout << '\n'; } return 0; }