#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>

using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
using ll = long long;
using lint = long long;
typedef pair<int,int> P;
const lint inf=1e18+7;
const int MOD=1000000007;
struct Node{
  int no;//number of node
  int col;//color of binary_graph
  long long dist;//distance of arbitrary node
  int group;//group number of disconnected graph
  int indegree;//indegree of this node
  vector<pair<Node*,long long>> child;//chidren of this node
  vector<pair<Node*,int>> par;//parretns of this node
  bool status;
  void build(int i){
    no=i;col=0;dist=inf;
    group=0;indegree=0;
  }
};
bool operator >(const Node &a,const Node &r){
        return a.dist > r.dist;
    }
struct Graph{
    int n,h,w;
    bool direct;
    vector<Node> node;
    Graph(int hh,int ww){
        h=hh;w=ww;
        n=h*w;
        node.resize(n+1);
        for(int i=1;i<=n;++i){
            node[i].build(i);
        }
    }
    void input(int m,bool flag){
        //choose direct or undirect
        direct=flag;
        for(int i=0;i<m;++i){
            int l=1;
            int a,b;cin>>a>>b>>l;
            node[a].child.push_back({&node[b],l});
            if(!direct){
                node[b].child.push_back({&node[a],l});
            }
            else{
                node[b].par.push_back({&node[a],l});
                node[b].indegree++;
            }
        }
    }
    vector<Node> topological_sort(){
        //return the least lexical order in the topological sorted array
        priority_queue<int,vector<int>,greater<int>> que;
        vector<Node> res;
        for(int i=1;i<=n;++i){
            if(node[i].indegree==0){
                que.push(i);
            }
        }
        while(!que.empty()){
            int buf=que.top();que.pop();
            int num=node[buf].child.size();
            res.push_back(node[buf]);
            for(int i=0;i<num;++i){
                node[buf].child[i].first->indegree--;
                if(node[buf].child[i].first->indegree==0){
                    que.push(node[buf].child[i].first->no);
                }
            }
        }
        return res;
    }
    void dijkstra(int s){
        for(int i=0;i<n;++i){
            node[i].dist=inf;
        }
        node[s].dist=0;
        priority_queue<Node,vector<Node>,greater<Node>> que;
        que.push(node[s]);
        while(!que.empty()){
            Node buf=que.top();que.pop();
            int num=buf.child.size();
            for(int i=0;i<num;++i){
                if(buf.child[i].first->dist>buf.child[i].second+buf.dist){
                    buf.child[i].first->dist=buf.child[i].second+buf.dist;
                    que.push(*buf.child[i].first);
                }
            }
        }
    }
    bool binary_graph(){
        node[1].col=1;
        queue<Node> que;
        que.push(node[1]);
        bool res=true;
        while(!que.empty()){
            Node buf=que.front();que.pop();
            int num=buf.child.size();
            for(int i=0;i<num;++i){
                if(buf.child[i].first->col==0){
                    buf.child[i].first->col=buf.col*2%3;
                    que.push(*buf.child[i].first);
                }
                else{
                    if(buf.child[i].first->col==buf.col){
                        res=false;
                    }
                }
            }
        }
        return res;
    }
    vector<vector<long long>> warshall_froyd(){
        vector<vector<long long>> dp(n+1,vector<long long>(n+1,inf));
        for(int i=1;i<=n;++i){
            int num=node[i].child.size();
            for(int j=0;j<num;++j){
                dp[i][node[i].child[j].first->no]=node[i].child[j].second;
            }
            dp[i][i]=0;
        }
        for(int k=1;k<=n;++k){
            for(int i=1;i<=n;++i){
                for(int j=1;j<=n;++j){
                    dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
                }
            }
        }
        return dp;
    }
    void solve(){
        P c,d;
        cin>>c.first>>c.second;
        cin>>d.first>>d.second;
        c.first--;c.second--;
        d.first--;d.second--;
        vector<string> s(h);
        rep(i,h)cin>>s[i];
        direct=false;
        rep(i,h){
            rep(j,w){
                if(s[i][j]=='.')node[i*w+j].status=true;
                else node[i*w+j].status=false;
            }
        }
        vector<int> dx={1,0,-1,0};
        vector<int> dy={0,1,0,-1};
        rep(i,h){
            rep(j,w){
                rep(x,4){
                    rep(y,4){
                        if(x!=y)continue;
                        if(i+dy[y]<h&&i+dy[y]>=0){
                            if(j+dx[x]<w&&j+dx[x]>=0){
                                if(node[(i+dy[y])*w+j+dx[x]].status){
                                    node[i*w+j].child.push_back({&node[(i+dy[y])*w+j+dx[x]],0});
                                }
                            }
                        }
                    }
                }
                rep1(x,5){
                    rep1(y,5){
                        if(x==3&&y==3)continue;
                        //i*w+j+1
                        if(i-3+y<h&&i-3+y>=0){
                            if(j-3+x<w&&j-3+x>=0){
                                if(node[(i+y-3)*w+j-3+x].status){
                                    node[i*w+j].child.push_back({&node[(i+y-3)*w+j-3+x],1});
                                }
                            }
                        }
                    }
                }
            }
        }
        dijkstra(c.first*w+c.second);
        int res=0;
        if(node[d.first*w+d.second].dist==inf){
            res=-1;
        }
        else res=node[d.first*w+d.second].dist;
        cout<<res<<"\n";
        rep(i,h*0){
            rep(j,w){
                cout<<node[i*w+j].dist<<" ";
            }
            cout<<endl;
        }
    }
};

signed main(){
  int h,w;cin>>h>>w;
  Graph g(h,w);
  g.solve();
  return 0;
}

Measured memory_bytes: 413532