#include <bits/stdc++.h>
using namespace std;
const int maxN = 70 + 13;
int n, x, y, z, dis[maxN][maxN][maxN], ans = -1, adj[maxN][maxN], fx, fy, fz;
queue<pair<int, pair<int, int>>> qu;
vector<pair<int, int>> vec;
pair<int, pair<int, int>> par[maxN][maxN][maxN];
bool check(int X, int Y, int Z) {
  vector<int> vec;
  fx = X, fy = Y, fz = Z;
  vec.clear();
  vec.push_back(X);
  vec.push_back(Y);
  vec.push_back(Z);
  sort(vec.begin(), vec.end());
  if (vec[0] == 1 && vec[1] == 2 && vec[2] == 3) return 1;
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> x >> y >> z;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= n; j++) adj[i][j] = s[j - 1];
  }
  memset(dis, -1, sizeof dis);
  qu.push({x, {y, z}});
  dis[x][y][z] = 0;
  while (!qu.empty()) {
    auto node = qu.front();
    qu.pop();
    int curx = node.first, cury = node.second.first, curz = node.second.second;
    int curdis = dis[curx][cury][curz];
    if (check(curx, cury, curz)) {
      ans = curdis;
      break;
    }
    for (int i = 1; i <= n; i++) {
      if (i != curx && i != cury && i != curz) {
        if (adj[curx][i] == adj[cury][curz] && dis[i][cury][curz] == -1) {
          dis[i][cury][curz] = curdis + 1;
          par[i][cury][curz] = {curx, {cury, curz}};
          qu.push({i, {cury, curz}});
        }
        if (adj[cury][i] == adj[curx][curz] && dis[curx][i][curz] == -1) {
          dis[curx][i][curz] = curdis + 1;
          par[curx][i][curz] = {curx, {cury, curz}};
          qu.push({curx, {i, curz}});
        }
        if (adj[curz][i] == adj[cury][curx] && dis[curx][cury][i] == -1) {
          dis[curx][cury][i] = curdis + 1;
          par[curx][cury][i] = {curx, {cury, curz}};
          qu.push({curx, {cury, i}});
        }
      }
    }
  }
  cout << ans << endl;
  if (ans == -1) return 0;
  while (!(fx == x && fy == y && fz == z)) {
    auto u = par[fx][fy][fz];
    if (fx != u.first) {
      vec.push_back({u.first, fx});
    }
    if (fy != u.second.first) {
      vec.push_back({u.second.first, fy});
    }
    if (fz != u.second.second) {
      vec.push_back({u.second.second, fz});
    }
    fx = u.first, fy = u.second.first, fz = u.second.second;
  }
  reverse(vec.begin(), vec.end());
  for (auto a : vec) cout << a.first << " " << a.second << '\n';
}
