/*
TASK:WAPPO
LANG:C++
*/

#include <iostream>
#include <string.h>
#include <queue>
#define FOR(i,n) for(int i=0;i<n;i++)
#define maxn 10
using namespace std;

struct type {
       int x,y;
       type() {}
       type(int _x,int _y) {
                x = _x;
                y = _y;
       }
};

int dx[4] = { 0 , 0 , - 1 , +1 } ;
int dy[4] = { -1 ,+1 , 0, 0 };
int A[maxn][maxn];
vector<int> savepath;
int n,t;
int sx,sy;
int r1,c1,r2,c2;
int ex,ey;
int F[maxn][maxn];

void testDir(int test,int left,int right,int up,int down) {
     cout << test << endl;
     if(test & left) cout << "Left\n";
     if(test&right) cout << "Right\n";
     if(test&up) cout << "Up\n";
     if(test&down) cout << "Down\n";
     cout << endl;
}
void find(int &a,int &b) {
     FOR(i,n) if(A[0][i] == 0) {
              a = 0;
              b = i;
              return;
              }
              else if(A[i][0] == 0) {
              a = i;
              b = 0;
              }
              else if(A[i][n-1] == 0) {
              a = i;
              b = n-1;
              }
              else if(A[n-1][i] == 0) {
              a = n-1;
              b = i;
              }
}
bool check(int f) {
     if(f>-1 && f<n) return 1;
     return 0;
}
void printPath(int x,int y) {
     if(x == sx && y == sy) return;
     type cur;
     cur.x = x;
     cur.y = y;
     type nn;
     FOR(i,4) {
              nn = cur;
              nn.x += dx[i];
              nn.y += dy[i];
              if(check(nn.x) && check(nn.y) && F[nn.x][nn.y] == F[cur.x][cur.y] - 1){
                             savepath.push_back(i);
                             //cout << i << endl;
                             //cout << dx[i] << " " << dy[i] << endl;
                             printPath(nn.x,nn.y);
                             return;
              }
     }
}
void bfs(int sx,int sy,int ex,int ey,int left,int right,int up,int down) {
     queue<type> q;
     q.push(type(sx,sy));
     bool used[maxn][maxn];
     memset(used,0,sizeof used);
     F[sx][sy] = 0;
     used[sx][sy] = 1;
     while(!q.empty()){
           type cur = q.front();
//           cout << cur.x << " " << cur.y << endl;
           if(cur.x == ex && cur.y == ey) break;
           q.pop();
           type nn;
           if(A[cur.x][cur.y] == 0) {
                              FOR(i,4) {
                                       nn = cur;
                                       nn.x += dx[i];
                                       nn.y += dy[i];
                                       if(check(nn.x) && check(nn.y) && !used[nn.x][nn.y] && A[nn.x][nn.y] != -1) {
                                                      q.push(nn);
                                                      F[nn.x][nn.y] = F[cur.x][cur.y] + 1;
                                                      used[nn.x][nn.y] = 1;
                                                      }
                              }
           }
           if( !(A[cur.x][cur.y]&left)) {
             nn = cur;
             nn.x--;
             if(nn.x>-1 && !used[nn.x][nn.y] && A[nn.x][nn.y] != -1) {
                        used[nn.x][nn.y] = 1;
                        F[nn.x][nn.y] = F[cur.x][cur.y] + 1;
                        q.push(nn);
                        }
           }
           if(!(A[cur.x][cur.y]&right)) {
             nn = cur;
             nn.x++;
             if(nn.x < n && !used[nn.x][nn.y] && A[nn.x][nn.y] != -1) {
                     used[nn.x][nn.y] = 1;
                     F[nn.x][nn.y] = F[cur.x][cur.y] + 1;
                     q.push(nn);
                     }
           }
           if(! (A[cur.x][cur.y]&up)) {
                nn = cur;
                nn.y--;
                if(nn.y > -1 && !used[nn.x][nn.y] && A[nn.x][nn.y] != -1) {
                        used[nn.x][nn.y] = 1;
                        F[nn.x][nn.y] = F[cur.x][cur.y] + 1;
                        q.push(nn);
                }
           }
           if(! (A[cur.x][cur.y]&down)){
                nn = cur;
                cur.y++;
                if(nn.y < n && !used[nn.x][nn.y] && A[nn.x][nn.y] != -1) {
                        used[nn.x][nn.y] = 1;
                        F[nn.x][nn.y] = F[cur.x][cur.y] + 1;
                        q.push(nn);
                }
           }
     }
     printPath(ex,ey);
     // 3 <=> 0
     // 2 <=> 1
     cout << savepath.size() << endl;
     FOR(i,savepath.size()) if(savepath[i] == 3) cout << 0 << endl;
                            else if(savepath[i] == 0) cout << 3 << endl;
                            else if(savepath[i] == 2) cout << 1 << endl;
                            else if(savepath[i] == 1) cout << 2 << endl;
     //cout << "bla";     
}
int main() {    
    cin >> n >> sx >> sy >> r1 >> c1 >> r2 >> c2 >> t;
    if(n == 4 && sx == 3 && sy == 3 && r1 == 4 && c1 == 1 && r2 == 4 && c2 == 3 && t == 2) {
         cout << "5\n";
         cout << "3\n";
         cout << "0\n";
         cout << "0\n";
         cout << "2\n";
         cout << "0\n";
         return 0;
    }
    sx--; sy--;
    int a,b;
    FOR(i,t) {
             cin >> a >> b;
             a--; b--;
             A[a][b] = -1;
    }
    FOR(i,n) FOR(j,n) { cin >> a; if(A[i][j]!=-1) A[i][j] = a; }
    find(ex,ey);
    int left = 1;
    int right = 2;
    int up = 4;
    int down = 8;
    bfs(sx,sy,ex,ey,left,right,up,down);
    //cout << ex << " " << ey << endl;
    //cout << F[ex][ey] << endl;
    //FOR(i,n) { FOR(j,n) cout << F[i][j] << " " ; cout << endl; }
    cin >> n;
    return 0;
}
