#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream& operator>>(istream& in, vector<T>& arr) {
  for (T& i : arr) {
    in >> i;
  }
  return in;
}
template <typename T>
ostream& operator<<(ostream& out, vector<vector<T>>& arr) {
  for (vector<T>& i : arr) {
    out << i;
  }
  out << endl;
  return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T>& arr) {
  for (T& i : arr) {
    out << i << ' ';
  }
  out << endl;
  return out;
}
int dp[71][71][71];
bool visited[71][71][71];
int previous_a[71][71][71];
int previous_b[71][71][71];
int previous_c[71][71][71];
char matrix[71][71];
int main() {
  int n;
  cin >> n;
  int a, b, c;
  cin >> a >> b >> c;
  for (int i = 1; i < n + 1; i++)
    for (int j = 1; j < n + 1; j++) cin >> matrix[i][j];
  for (int i = 0; i < 71; i++)
    for (int j = 0; j < 71; j++)
      for (int k = 0; k < 71; k++) dp[i][j][k] = -1;
  visited[a][b][c] = true;
  dp[a][b][c] = 0;
  queue<int> as, bs, cs;
  as.push(a);
  bs.push(b);
  cs.push(c);
  while (!as.empty()) {
    int cur_a = as.front();
    int cur_b = bs.front();
    int cur_c = cs.front();
    as.pop();
    bs.pop();
    cs.pop();
    for (int d = 1; d <= n; d++) {
      if (d != cur_a && d != cur_b && d != cur_c) {
        if (matrix[cur_a][d] == matrix[cur_b][cur_c] &&
            !visited[d][cur_b][cur_c]) {
          visited[d][cur_b][cur_c] = true;
          dp[d][cur_b][cur_c] = dp[cur_a][cur_b][cur_c] + 1;
          previous_a[d][cur_b][cur_c] = cur_a;
          previous_b[d][cur_b][cur_c] = cur_b;
          previous_c[d][cur_b][cur_c] = cur_c;
          as.push(d);
          bs.push(cur_b);
          cs.push(cur_c);
        }
      }
    }
    for (int d = 1; d <= n; d++) {
      if (d != cur_a && d != cur_b && d != cur_c) {
        if (matrix[cur_b][d] == matrix[cur_a][cur_c] &&
            !visited[cur_a][d][cur_c]) {
          visited[cur_a][d][cur_c] = true;
          dp[cur_a][d][cur_c] = dp[cur_a][cur_b][cur_c] + 1;
          previous_a[cur_a][d][cur_c] = cur_a;
          previous_b[cur_a][d][cur_c] = cur_b;
          previous_c[cur_a][d][cur_c] = cur_c;
          as.push(cur_a);
          bs.push(d);
          cs.push(cur_c);
        }
      }
    }
    for (int d = 1; d <= n; d++) {
      if (d != cur_a && d != cur_b && d != cur_c) {
        if (matrix[cur_c][d] == matrix[cur_a][cur_b] &&
            !visited[cur_a][cur_b][d]) {
          visited[cur_a][cur_b][d] = true;
          dp[cur_a][cur_b][d] = dp[cur_a][cur_b][cur_c] + 1;
          previous_a[cur_a][cur_b][d] = cur_a;
          previous_b[cur_a][cur_b][d] = cur_b;
          previous_c[cur_a][cur_b][d] = cur_c;
          as.push(cur_a);
          bs.push(cur_b);
          cs.push(d);
        }
      }
    }
  }
  int cur_min = INT_MAX;
  int min_a = -1, min_b = -1, min_c = -1;
  if (dp[1][2][3] < cur_min && dp[1][2][3] != -1) {
    min_a = 1, min_b = 2, min_c = 3;
    cur_min = dp[1][2][3];
  }
  if (dp[1][3][2] < cur_min && dp[1][3][2] != -1) {
    min_a = 1, min_b = 3, min_c = 2;
    cur_min = dp[1][3][2];
  }
  if (dp[2][1][3] < cur_min && dp[2][1][3] != -1) {
    min_a = 2, min_b = 1, min_c = 3;
    cur_min = dp[2][1][3];
  }
  if (dp[2][3][1] < cur_min && dp[2][3][1] != -1) {
    min_a = 2, min_b = 3, min_c = 1;
    cur_min = dp[2][3][1];
  }
  if (dp[3][1][2] < cur_min && dp[3][1][2] != -1) {
    min_a = 3, min_b = 1, min_c = 2;
    cur_min = dp[3][1][2];
  }
  if (dp[3][2][1] < cur_min && dp[3][2][1] != -1) {
    min_a = 3, min_b = 2, min_c = 1;
    cur_min = dp[3][2][1];
  }
  if (cur_min == INT_MAX) {
    cout << -1;
    return 0;
  }
  cout << dp[min_a][min_b][min_c] << endl;
  vector<int> path_a;
  vector<int> path_b;
  vector<int> path_c;
  int cur_a = min_a, cur_b = min_b, cur_c = min_c;
  while (dp[cur_a][cur_b][cur_c] != -1) {
    path_a.push_back(cur_a);
    path_b.push_back(cur_b);
    path_c.push_back(cur_c);
    int n_cur_a = previous_a[cur_a][cur_b][cur_c];
    int n_cur_b = previous_b[cur_a][cur_b][cur_c];
    int n_cur_c = previous_c[cur_a][cur_b][cur_c];
    cur_a = n_cur_a;
    cur_b = n_cur_b;
    cur_c = n_cur_c;
  }
  path_a.push_back(a);
  path_b.push_back(b);
  path_c.push_back(c);
  reverse(path_a.begin(), path_a.end());
  reverse(path_b.begin(), path_b.end());
  reverse(path_c.begin(), path_c.end());
  for (int i = 0; i < path_a.size() - 1; i++) {
    if (path_a[i] != path_a[i + 1]) {
      printf("%d %d\n", path_a[i], path_a[i + 1]);
    } else if (path_b[i] != path_b[i + 1]) {
      printf("%d %d\n", path_b[i], path_b[i + 1]);
    } else if (path_c[i] != path_c[i + 1]) {
      printf("%d %d\n", path_c[i], path_c[i + 1]);
    }
  }
}
