#include<iostream>
//#include<fstream>
using namespace std;
//ifstream fin("labyrinth.in");
const int N_MAX = 2005;
const int M_MAX = 2005;
int N, M;
unsigned int poz_init_X, poz_init_Y;
unsigned int poz_finala_X, poz_finala_Y;
unsigned int matrice[N_MAX][M_MAX];
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};
void Citire()
{
cin >> N >> M;
cin >> poz_init_X >> poz_init_Y;
cin >> poz_finala_X >> poz_finala_Y;
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
cin >> matrice[i][j];
}
bool check(int i, int j)
{
return i >= 0 && i < N && j >= 0 && j < M;
}
void afisare()
{
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cout << matrice[i][j] << " ";
cout << "\n";
}
cout << "\n\n";
}
void solve(int i, int j, int level)
{
for(int k = 0; k < 4; k++)
{
int i_vecin = i + di[k];
int j_vecin = j + dj[k];
if( check(i_vecin, j_vecin) && !matrice[i_vecin][j_vecin] )
{
matrice[i_vecin][j_vecin] = level;
if(i_vecin == poz_finala_X && j_vecin == poz_finala_Y) afisare();
else
solve(i_vecin, j_vecin, level + 1);
matrice[i_vecin][j_vecin] = 0;
}
}
}
int main()
{
Citire();
matrice[poz_init_X][poz_init_Y] = 1;
solve(poz_init_X, poz_init_Y, 2);
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+Ci8vI2luY2x1ZGU8ZnN0cmVhbT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovL2lmc3RyZWFtIGZpbigibGFieXJpbnRoLmluIik7Cgpjb25zdCBpbnQgTl9NQVggPSAyMDA1Owpjb25zdCBpbnQgTV9NQVggPSAyMDA1OwoKaW50IE4sIE07Cgp1bnNpZ25lZCBpbnQgcG96X2luaXRfWCwgcG96X2luaXRfWTsKdW5zaWduZWQgaW50IHBvel9maW5hbGFfWCwgcG96X2ZpbmFsYV9ZOwoKdW5zaWduZWQgaW50IG1hdHJpY2VbTl9NQVhdW01fTUFYXTsKCmNvbnN0IGludCBkaVs0XSA9IHstMSwgMCwgMSwgIDB9OwoKY29uc3QgaW50IGRqWzRdID0gezAsICAxLCAwLCAtMX07Cgp2b2lkIENpdGlyZSgpCnsKICAgIGNpbiA+PiBOID4+IE07CiAgICBjaW4gPj4gcG96X2luaXRfWCA+PiBwb3pfaW5pdF9ZOwogICAgCiAgICBjaW4gPj4gcG96X2ZpbmFsYV9YID4+IHBvel9maW5hbGFfWTsKCiAgICBmb3IoaW50IGkgPSAwOyBpIDwgTjsgaSsrKQogICAgCiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IE07IGorKykKICAgICAgICAKICAgICAgICAgICAgY2luID4+IG1hdHJpY2VbaV1bal07Cn0KCmJvb2wgY2hlY2soaW50IGksIGludCBqKQp7CiAgICAgcmV0dXJuIGkgPj0gMCAmJiBpIDwgTiAmJiBqID49IDAgJiYgaiA8IE07Cn0KCnZvaWQgYWZpc2FyZSgpCnsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBOOyBpKyspCiAgICB7CiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IE07IGorKykKICAgICAgICAKICAgICAgICAgICAgY291dCA8PCBtYXRyaWNlW2ldW2pdIDw8ICIgIjsKCiAgICAgICAgY291dCA8PCAiXG4iOwogICAgfQogICAgY291dCA8PCAiXG5cbiI7Cn0KCnZvaWQgc29sdmUoaW50IGksIGludCBqLCBpbnQgbGV2ZWwpCnsKICAgICAgICBmb3IoaW50IGsgPSAwOyBrIDwgNDsgaysrKQogICAgICAgIHsKCiAgICAgICAgICAgIGludCBpX3ZlY2luID0gaSArIGRpW2tdOwogICAgICAgICAgICAKICAgICAgICAgICAgaW50IGpfdmVjaW4gPSBqICsgZGpba107CiAgICAgICAgICAgIAogICAgICAgICAgICBpZiggY2hlY2soaV92ZWNpbiwgal92ZWNpbikgJiYgIW1hdHJpY2VbaV92ZWNpbl1bal92ZWNpbl0gKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBtYXRyaWNlW2lfdmVjaW5dW2pfdmVjaW5dID0gbGV2ZWw7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIGlmKGlfdmVjaW4gPT0gcG96X2ZpbmFsYV9YICYmIGpfdmVjaW4gPT0gcG96X2ZpbmFsYV9ZKSBhZmlzYXJlKCk7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIGVsc2UgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgc29sdmUoaV92ZWNpbiwgal92ZWNpbiwgbGV2ZWwgKyAxKTsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgIG1hdHJpY2VbaV92ZWNpbl1bal92ZWNpbl0gPSAwOwogICAgICAgICAgICB9CiAgICAgICAgfQoKfQoKaW50IG1haW4oKQp7CiAgICBDaXRpcmUoKTsKICAgIAogICAgbWF0cmljZVtwb3pfaW5pdF9YXVtwb3pfaW5pdF9ZXSA9IDE7CiAgICAKICAgIHNvbHZlKHBvel9pbml0X1gsIHBvel9pbml0X1ksIDIpOwogICAgCiAgICByZXR1cm4gMDsKfQ==