#include <fstream>
#include <deque>
#include <vector>

using namespace std;

class cState {
public :
  int x, y, z;
  cState(int X, int Y, int Z) {
    x = X; y = Y; z = Z;
  }
} ;

typedef pair <cState, int> tPair;
typedef deque <tPair> tQue;
typedef vector <bool> tVec;

#include <iostream>
#define in cin

int n;
char m[100][100];
int main() {
  while (1) {
    int n, p1, p2, p3;
    in >> n;
    if (!n)
      break;
    in >> p1 >> p2 >> p3;
    p1--; p2--; p3--;
    for (int i=0; i<n; i++)
      for (int j=0; j<n; j++)
	in >> m[i][j];
    
    tQue q;
    tVec v(n*n*n, false);
    
    q.push_back(tPair(cState(p1, p2, p3),0));
    v[p1*n*n+p2*n+p3] = true;
    
    while (1) {
      if (q.empty()) {
	cout << "impossible";
	break;
      }
      tPair p = q.front(); q.pop_front();
      cState s = p.first;
      int level = p.second;

      if (s.x == s.y && s.y == s.z) {
	cout << level;
	break;
      }

      for (int i=0; i<n; i++)
	if (m[s.x][i] == m[s.y][s.z] && !v[i*n*n+s.y*n+s.z]) {
	  q.push_back(tPair(cState(i, s.y, s.z), level+1));
	  v[i*n*n+s.y*n+s.z] = true;
	}
      for (int i=0; i<n; i++)
	if (m[s.y][i] == m[s.x][s.z] && !v[s.x*n*n+i*n+s.z]) {
	  q.push_back(tPair(cState(s.x, i, s.z), level+1));
	  v[s.x*n*n+i*n+s.z] = true;
	}
      for (int i=0; i<n; i++)
	if (m[s.z][i] == m[s.x][s.y] && !v[s.x*n*n+s.y*n+i]) {
	  q.push_back(tPair(cState(s.x, s.y, i), level+1));
	  v[s.x*n*n+s.y*n+i] = true;
	}   
    }
    cout << endl;
    
  }
  return 0;
}

