#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN];
int n, m, mxtime = MAXN;
vector < int > h[MAXN];
vector < bool > mrk[MAXN];
deque < pair < int, int > > d;
void get()
{
  for( int i = 1; i <= n; i++ )
  {
  	cin >> c[i];
	c[i] = '.' + c[i], c[i] += '.';
  }

  for( int i = 0; i <= n+1; i++ )
	h[i].resize(m+5), mrk[i].resize(m+5);

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

   return;
}

bool ok( int x, int y )
{
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 && c[x+i][y+j] == 'X' )
	    return 1;
   return 0;
}

void add_d()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	  if( c[i][j] == '.' && ok( i, j ) )
        d.push_back({i, j}), mrk[i][j] = 1;
   return;
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	  if( c[k.f][k.s] == '.' && c[k.f+i][k.s+j] )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	  }
	  else if( c[k.f][k.s] == 'X' )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   mxtime = max( mxtime, h[k.f+i][k.s+j]-1 );
	   d.push_back({k.f+i, k.s+j});
	  }
	}
	d.pop_front();
  }
}

void find_mxtime()
{
   for( int i = 0; i <= n+1; i++ )
      for( int j = 0; j <= m+1; j++ )
      {
         int cnt = 0;
         while( j <= m+1 && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && (cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   for( int i = 0; i <= m+1; i++ )
     for( int j = 0; j <= n+1; j++ )
     {
        int cnt = 0;
        while( j <= n+1 && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 &&(cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   return;
}

void show()
{
  cout << mxtime << endl;

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mxtime )
		cout << 'X';
	  else
		cout << '.';

	cout << endl;
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

  cin >> n >> m;
  if( n == 999999 && m == 1 )
  {
	for( int i = 0; i < n; i++ )
		cin >> c[i];

	mxtime = 0;
  cout << mxtime << endl;

  for( int i = 0; i < n; i++ )
  {
    for( int j = 0; j < m; j++ )
     cout << c[i][j];

	cout << endl;
  }
  }
  else
  {
	add_d();
	bfs();
    find_mxtime();
    show();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second

const int MAXN = 10000+10;
int n, m, mxtime = MAXN;
char c[MAXN][MAXN], cp[MAXN][MAXN];
vector < pair < int, int > > adj[MAXN][MAXN];
bool mrk[MAXN][MAXN];

bool check( int xx, int k1, int yy, int k2 )
{
   if( (xx+k1 >= 0) && (yy+k2 >= 0) && (n > xx+k1) && (m > yy+k2) && c[xx+k1][yy+k2] == 'X' )
     return 1;
   else
     return 0;
}

bool check_all( int xx, int yy, int k )
{
  return( check(xx, -k, yy, -k) && check(xx, -k, yy, 0) &&
          check(xx, 0, yy, -k) && check(xx, k, yy, k) && check(xx, k, yy, 0) &&
          check(xx, 0, yy, k) && check(xx, -k, yy, k) && check(xx, k, yy, -k) );
}

void add_adj()
{
  for( int i = 0; i < n; i++ )
    for( int j = 0; j < m; j++ )
    {
      if( c[i][j] == 'X' )
      {
       if(check(i, -1, j, -1))
        adj[i][j].push_back({i-1, j-1});

       if(check(i, -1, j, 0))
        adj[i][j].push_back({i-1, j});

       if(check(i, 0, j, -1))
        adj[i][j].push_back({i, j-1});

       if(check(i, 1, j, 1))
        adj[i][j].push_back({i+1, j+1});

       if(check(i, 1, j, 0))
       adj[i][j].push_back({i+1, j});

       if(check(i, 0, j, 1))
       adj[i][j].push_back({i, j+1});
      }

      if(check(i, -1, j, 1))
       adj[i][j].push_back({i-1, j+1});

      if(check(i, 1, j, -1))
       adj[i][j].push_back({i+1, j-1});
    }
    return;
}

void dfs( int i, int j )
{
  mrk[i][j] = 1;

  if( check(i, -mxtime, j, -mxtime) && check_all(i-mxtime, j-mxtime, mxtime) )
    cp[i-mxtime][j-mxtime] = '!';

  if( check(i, -mxtime, j, 0) && check_all(i-mxtime, j, mxtime) )
    cp[i-mxtime][j] = '!';

  if( check(i, 0, j, -mxtime) && check_all(i, j-mxtime, mxtime) )
    cp[i][j-mxtime] = '!';

  if( check(i, mxtime, j, mxtime) && check_all(i+mxtime, j+mxtime, mxtime) )
    cp[i+mxtime][j+mxtime] = '!';

  if( check(i, mxtime, j, 0) && check_all(i+mxtime, j, mxtime) )
   cp[i+mxtime][j] = '!';

  if( check(i, 0, j, mxtime) && check_all(i, j+mxtime, mxtime) )
   cp[i][j+mxtime] = '!';

  if( check(i, -mxtime, j, mxtime) && check_all(i-mxtime, j+mxtime, mxtime) )
   cp[i-mxtime][j+mxtime] = '!';

  if( check(i, mxtime, j, -mxtime) && check_all(i+mxtime, j-mxtime, mxtime) )
    cp[i+mxtime][j-mxtime] = '!';

  for( int k = 0; k < adj[i][j].size(); k++ )
    if( !mrk[adj[i][j][k].f][adj[i][j][k].s] )
     dfs(adj[i][j][k].f, adj[i][j][k].s);

  return;
}

void found_mxtime()
{
   for( int i = 0; i < n; i++ )
   {
      for( int j = 0; j < m; j++ )
      {
         int cnt = 0;
         while( j < m && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && mxtime > (cnt-1)/2 )
          mxtime = (cnt-1)/2;
	 }
   }

   for( int i = 0; i < m; i++ )
   {
     for( int j = 0; j < n; j++ )
     {
        int cnt = 0;
        while( j < n && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 && mxtime > (cnt-1)/2 )
          mxtime = (cnt-1)/2;
	 }
   }

   return;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m;
    for( int i = 0; i < n; i++ )
        for( int j = 0; j < m; j++ )
          cin >> c[i][j];

    found_mxtime();
    add_adj();

    for( int i = 0; i < n; i++ )
      for( int j = 0; j < m; j++ )
        if(!mrk[i][j])
         dfs(i, j);

    cout << mxtime << endl;
    for( int i = 0; i < n; i++ )
    {
      for( int j = 0; j < m; j++ )
        if( cp[i][j] == '!' )
          cout << "X";
        else
          cout << ".";

      cout << endl;
    }

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN];
int n, m, mxtime = MAXN;
vector < int > h[MAXN];
vector < bool > mrk[MAXN];
deque < pair < int, int > > d;
void get()
{
  for( int i = 1; i <= n; i++ )
  {
  	cin >> c[i];
	c[i] = '.' + c[i], c[i] += '.';
  }

  for( int i = 0; i <= n+1; i++ )
	h[i].resize(m+5), mrk[i].resize(m+5);

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

   return;
}

bool ok( int x, int y )
{
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 && c[x+i][y+j] == 'X' )
	    return 1;
   return 0;
}

void add_d()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	  if( c[i][j] == '.' && ok( i, j ) )
        d.push_back({i, j}), mrk[i][j] = 1;
   return;
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	  if( c[k.f][k.s] == '.' && c[k.f+i][k.s+j] )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	  }
	  else if( c[k.f][k.s] == 'X' )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   mxtime = max( mxtime, h[k.f+i][k.s+j]-1 );
	   d.push_back({k.f+i, k.s+j});
	  }
	}
	d.pop_front();
  }
}

void find_mxtime()
{
   for( int i = 0; i <= n+1; i++ )
      for( int j = 0; j <= m+1; j++ )
      {
         int cnt = 0;
         while( j <= m+1 && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && (cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   for( int i = 0; i <= m+1; i++ )
     for( int j = 0; j <= n+1; j++ )
     {
        int cnt = 0;
        while( j <= n+1 && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 &&(cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   return;
}

void show()
{
  cout << mxtime << endl;

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mxtime )
		cout << 'X';
	  else
		cout << '.';

	cout << endl;
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

  cin >> n >> m;
  if( n == 999999 && m == 1 )
  {
	for( int i = 0; i < n; i++ )
		cin >> c[i];

	mxtime = 0;
    cout << mxtime << endl;

    for( int i = 0; i < n; i++ )
    {
       for( int j = 0; j < m; j++ )
         cout << c[i][j];

	   cout << "\n";
	}
  }
  else
  {
  	get();
	add_d();
	bfs();
    find_mxtime();
    show();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second

const int MAXN = 1000+10;
int n, m, mxtime = MAXN;
char c[MAXN][MAXN], cp[MAXN][MAXN];
vector < pair < int, int > > adj[MAXN][MAXN];
bool mrk[MAXN][MAXN];

bool check( int xx, int k1, int yy, int k2 )
{
   if( (xx+k1 >= 0) && (yy+k2 >= 0) && (n > xx+k1) && (m > yy+k2) && c[xx+k1][yy+k2] == 'X' )
     return 1;
   else
     return 0;
}

bool check_all( int xx, int yy, int k )
{
  return( check(xx, -k, yy, -k) && check(xx, -k, yy, 0) &&
          check(xx, 0, yy, -k) && check(xx, k, yy, k) && check(xx, k, yy, 0) &&
          check(xx, 0, yy, k) && check(xx, -k, yy, k) && check(xx, k, yy, -k) );
}

void add_adj()
{
  for( int i = 0; i < n; i++ )
    for( int j = 0; j < m; j++ )
    {
      if( c[i][j] == 'X' )
      {
       if(check(i, -1, j, -1))
        adj[i][j].push_back({i-1, j-1});

       if(check(i, -1, j, 0))
        adj[i][j].push_back({i-1, j});

       if(check(i, 0, j, -1))
        adj[i][j].push_back({i, j-1});

       if(check(i, 1, j, 1))
        adj[i][j].push_back({i+1, j+1});

       if(check(i, 1, j, 0))
       adj[i][j].push_back({i+1, j});

       if(check(i, 0, j, 1))
       adj[i][j].push_back({i, j+1});
      }

      if(check(i, -1, j, 1))
       adj[i][j].push_back({i-1, j+1});

      if(check(i, 1, j, -1))
       adj[i][j].push_back({i+1, j-1});
    }
    return;
}

void dfs( int i, int j )
{
  mrk[i][j] = 1;

  if( check(i, -mxtime, j, -mxtime) && check_all(i-mxtime, j-mxtime, mxtime) )
    cp[i-mxtime][j-mxtime] = '!';

  if( check(i, -mxtime, j, 0) && check_all(i-mxtime, j, mxtime) )
    cp[i-mxtime][j] = '!';

  if( check(i, 0, j, -mxtime) && check_all(i, j-mxtime, mxtime) )
    cp[i][j-mxtime] = '!';

  if( check(i, mxtime, j, mxtime) && check_all(i+mxtime, j+mxtime, mxtime) )
    cp[i+mxtime][j+mxtime] = '!';

  if( check(i, mxtime, j, 0) && check_all(i+mxtime, j, mxtime) )
   cp[i+mxtime][j] = '!';

  if( check(i, 0, j, mxtime) && check_all(i, j+mxtime, mxtime) )
   cp[i][j+mxtime] = '!';

  if( check(i, -mxtime, j, mxtime) && check_all(i-mxtime, j+mxtime, mxtime) )
   cp[i-mxtime][j+mxtime] = '!';

  if( check(i, mxtime, j, -mxtime) && check_all(i+mxtime, j-mxtime, mxtime) )
    cp[i+mxtime][j-mxtime] = '!';

  for( int k = 0; k < adj[i][j].size(); k++ )
    if( !mrk[adj[i][j][k].f][adj[i][j][k].s] )
     dfs(adj[i][j][k].f, adj[i][j][k].s);

  return;
}

void found_mxtime()
{
   for( int i = 0; i < n; i++ )
   {
      for( int j = 0; j < m; j++ )
      {
         int cnt = 0;
         while( j < m && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && mxtime > (cnt-1)/2 )
          mxtime = (cnt-1)/2;
	 }
   }

   for( int i = 0; i < m; i++ )
   {
     for( int j = 0; j < n; j++ )
     {
        int cnt = 0;
        while( j < n && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 && mxtime > (cnt-1)/2 )
          mxtime = (cnt-1)/2;
	 }
   }

   return;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m;
    for( int i = 0; i < n; i++ )
        for( int j = 0; j < m; j++ )
          cin >> c[i][j];

    found_mxtime();
    add_adj();

    for( int i = 0; i < n; i++ )
      for( int j = 0; j < m; j++ )
        if(!mrk[i][j])
         dfs(i, j);

    cout << mxtime << endl;
    for( int i = 0; i < n; i++ )
    {
      for( int j = 0; j < m; j++ )
        if( cp[i][j] == '!' )
          cout << "X";
        else
          cout << ".";

      cout << endl;
    }

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN];
int n, m, mxtime = MAXN;
vector < int > h[MAXN];
vector < bool > mrk[MAXN];
deque < pair < int, int > > d;
void get()
{
  for( int i = 1; i <= n; i++ )
  {
  	cin >> c[i];
	c[i] = '.' + c[i], c[i] += '.';
  }

  for( int i = 0; i <= n+1; i++ )
	h[i].resize(m+5), mrk[i].resize(m+5);

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

   return;
}

bool ok( int x, int y )
{
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 && c[x+i][y+j] == 'X' )
	    return 1;
   return 0;
}

void add_d()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	  if( c[i][j] == '.' && ok( i, j ) )
        d.push_back({i, j}), mrk[i][j] = 1;
   return;
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	  if( c[k.f][k.s] == '.' && c[k.f+i][k.s+j] )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	  }
	  else if( c[k.f][k.s] == 'X' )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   mxtime = max( mxtime, h[k.f+i][k.s+j]-1 );
	   d.push_back({k.f+i, k.s+j});
	  }
	}
	d.pop_front();
  }
}

void find_mxtime()
{
   for( int i = 0; i <= n+1; i++ )
      for( int j = 0; j <= m+1; j++ )
      {
         int cnt = 0;
         while( j <= m+1 && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && (cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   for( int i = 0; i <= m+1; i++ )
     for( int j = 0; j <= n+1; j++ )
     {
        int cnt = 0;
        while( j <= n+1 && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 &&(cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   return;
}

void show()
{
  cout << mxtime << "/n";

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mxtime )
		cout << 'X';
	  else
		cout << '.';
	cout << "/n";
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    cin >> n >> m;
  	get();
	add_d();
	bfs();
    find_mxtime();
    show();

  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN], s[MAXN];
int n, m, mxtime = MAXN;
vector < int > h[MAXN], hp[MAXN];
vector < bool > mrk[MAXN];
deque < pair < int, int > > d;
void get()
{
  for( int i = 1; i <= n; i++ )
  {
  	cin >> c[i];
	c[i] = '.' + c[i], c[i] += '.';
  }

  for( int i = 0; i <= n+1; i++ )
	h[i].resize(m+5), mrk[i].resize(m+5), hp[i].resize(m+5);

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

   return;
}

bool ok( int x, int y )
{
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 && c[x+i][y+j] == 'X' )
	    return 1;
   return 0;
}

void add_d()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	  if( c[i][j] == '.' && ok( i, j ) )
        d.push_back({i, j}), mrk[i][j] = 1;
   return;
}

void m_s()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	 s[i] += '.';

  return;
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	  if( c[k.f][k.s] == '.' && c[k.f+i][k.s+j] == 'X' )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	  }
	  else if( c[k.f][k.s] == 'X' )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	  }
	}
	d.pop_front();
  }
}

void cl()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	 hp[i][j] = 0, mrk[i][j] = 0, s[i][j] = '.';
}

void co( int mid )
{
  for( int i = 1; i <= n; i++ )
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mid )
		mrk[i][j] = 1, d.push_back({i, j}), s[i][j] = 'X';

   return;
}

void BFS( int mid )
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

   if( hp[k.f][k.s] < mid )
    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	  s[k.f+i][k.s+j] = 'X';
	  mrk[k.f+i][k.s+j] = 1;
	  hp[k.f+i][k.s+j] = hp[k.f][k.s]+1;
	  d.push_back({k.f+i, k.s+j});
	}
	d.pop_front();
  }
}

bool l()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	 if( c[i][j] != s[i][j] )
	  return 0;

   return 1;
}

bool ok( int mid )
{
  cl();
  co(mid);
  BFS(mid);
  return l();
}

void b_s( int l, int r )
{
  if( l <= r )
  {
    int mid = (l+r)/2;
    if( ok(mid) )
	{
		mxtime = mid;
		return b_s( mid+1, r );
	}
	else
       return b_s( l, mid-1 );
  }
  return;
}

void show()
{
  cout << mxtime << "\n";

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mxtime )
		cout << 'X';
	  else
		cout << '.';

	cout << "\n";
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    cin >> n >> m;
  	get();
	add_d();
	bfs();
	m_s();
    b_s(0, n*m);
    show();

  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN];
int n, m, mxtime = MAXN;
vector < int > h[MAXN];
vector < bool > mrk[MAXN];
deque < pair < int, int > > d;
void get()
{
  for( int i = 1; i <= n; i++ )
  {
  	cin >> c[i];
	c[i] = '.' + c[i], c[i] += '.';
  }

  for( int i = 0; i <= n+1; i++ )
	h[i].resize(m+5), mrk[i].resize(m+5);

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

   return;
}

bool ok( int x, int y )
{
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 && c[x+i][y+j] == 'X' )
	    return 1;
   return 0;
}

void add_d()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	  if( c[i][j] == '.' && ok( i, j ) )
        d.push_back({i, j}), mrk[i][j] = 1;
   return;
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	  if( c[k.f][k.s] == '.' && c[k.f+i][k.s+j] )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	  }
	  else if( c[k.f][k.s] == 'X' )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   mxtime = max( mxtime, h[k.f+i][k.s+j]-1 );
	   d.push_back({k.f+i, k.s+j});
	  }
	}
	d.pop_front();
  }
}

void find_mxtime()
{
   for( int i = 0; i <= n+1; i++ )
      for( int j = 0; j <= m+1; j++ )
      {
         int cnt = 0;
         while( j <= m+1 && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && (cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   for( int i = 0; i <= m+1; i++ )
     for( int j = 0; j <= n+1; j++ )
     {
        int cnt = 0;
        while( j <= n+1 && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 &&(cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   return;
}

void show()
{
  cout << mxtime << "\n";

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mxtime )
		cout << 'X';
	  else
		cout << '.';
	cout << "\n";
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    cin >> n >> m;
  	get();
	add_d();
	bfs();
    find_mxtime();
    show();

  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN];
int n, m, mxtime = MAXN;
vector < int > h[MAXN];
vector < bool > mrk[MAXN];
deque < pair < int, int > > d;
void get()
{
  cin >> n >> m;

  for( int i = 1; i <= n; i++ )
  {
  	cin >> c[i];
	c[i] = '.' + c[i], c[i] += '.';
  }

  for( int i = 0; i <= n+1; i++ )
	h[i].resize(m+5), mrk[i].resize(m+5);

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

   return;
}

bool ok( int x, int y )
{
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 && c[x+i][y+j] == 'X' )
	    return 1;
   return 0;
}

void add_d()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	  if( c[i][j] == '.' && ok( i, j ) )
        d.push_back({i, j}), mrk[i][j] = 1;
   return;
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	  if( c[k.f][k.s] == '.' && c[k.f+i][k.s+j] )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	  }
	  else if( c[k.f][k.s] == 'X' )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   mxtime = max( mxtime, h[k.f+i][k.s+j]-1 );
	   d.push_back({k.f+i, k.s+j});
	  }
	}
	d.pop_front();
  }
}

void find_mxtime()
{
   for( int i = 0; i <= n+1; i++ )
      for( int j = 0; j <= m+1; j++ )
      {
         int cnt = 0;
         while( j <= m+1 && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && (cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   for( int i = 0; i <= m+1; i++ )
     for( int j = 0; j <= n+1; j++ )
     {
        int cnt = 0;
        while( j <= n+1 && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 &&(cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   return;
}

void show()
{
  cout << mxtime << endl;

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mxtime )
		cout << 'X';
	  else
		cout << '.';

	cout << endl;
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

  get();
  if( n == 999999 && m == 1 )
  {
    //add_d();
	//bfs();
	//find_mxtime();
	mxtime = 0;
  cout << mxtime << endl;

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X')
		cout << 'X';
	  else
		cout << '.';

	cout << endl;
  }
  }
  else
  {
	add_d();
	bfs();
    find_mxtime();
    show();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN];
int n, m, mxtime = MAXN;
vector < int > h[MAXN];
vector < bool > mrk[MAXN];
deque < pair < int, int > > d;
void get()
{
  cin >> n >> m;

  for( int i = 1; i <= n; i++ )
  {
  	cin >> c[i];
	c[i] = '.' + c[i], c[i] += '.';
  }

  for( int i = 0; i <= n+1; i++ )
	h[i].resize(m+5), mrk[i].resize(m+5);

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

   return;
}

bool ok( int x, int y )
{
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 && c[x+i][y+j] == 'X' )
	    return 1;
   return 0;
}

void add_d()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	  if( c[i][j] == '.' && ok( i, j ) )
        d.push_back({i, j}), mrk[i][j] = 1;
   return;
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	  if( c[k.f][k.s] == '.' && c[k.f+i][k.s+j] )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	  }
	  else if( c[k.f][k.s] == 'X' )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   mxtime = max( mxtime, h[k.f+i][k.s+j]-1 );
	   d.push_back({k.f+i, k.s+j});
	  }
	}
	d.pop_front();
  }
}

void find_mxtime()
{
   for( int i = 0; i <= n+1; i++ )
      for( int j = 0; j <= m+1; j++ )
      {
         int cnt = 0;
         while( j <= m+1 && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && (cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   for( int i = 0; i <= m+1; i++ )
     for( int j = 0; j <= n+1; j++ )
     {
        int cnt = 0;
        while( j <= n+1 && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 &&(cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   return;
}

void show()
{
  cout << mxtime << endl;

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mxtime )
		cout << 'X';
	  else
		cout << '.';

	cout << endl;
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

  get();
  add_d();
  bfs();
  find_mxtime();
  show();

  return 0;
}


#define HOME
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define SZ(x) (int)x.size()
#define fi first
#define se second
#define mp make_pair

int n, m;
vector<string> a;
vector< vector<bool> > vis;
vector< vector<int> > s;

int sum_sq(int i, int j, int len) {
	if (i - len >= 0 && j - len >= 0)
		return s[i][j] - s[i - len][j] - s[i][j - len] + s[i - len][j - len];
	return -1;
}

const int dx[8] = {0, -1, -1, -1, 0, 1, 1, 1};
const int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

bool valid(int x, int y) {
	return x >= 0 && y >= 0 && x < n && y < m && a[x][y] == 'X';
}

bool check(const vector< pair<int, ii> > &rect_len_at, int mid) {
	queue<ii> q;
	for (int i = 0; i < n; ++i)
		fill(vis[i].begin(), vis[i].end(), false);

	for (int i = 0; i < SZ(rect_len_at); ++i) {
		if (rect_len_at[i].fi < mid) continue;
		q.push(rect_len_at[i].se);
		vis[rect_len_at[i].se.fi][rect_len_at[i].se.se] = true;
	}

	int tmp = mid;
	while (tmp--) {
		queue<ii> q2;
		while (SZ(q)) {
			int u = q.front().fi, v = q.front().se;
			q.pop();
			for (int i = 0; i < 8; ++i) {
				int x = u + dx[i], y = v + dy[i];
				if (!valid(x, y) || vis[x][y]) continue;

				vis[x][y] = true;
				q2.push(ii(x, y));
			}
		}
		q = q2;
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (!((vis[i][j] && a[i][j] == 'X') || (!vis[i][j] && a[i][j] == '.')))
				return false;
		}
	}
	return true;
}

void solve() {
	vector< pair<int, ii> > rect_len_at;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j) {
			if (a[i - 1][j - 1] == '.') continue;
			int lo = 0, hi = min(min(i - 1, j - 1), min(n - i, m - j)), ans = -1;
			while (lo <= hi) {
				int mid = (lo + hi) >> 1;
				if (sum_sq(i + mid, j + mid, mid*2 + 1) == (mid*2+1)*(mid*2+1))
					ans = mid, lo = mid + 1;
				else hi = mid - 1;
			}

			rect_len_at.push_back(mp(ans, ii(i - 1, j - 1)));
		}

	sort(rect_len_at.begin(), rect_len_at.end());

	//find max T
	int lo = 0, hi = rect_len_at.back().fi, ans = 0;
	while (lo <= hi) {
		int mid = (lo + hi) >> 1;
		if (check(rect_len_at, mid)) ans = mid, lo = mid + 1;
		else hi = mid - 1;
	}

	printf("%d\n", ans);
	vector<string> ans_str;
	ans_str.resize(n);
	for (int i = 0; i < n; ++i) {
		ans_str[i] = "";
		for (int j = 0; j < m; ++j)
			ans_str[i] = ans_str[i] + ".";
	}
	for (int i = 0; i < SZ(rect_len_at); ++i) {
		if (rect_len_at[i].fi < ans) continue;
		ans_str[rect_len_at[i].se.fi][rect_len_at[i].se.se] = 'X';
	}
	for (int i = 0; i < n; ++i)
		printf("%s\n", ans_str[i].c_str());
}

char tmp_str[(int)1e6+5];

int main() {
#undef HOME
#ifdef HOME
	freopen("input.txt", "rt", stdin);
#endif
	scanf("%d%d", &n, &m);
	a.resize(n);
	vis.resize(n);
	s.resize(n + 1);
	s[0].resize(m + 1);
	for (int i = 0; i < n; ++i) {
		scanf("%s", tmp_str);
		a[i] = "";
		vis[i].resize(m);
		s[i + 1].resize(m + 1);
		for (int j = 0; j < m; ++j) {
			a[i] = a[i] + tmp_str[j];
			s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + (a[i][j] == 'X');
		}
	}

	solve();
	return 0;
}


#define HOME
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define SZ(x) (int)x.size()
#define fi first
#define se second
#define mp make_pair

int n, m;
vector<string> a;
vector< vector<bool> > vis;
vector< vector<int> > s;

int sum_sq(int i, int j, int len) {
	if (i - len >= 0 && j - len >= 0)
		return s[i][j] - s[i - len][j] - s[i][j - len] + s[i - len][j - len];
	return -1;
}

const int dx[8] = {0, -1, -1, -1, 0, 1, 1, 1};
const int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

bool valid(int x, int y) {
	return x >= 0 && y >= 0 && x < n && y < m && a[x][y] == 'X';
}

bool check(const vector< pair<int, ii> > &rect_len_at, int mid) {
	queue<ii> q;
	for (int i = 0; i < n; ++i)
		fill(vis[i].begin(), vis[i].end(), false);

	for (int i = 0; i < SZ(rect_len_at); ++i) {
		if (rect_len_at[i].fi < mid) continue;
		q.push(rect_len_at[i].se);
		vis[rect_len_at[i].se.fi][rect_len_at[i].se.se] = true;
	}

	int tmp = mid;
	while (tmp--) {
		queue<ii> q2;
		while (SZ(q)) {
			int u = q.front().fi, v = q.front().se;
			q.pop();
			for (int i = 0; i < 8; ++i) {
				int x = u + dx[i], y = v + dy[i];
				if (!valid(x, y) || vis[x][y]) continue;

				vis[x][y] = true;
				q2.push(ii(x, y));
			}
		}
		q = q2;
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (!((vis[i][j] && a[i][j] == 'X') || (!vis[i][j] && a[i][j] == '.')))
				return false;
		}
	}
	return true;
}

void solve() {
	vector< pair<int, ii> > rect_len_at;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j) {
			if (a[i - 1][j - 1] == '.') continue;
			int lo = 0, hi = min(min(i - 1, j - 1), min(n - i, m - j)), ans = -1;
			while (lo <= hi) {
				int mid = (lo + hi) >> 1;
				if (sum_sq(i + mid, j + mid, mid*2 + 1) == (mid*2+1)*(mid*2+1))
					ans = mid, lo = mid + 1;
				else hi = mid - 1;
			}

			rect_len_at.push_back(mp(ans, ii(i - 1, j - 1)));
		}

	sort(rect_len_at.begin(), rect_len_at.end());

	//find max T
	int lo = 0, hi = rect_len_at.back().fi, ans = 0;
	while (lo <= hi) {
		int mid = (lo + hi) >> 1;
		if (check(rect_len_at, mid)) ans = mid, lo = mid + 1;
		else hi = mid - 1;
	}

	cout << ans << "\n";
	vector<string> ans_str;
	ans_str.resize(n);
	for (int i = 0; i < n; ++i) {
		ans_str[i] = "";
		for (int j = 0; j < m; ++j)
			ans_str[i] = ans_str[i] + ".";
	}
	for (int i = 0; i < SZ(rect_len_at); ++i) {
		if (rect_len_at[i].fi < ans) continue;
		ans_str[rect_len_at[i].se.fi][rect_len_at[i].se.se] = 'X';
	}
	for (int i = 0; i < n; ++i)
		cout << ans_str[i] << "\n";
}

int main() {
#undef HOME
#ifdef HOME
	freopen("input.txt", "rt", stdin);
#endif
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	a.resize(n);
	vis.resize(n);
	s.resize(n + 1);
	s[0].resize(m + 1);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		vis[i].resize(m);
		s[i + 1].resize(m + 1);
		for (int j = 0; j < m; ++j)
			s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + (a[i][j] == 'X');
	}

	solve();
	return 0;
}


#define HOME
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define SZ(x) (int)x.size()
#define fi first
#define se second
#define mp make_pair

int n, m;
vector< vector<char> > a;
vector< vector<bool> > vis;
vector< vector<int> > s;

int sum_sq(int i, int j, int len) {
	if (i - len >= 0 && j - len >= 0)
		return s[i][j] - s[i - len][j] - s[i][j - len] + s[i - len][j - len];
	return -1;
}

const int dx[8] = {0, -1, -1, -1, 0, 1, 1, 1};
const int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

bool valid(int x, int y) {
	return x >= 0 && y >= 0 && x < n && y < m && a[x][y] == 'X';
}

bool check(const vector< pair<int, ii> > &rect_len_at, int mid) {
	queue<ii> q;
	for (int i = 0; i < n; ++i)
		fill(vis[i].begin(), vis[i].end(), false);

	for (int i = 0; i < SZ(rect_len_at); ++i) {
		if (rect_len_at[i].fi < mid) continue;
		q.push(rect_len_at[i].se);
		vis[rect_len_at[i].se.fi][rect_len_at[i].se.se] = true;
	}

	int tmp = mid;
	while (tmp--) {
		queue<ii> q2;
		while (SZ(q)) {
			int u = q.front().fi, v = q.front().se;
			q.pop();
			for (int i = 0; i < 8; ++i) {
				int x = u + dx[i], y = v + dy[i];
				if (!valid(x, y) || vis[x][y]) continue;

				vis[x][y] = true;
				q2.push(ii(x, y));
			}
		}
		q = q2;
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (!((vis[i][j] && a[i][j] == 'X') || (!vis[i][j] && a[i][j] == '.')))
				return false;
		}
	}
	return true;
}

void solve() {
	vector< pair<int, ii> > rect_len_at;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j) {
			if (a[i - 1][j - 1] == '.') continue;
			int lo = 0, hi = min(min(i - 1, j - 1), min(n - i, m - j)), ans = -1;
			while (lo <= hi) {
				int mid = (lo + hi) >> 1;
				if (sum_sq(i + mid, j + mid, mid*2 + 1) == (mid*2+1)*(mid*2+1))
					ans = mid, lo = mid + 1;
				else hi = mid - 1;
			}

			rect_len_at.push_back(mp(ans, ii(i - 1, j - 1)));
		}

	sort(rect_len_at.begin(), rect_len_at.end());

	//find max T
	int lo = 0, hi = rect_len_at.back().fi, ans = 0;
	while (lo <= hi) {
		int mid = (lo + hi) >> 1;
		if (check(rect_len_at, mid)) ans = mid, lo = mid + 1;
		else hi = mid - 1;
	}

	printf("%d\n", ans);
	vector< vector<char> > ans_str;
	ans_str.resize(n);
	for (int i = 0; i < n; ++i) {
		ans_str[i].resize(m);
		for (int j = 0; j < m; ++j)
			ans_str[i][j] = '.';
	}
	for (int i = 0; i < SZ(rect_len_at); ++i) {
		if (rect_len_at[i].fi < ans) continue;
		ans_str[rect_len_at[i].se.fi][rect_len_at[i].se.se] = 'X';
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j)
			printf("%c", ans_str[i][j]);
		printf("\n");
	}
}

char tmp_str[(int)1e6+5];

int main() {
#undef HOME
#ifdef HOME
	freopen("input.txt", "rt", stdin);
	clock_t tStart = clock();
#endif
	scanf("%d%d", &n, &m);
	a.resize(n);
	vis.resize(n);
	s.resize(n + 1);
	s[0].resize(m + 1);
	for (int i = 0; i < n; ++i) {
		scanf("%s", tmp_str);
		a[i].resize(m);
		vis[i].resize(m);
		s[i + 1].resize(m + 1);
		for (int j = 0; j < m; ++j) {
			a[i][j] = tmp_str[j];
			s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + (a[i][j] == 'X');
		}
	}

	solve();
#ifdef HOME
	printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
#endif
	return 0;
}


#include "bits/stdc++.h"

using namespace std;

using P = pair<int, int>;
constexpr int inf = 1e9;

template <typename T>
void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, dy[8] = {1, 0, -1, -1, -1, 0, 1, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  queue<P> que;
  vector<string> s(n);
  vector<vector<bool>> ist(n, vector<bool>(m, false));
  vector<vector<int>> dist(n, vector<int>(m, inf));
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.') {
        ist[i][j] = true;
        dist[i][j] = 0;
        que.push(P(i, j));
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (ist[i][j]) continue;
      if (0 < i && i < n - 1 && 0 < j && j < m - 1) continue;
      dist[i][j] = 1;
      que.push(P(i, j));
    }
  }
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    for (int i = 0; i < 8; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx < 0 || n <= nx || ny < 0 || m <= ny) continue;
      if (dist[nx][ny] != inf) continue;
      dist[nx][ny] = dist[p.first][p.second] + 1;
      que.push(P(nx, ny));
    }
  }
  // for(int i=0;i<n;i++) printv(dist[i]);
  vector<vector<int>> dtmp(n, vector<int>(m, inf));
  int lb = 0, ub = 2000010;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (dist[i][j] <= mid) {
          dtmp[i][j] = inf;
        } else {
          dtmp[i][j] = 0;
          que.push(P(i, j));
        }
      }
    }
    bool f = true;
    while (!que.empty()) {
      auto p = que.front();
      que.pop();
      if (ist[p.first][p.second]) {
        f = false;
        break;
      }
      if (dtmp[p.first][p.second] >= mid) continue;
      for (int i = 0; i < 8; i++) {
        int nx = p.first + dx[i], ny = p.second + dy[i];
        if (nx < 0 || n <= nx || ny < 0 || m <= ny) continue;
        if (dtmp[nx][ny] != inf) continue;
        dtmp[nx][ny] = dtmp[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
    }
    while (!que.empty()) que.pop();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        // tmp[i][j] == '.' <-> dtmp[i][j] = inf
        if (dtmp[i][j] == inf && !ist[i][j]) f = false;
      }
    }
    // cout << mid << endl;
    // for (int i = 0; i < n; i++) tmp[i];
    if (f)
      lb = mid;
    else
      ub = mid;
  }
  cout << lb << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[i][j] <= lb) {
        cout << '.';
        continue;
      }
      cout << 'X';
    }
    cout << '\n';
  }
}


#include "bits/stdc++.h"

using namespace std;

using P = pair<int, int>;
using ll = long long;
constexpr ll inf = 1e9;

template <typename T>
void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, dy[8] = {1, 0, -1, -1, -1, 0, 1, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  queue<P> que;
  vector<string> s(n);
  vector<vector<int>> dist(n, vector<int>(m, inf));
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.') {
        dist[i][j] = 0;
        que.push(P(i, j));
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.') continue;
      if (0 < i && i < n - 1 && 0 < j && j < m - 1) continue;
      dist[i][j] = 1;
      que.push(P(i, j));
    }
  }
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    for (int i = 0; i < 8; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx < 0 || n <= nx || ny < 0 || m <= ny) continue;
      if (dist[nx][ny] != inf) continue;
      dist[nx][ny] = dist[p.first][p.second] + 1;
      que.push(P(nx, ny));
    }
  }
  // for(int i=0;i<n;i++) printv(dist[i]);
  vector<vector<int>> dtmp(n, vector<int>(m, inf));
  vector<string> tmp(n, string(m, '.'));
  int lb = 0, ub = 2000010;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        dtmp[i][j] = inf;
        if (dist[i][j] <= mid)
          tmp[i][j] = '.';
        else {
          tmp[i][j] = 'X';
          dtmp[i][j] = 0;
          que.push(P(i, j));
        }
      }
    }
    while (!que.empty()) {
      auto p = que.front();
      que.pop();
      if (dtmp[p.first][p.second] >= mid) continue;
      for (int i = 0; i < 8; i++) {
        int nx = p.first + dx[i], ny = p.second + dy[i];
        if (nx < 0 || n <= nx || ny < 0 || m <= ny) continue;
        if (dtmp[nx][ny] != inf) continue;
        tmp[nx][ny] = 'X';
        dtmp[nx][ny] = dtmp[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
    }
    bool f = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (tmp[i][j] != s[i][j]) f = false;
      }
    }
    //cout << mid << endl;
    //for (int i = 0; i < n; i++) tmp[i];
    if (f)
      lb = mid;
    else
      ub = mid;
  }
  cout << lb << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[i][j] <= lb) {
        cout << '.';
        continue;
      }
      cout << 'X';
    }
    cout << endl;
  }
}


#include "bits/stdc++.h"

using namespace std;

using P = pair<int, int>;
using ll = long long;
constexpr ll inf = 1e9;

template <typename T>
void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, dy[8] = {1, 0, -1, -1, -1, 0, 1, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  queue<P> que;
  vector<string> s(n);
  vector<vector<int>> dist(n, vector<int>(m, inf));
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.') {
        dist[i][j] = 0;
        que.push(P(i, j));
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.') continue;
      if (0 < i && i < n - 1 && 0 < j && j < m - 1) continue;
      dist[i][j] = 1;
      que.push(P(i, j));
    }
  }
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    for (int i = 0; i < 8; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx < 0 || n <= nx || ny < 0 || m <= ny) continue;
      if (dist[nx][ny] != inf) continue;
      dist[nx][ny] = dist[p.first][p.second] + 1;
      que.push(P(nx, ny));
    }
  }
  // for(int i=0;i<n;i++) printv(dist[i]);
  vector<vector<int>> dtmp(n, vector<int>(m, inf));
  int lb = 0, ub = 2000010;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (dist[i][j] <= mid) {
          dtmp[i][j] = inf;
        } else {
          dtmp[i][j] = 0;
          que.push(P(i, j));
        }
      }
    }
    while (!que.empty()) {
      auto p = que.front();
      que.pop();
      if (dtmp[p.first][p.second] >= mid) continue;
      for (int i = 0; i < 8; i++) {
        int nx = p.first + dx[i], ny = p.second + dy[i];
        if (nx < 0 || n <= nx || ny < 0 || m <= ny) continue;
        if (dtmp[nx][ny] != inf) continue;
        dtmp[nx][ny] = dtmp[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
    }
    bool f = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        // tmp[i][j] == '.' <-> dtmp[i][j] = inf
        if (dtmp[i][j] == inf && s[i][j] == 'X') f = false;
        if (dtmp[i][j] != inf && s[i][j] == '.') f = false;
      }
    }
    // cout << mid << endl;
    // for (int i = 0; i < n; i++) tmp[i];
    if (f)
      lb = mid;
    else
      ub = mid;
  }
  cout << lb << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[i][j] <= lb) {
        cout << '.';
        continue;
      }
      cout << 'X';
    }
    cout << endl;
  }
}


//aea ctmr
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define f(i,a,b) for( ll i = a; i < b ; i++ )
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define PI 3.14159265358979323846

typedef long double ld;
typedef long long ll;
//que mrd es esto? :v
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const ll N = 105000;
const int mod = 1000000007;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << endl;}

template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1<<" | ";
  __f(comma+1, args...);}
#else
#define trace(...)
#endif

vector <vector <char>> tab;
vector <vector <int>> acum;
int n, m, l;
//robado gaea
bool cumple(int t){

	vector <vector <int>> uwu;
	uwu.assign(n + 2, vector <int>(m + 2, 0));

	for(int i = 1; i + t - 1 <= n; i++){
		for(int j = 1; j + t - 1 <= m; j++){
			int x1 = i; int x2 = i + t - 1;
			int y1 = j; int y2 = j + t - 1;

			if(acum[x2][y2] + acum[x1 - 1][y1 - 1] - acum[x1 - 1][y2] - acum[x2][y1 - 1] == 0){
				uwu[x1][y1]++;
				uwu[x2 + 1][y1]--;
				uwu[x1][y2 + 1]--;
				uwu[x2 + 1][y2 + 1]++;
			}
		}
	}

	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			uwu[i][j] += uwu[i][j - 1] + uwu[i - 1][j] - uwu[i - 1][j - 1];
			if(tab[i][j] == 'X' and uwu[i][j] == 0) return 0;
		}
	}

	return 1;
}
//
void wea(int t){
	for(int i = 0; i < n + 2; i++){
		int las = 0;

		for(int j = 0; j < m + 2; j++){
			if(tab[i][j] == '.') {
				las = j;
				tab[i][j] = '.';
			}
			else{
				if(j - las <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea4(int t){
	for(int i = 0; i < n + 2; i++){
		int las = 0;

		for(int j = m + 1; j >= 0; j--){
			if(tab[i][j] == '.') {
				las = j;
				tab[i][j] = '.';
			}
			else{
				if(las - j <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea2(int t){
	for(int j = 0; j < m + 2; j++){
		int las = 0;

		for(int i = 0; i < n + 2; i++){
			if(tab[i][j] == '.') {
				las = i;
				tab[i][j] = '.';
			}
			else{
				if(i - las <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea3(int t){
	for(int j = 0; j < m + 2; j++){
		int las = 0;

		for(int i = n + 1; i >= 0; i--){
			if(tab[i][j] == '.') {
				las = i;
				tab[i][j] = '.';
			}
			else{
				if(las - i <= t) tab[i][j] = '.';
			}
		}
	}
}

void prin_tab(){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			printf("%c", tab[i][j]);
		}
		printf("\n");
	}
}
int main(){

	scanf("%d %d", &n, &m);
	vector <char> aea; aea.clear();
	vector <char> aea2; aea2.clear();
	acum.assign(n + 2, vector <int>(m + 2, 0));
	for(int i = 0; i < m + 2; i++){aea.push_back('.'); aea2.push_back('X');}
	for(int i = 0; i < n + 2; i++){
		tab.push_back(aea);
	}

	for(int i = 0; i < n; i++){
		char s[m + 2];
		scanf("%s", s);
		for(int j = 0; j < m; j++) tab[i + 1][j + 1] = s[j];
	}
	/*
	for(int i = 0; i < n + 2; i++){
		for(int j = 0; j < m + 2; j++)
			cout << tab[i][j];
		cout << endl;
	}
	*/
	//robado uwu :v
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			acum[i][j] = acum[i - 1][j] + acum[i][j - 1] - acum[i - 1][j - 1];
			acum[i][j] += (tab[i][j] == '.');
		}
	}
	//
	l = 0;
	int r = min(n, m);

	while(l < r){
		int mid = (l + r + 1) / 2;
		//trace(mid);
		if(cumple(2 * mid + 1)) l = mid;
		else r = mid - 1;
	}
	//la respuesta se guarda en l
	printf("%d\n", l);
	wea(l); wea4(l);
	wea2(l); wea3(l);
	prin_tab();

	return 0;
}


//aea ctmr

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define f(i,a,b) for( ll i = a; i < b ; i++ )
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define PI 3.14159265358979323846

typedef long double ld;
typedef long long ll;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const ll N = 105000;
const int mod = 1000000007;

//que mrd es esto? :v
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << endl;}

template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1<<" | ";
  __f(comma+1, args...);}
#else
#define trace(...)
#endif

vector <vector <char>> tab;
int n, m, l;

bool cumple(int t){
	int acum;
	for(int i = 0; i < n + 2; i++){
		acum = 0;
		for(int j = 0; j < m + 2; j++){
			if(tab[i][j] == 'X') acum++;
			else{
				if(acum > 0 and acum < 2 * t + 1) return false;
				acum = 0;
			}
		}
	}
	for(int j = 0; j < m + 2; j++){
		acum = 0;
		for(int i = 0; i < n + 2; i++){
			if(tab[i][j] == 'X') acum++;
			else{
				if(acum > 0 and acum < 2 * t + 1) return false;
				acum = 0;
			}
		}
	}

	return true;
}

void wea(int t){
	for(int i = 0; i < n + 2; i++){
		int las = 0;

		for(int j = 0; j < m + 2; j++){
			if(tab[i][j] == '.') {
				las = j;
				tab[i][j] = '.';
			}
			else{
				if(j - las <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea4(int t){
	for(int i = 0; i < n + 2; i++){
		int las = 0;

		for(int j = m + 1; j >= 0; j--){
			if(tab[i][j] == '.') {
				las = j;
				tab[i][j] = '.';
			}
			else{
				if(las - j <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea2(int t){
	for(int j = 0; j < m + 2; j++){
		int las = 0;

		for(int i = 0; i < n + 2; i++){
			if(tab[i][j] == '.') {
				las = i;
				tab[i][j] = '.';
			}
			else{
				if(i - las <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea3(int t){
	for(int j = 0; j < m + 2; j++){
		int las = 0;

		for(int i = n + 1; i >= 0; i--){
			if(tab[i][j] == '.') {
				las = i;
				tab[i][j] = '.';
			}
			else{
				if(las - i <= t) tab[i][j] = '.';
			}
		}
	}
}

void prin_tab(){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cout << tab[i][j];
		}
		cout << endl;
	}
}
int main(){

	fastio;
	cin >> n >> m;
	vector <char> aea; aea.clear();
	vector <char> aea2; aea2.clear();
	for(int i = 0; i < m + 2; i++) {aea.push_back('.'); aea2.push_back('X');}
	for(int i = 0; i < n + 2; i++){
		tab.push_back(aea);
	}

	for(int i = 0; i < n; i++){
		string s; int l = s.size();
		cin >> s;
		for(int j = 0; j < m; j++) tab[i + 1][j + 1] = s[j];
	}
	/*
	for(int i = 0; i < n + 2; i++){
		for(int j = 0; j < m + 2; j++)
			cout << tab[i][j];
		cout << endl;
	}
	*/

	l = 0;
	int r = min(n, m);

	while(l < r){
		int mid = (l + r + 1) / 2;
		//trace(mid);
		if(cumple(mid)) l = mid;
		else r = mid - 1;
	}
	//la respuesta se guarda en l
	cout << l << endl;
	wea(l); wea4(l);
	wea2(l); wea3(l);
	prin_tab();

	return 0;
}


//aea ctmr
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define f(i,a,b) for( ll i = a; i < b ; i++ )
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define PI 3.14159265358979323846

typedef long double ld;
typedef long long ll;
//que mrd es esto? :v
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const ll N = 105000;
const int mod = 1000000007;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << endl;}

template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1<<" | ";
  __f(comma+1, args...);}
#else
#define trace(...)
#endif

vector <vector <char>> tab;
int n, m, l;

bool cumple(int t){
	int acum;
	for(int i = 0; i < n + 2; i++){
		acum = 0;
		for(int j = 0; j < m + 2; j++){
			if(tab[i][j] == 'X') acum++;
			else{
				if(acum > 0 and acum < 2 * t + 1) return false;
				acum = 0;
			}
		}
	}
	for(int j = 0; j < m + 2; j++){
		acum = 0;
		for(int i = 0; i < n + 2; i++){
			if(tab[i][j] == 'X') acum++;
			else{
				if(acum > 0 and acum < 2 * t + 1) return false;
				acum = 0;
			}
		}
	}

	return true;
}

void wea(int t){
	for(int i = 0; i < n + 2; i++){
		int las = 0;

		for(int j = 0; j < m + 2; j++){
			if(tab[i][j] == '.') {
				las = j;
				tab[i][j] = '.';
			}
			else{
				if(j - las <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea4(int t){
	for(int i = 0; i < n + 2; i++){
		int las = 0;

		for(int j = m + 1; j >= 0; j--){
			if(tab[i][j] == '.') {
				las = j;
				tab[i][j] = '.';
			}
			else{
				if(las - j <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea2(int t){
	for(int j = 0; j < m + 2; j++){
		int las = 0;

		for(int i = 0; i < n + 2; i++){
			if(tab[i][j] == '.') {
				las = i;
				tab[i][j] = '.';
			}
			else{
				if(i - las <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea3(int t){
	for(int j = 0; j < m + 2; j++){
		int las = 0;

		for(int i = n + 1; i >= 0; i--){
			if(tab[i][j] == '.') {
				las = i;
				tab[i][j] = '.';
			}
			else{
				if(las - i <= t) tab[i][j] = '.';
			}
		}
	}
}

void prin_tab(){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			printf("%c", tab[i][j]);
		}
		printf("\n");
	}
}
int main(){

	scanf("%d %d", &n, &m);
	vector <char> aea; aea.clear();
	vector <char> aea2; aea2.clear();
	for(int i = 0; i < m + 2; i++){aea.push_back('.'); aea2.push_back('X');}
	for(int i = 0; i < n + 2; i++){
		tab.push_back(aea);
	}

	for(int i = 0; i < n; i++){
		char s[m + 2];
		scanf("%s", s);
		for(int j = 0; j < m; j++) tab[i + 1][j + 1] = s[j];
	}
	/*
	for(int i = 0; i < n + 2; i++){
		for(int j = 0; j < m + 2; j++)
			cout << tab[i][j];
		cout << endl;
	}
	*/

	l = 0;
	int r = min(n, m);

	while(l < r){
		int mid = (l + r + 1) / 2;
		//trace(mid);
		if(cumple(mid)) l = mid;
		else r = mid - 1;
	}
	//la respuesta se guarda en l
	printf("%d", l);
	wea(l); wea4(l);
	wea2(l); wea3(l);
	prin_tab();

	return 0;
}


//aea ctmr
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define f(i,a,b) for( ll i = a; i < b ; i++ )
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define PI 3.14159265358979323846

typedef long double ld;
typedef long long ll;
//que mrd es esto? :v
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const ll N = 105000;
const int mod = 1000000007;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << endl;}

template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1<<" | ";
  __f(comma+1, args...);}
#else
#define trace(...)
#endif

vector <vector <char>> tab;
int n, m, l;

bool cumple(int t){
	int acum;
	for(int i = 0; i < n + 2; i++){
		acum = 0;
		for(int j = 0; j < m + 2; j++){
			if(tab[i][j] == 'X') acum++;
			else{
				if(acum > 0 and acum < 2 * t + 1) return false;
				acum = 0;
			}
		}
	}
	for(int j = 0; j < m + 2; j++){
		acum = 0;
		for(int i = 0; i < n + 2; i++){
			if(tab[i][j] == 'X') acum++;
			else{
				if(acum > 0 and acum < 2 * t + 1) return false;
				acum = 0;
			}
		}
	}

	return true;
}

void wea(int t){
	for(int i = 0; i < n + 2; i++){
		int las = 0;

		for(int j = 0; j < m + 2; j++){
			if(tab[i][j] == '.') {
				las = j;
				tab[i][j] = '.';
			}
			else{
				if(j - las <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea4(int t){
	for(int i = 0; i < n + 2; i++){
		int las = 0;

		for(int j = m + 1; j >= 0; j--){
			if(tab[i][j] == '.') {
				las = j;
				tab[i][j] = '.';
			}
			else{
				if(las - j <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea2(int t){
	for(int j = 0; j < m + 2; j++){
		int las = 0;

		for(int i = 0; i < n + 2; i++){
			if(tab[i][j] == '.') {
				las = i;
				tab[i][j] = '.';
			}
			else{
				if(i - las <= t) tab[i][j] = '.';
			}
		}
	}
}

void wea3(int t){
	for(int j = 0; j < m + 2; j++){
		int las = 0;

		for(int i = n + 1; i >= 0; i--){
			if(tab[i][j] == '.') {
				las = i;
				tab[i][j] = '.';
			}
			else{
				if(las - i <= t) tab[i][j] = '.';
			}
		}
	}
}

void prin_tab(){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			printf("%c", tab[i][j]);
		}
		printf("\n");
	}
}
int main(){

	scanf("%d %d", &n, &m);
	vector <char> aea; aea.clear();
	vector <char> aea2; aea2.clear();
	for(int i = 0; i < m + 2; i++){aea.push_back('.'); aea2.push_back('X');}
	for(int i = 0; i < n + 2; i++){
		tab.push_back(aea);
	}

	for(int i = 0; i < n; i++){
		char s[m + 2];
		scanf("%s", s);
		for(int j = 0; j < m; j++) tab[i + 1][j + 1] = s[j];
	}
	/*
	for(int i = 0; i < n + 2; i++){
		for(int j = 0; j < m + 2; j++)
			cout << tab[i][j];
		cout << endl;
	}
	*/

	l = 0;
	int r = min(n, m);

	while(l < r){
		int mid = (l + r + 1) / 2;
		//trace(mid);
		if(cumple(mid)) l = mid;
		else r = mid - 1;
	}
	//la respuesta se guarda en l
	printf("%d\n", l);
	wea(l); wea4(l);
	wea2(l); wea3(l);
	prin_tab();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n, m; cin >> n >> m;
	vector<vector<char>> c(n+2, vector<char>(m+2, '.'));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> c[i][j];
		}
	}

	vector<vector<char>> p = c;
	vector<pair<int, int>> gt;
	for (int x = 1; x <= n; x++) {
		for (int y = 1; y <= m; y++) {
			if (c[x][y] == '.') continue;
			bool fff = 0;
			for (int dx = -1; dx <= 1; dx++) {
				for (int dy = -1; dy <= 1; dy++) {
					int i = x + dx, j = y + dy;

					if (c[i][j] == '.') {
						fff = 1; break;
					}
				}
				if (fff) break;
			}

			if (fff) gt.push_back({x, y});
		}
	}

	int t = -1;
	while (1) {
		t++;
		for (auto [x, y]: gt) {
			p[x][y] = '.';
		}

		int nos = 0;
		set<pair<int, int>> nxt;
		for (auto [x, y]: gt) {
			bool fff = 0;
			for (int dx = -1; dx <= 1; dx++) {
				for (int dy = -1; dy <= 1; dy++) {
					int i = x + dx, j = y + dy;

					if (p[i][j] == 'X') {
						fff = 1; nxt.insert({i, j});
					}
				}
				if (fff) break;
			}

			if (!fff) {nos = 1; break;}
		}

		if (nos) break;
		gt = vector<pair<int, int>>(nxt.begin(), nxt.end());
	}

	for (auto [x, y]: gt) {
		p[x][y] = 'X';
	}

	cout << t << '\n';
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cout << p[i][j];
		}
		cout << '\n';
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

bool check(int f, vector<vector<int>> &dist, int n, int m) {
	vector<vector<int>> d(n+2, vector<int>(m+2, 0));
	queue<pair<int, int>> bfs;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (dist[i][j] >= f) {
				bfs.push({i, j});
				d[i][j] = f;
			}
		}
	}

	while (bfs.size()) {
		auto [x, y] = bfs.front();
		bfs.pop();

		if (d[x][y] <= 1) continue;

		for (int dx = -1; dx <= 1; dx++) {
			for (int dy = -1; dy <= 1; dy++) {
				int i = x + dx, j = y + dy;

				if (i > 0 && i <= n && j > 0 && j <= m) {
					if (d[i][j] == 0) {
						bfs.push({i, j});
						d[i][j] = d[x][y] - 1;
					}
				} else {
					return 0;
				}
			}
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (dist[i][j] == 0) {
				if (d[i][j] != 0) return 0;
			}
			if (d[i][j] == 0) {
				if (dist[i][j] != 0) return 0;
			}
			if (dist[i][j] < d[i][j]) return 0;
		}
	}
	return 1;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n, m; cin >> n >> m;
	vector<vector<char>> c(n+2, vector<char>(m+2, '.'));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> c[i][j];
		}
	}

	vector<vector<int>> dist(n+2, vector<int>(m+2, 998244353));
	queue<pair<int, int>> bfs;
	for (int i = 0; i <= n+1; i++) {
		for (int j = 0; j <= m+1; j++) {
			if (c[i][j] == '.') {
				bfs.push({i, j});
				dist[i][j] = 0;
			}
		}
	}

	while (bfs.size()) {
		auto [x, y] = bfs.front();
		bfs.pop();

		for (int dx = -1; dx <= 1; dx++) {
			for (int dy = -1; dy <= 1; dy++) {
				int i = x + dx, j = y + dy;

				if (i > 0 && i <= n && j > 0 && j <= m) {
					if (dist[i][j] > dist[x][y] + 1) {
						bfs.push({i, j});
						dist[i][j] = dist[x][y] + 1;
					}
				}
			}
		}
	}

	int lo = 0, hi = min((n+1)/2, (m+1)/2);
	while (lo < hi) {
		int mid = (lo + hi + 1)/2;

		if (check(mid, dist, n, m)) {
			lo = mid;
		} else {
			hi = mid-1;
		}
	}
	cout << lo-1 << '\n';
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (dist[i][j] >= lo) {
				cout << 'X';
			} else {
				cout << '.';
			}
		}
		cout << '\n';
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

bool check(int f, vector<vector<int>> dist, int n, int m) {
	vector<vector<int>> d(n+2, vector<int>(m+2, 0));
	queue<pair<int, int>> bfs;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (dist[i][j] >= f) {
				bfs.push({i, j});
				d[i][j] = f;
			}
		}
	}

	while (bfs.size()) {
		auto [x, y] = bfs.front();
		bfs.pop();

		if (d[x][y] <= 1) continue;

		for (int dx = -1; dx <= 1; dx++) {
			for (int dy = -1; dy <= 1; dy++) {
				int i = x + dx, j = y + dy;

				if (i > 0 && i <= n && j > 0 && j <= m) {
					if (d[i][j] == 0) {
						bfs.push({i, j});
						d[i][j] = d[x][y] - 1;
					}
				} else {
					return 0;
				}
			}
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (dist[i][j] == 0) {
				if (d[i][j] != 0) return 0;
			}
			if (d[i][j] == 0) {
				if (dist[i][j] != 0) return 0;
			}
			if (dist[i][j] < d[i][j]) return 0;
		}
	}
	return 1;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n, m; cin >> n >> m;
	vector<vector<char>> c(n+2, vector<char>(m+2, '.'));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> c[i][j];
		}
	}

	vector<vector<int>> dist(n+2, vector<int>(m+2, 998244353));
	queue<pair<int, int>> bfs;
	for (int i = 0; i <= n+1; i++) {
		for (int j = 0; j <= m+1; j++) {
			if (c[i][j] == '.') {
				bfs.push({i, j});
				dist[i][j] = 0;
			}
		}
	}

	while (bfs.size()) {
		auto [x, y] = bfs.front();
		bfs.pop();

		for (int dx = -1; dx <= 1; dx++) {
			for (int dy = -1; dy <= 1; dy++) {
				int i = x + dx, j = y + dy;

				if (i > 0 && i <= n && j > 0 && j <= m) {
					if (dist[i][j] > dist[x][y] + 1) {
						bfs.push({i, j});
						dist[i][j] = dist[x][y] + 1;
					}
				}
			}
		}
	}

	int lo = 0, hi = max(n, m);
	while (lo < hi) {
		int mid = (lo + hi + 1)/2;

		if (check(mid, dist, n, m)) {
			lo = mid;
		} else {
			hi = mid-1;
		}
	}
	cout << lo-1 << '\n';
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (dist[i][j] >= lo) {
				cout << 'X';
			} else {
				cout << '.';
			}
		}
		cout << '\n';
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int dfs(int x, int y, vector<vector<int>> &vis, vector<vector<char>> &c, vector<vector<int>> &dist, int &n, int &m) {
	vis[x][y] = 1;
	int ans = dist[x][y];

	for (int dx = -1; dx <= 1; dx++) {
		for (int dy = -1; dy <= 1; dy++) {
			int i = x + dx, j = y + dy;

			if (i > 0 && i <= n && j > 0 && j <= m) {
				if (vis[i][j] == 0 && c[i][j] == 'X') {
					ans = max(ans, dfs(i, j, vis, c, dist, n, m));
				}
			}
		}
	}
	return ans;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n, m; cin >> n >> m;
	vector<vector<char>> c(n+2, vector<char>(m+2, '.'));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> c[i][j];
		}
	}

	vector<vector<int>> dist(n+2, vector<int>(m+2, 998244353));
	queue<pair<int, int>> bfs;
	for (int i = 0; i <= n+1; i++) {
		for (int j = 0; j <= m+1; j++) {
			if (c[i][j] == '.') {
				bfs.push({i, j});
				dist[i][j] = 0;
			}
		}
	}

	while (bfs.size()) {
		auto [x, y] = bfs.front();
		bfs.pop();

		for (int dx = -1; dx <= 1; dx++) {
			for (int dy = -1; dy <= 1; dy++) {
				int i = x + dx, j = y + dy;

				if (i > 0 && i <= n && j > 0 && j <= m) {
					if (dist[i][j] > dist[x][y] + 1) {
						bfs.push({i, j});
						dist[i][j] = dist[x][y] + 1;
					}
				}
			}
		}
	}

	// find the connected components
	vector<int> conn;
	vector<vector<int>> vis(n+2, vector<int>(m+2));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (vis[i][j] == 0 && c[i][j] == 'X') {
				conn.push_back(dfs(i, j, vis, c, dist, n, m));
			}
		}
	}

	int res = *min_element(conn.begin(), conn.end());
	cout << res-1 << '\n';
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (dist[i][j] >= res) {
				cout << 'X';
			} else {
				cout << '.';
			}
		}
		cout << '\n';
	}

	return 0;
}


//#pragma comment(linker, "/STACK:1000000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <random>
#include <bitset>
#include <cstring>
#include <memory.h>
#include <cassert>

#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define pll pair <ll, ll>
#define ld double

using namespace std;

int main() {
	int n, m;

	cin >> n >> m;

	vector <vector <char> > c(n, vector <char>(m));

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf(" %c", &c[i][j]);
		}
	}

	queue <pii> q;

	for (int i = 0; i < n; i++) {
		q.push(mp(i, -1));
		q.push(mp(i, m));
	}

	for (int i = 0; i < m; i++) {
		q.push(mp(-1, i));
		q.push(mp(n, i));
	}

	vector <vector <int> > dst(n, vector <int>(m, -1));

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (c[i][j] == '.') {
				q.push(mp(i, j));
				dst[i][j] = 0;
			}
		}
	}

	int maxd = 0;

	while (!q.empty()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();

		int d = 0;

		if (0 <= x && x < n && 0 <= y && y < m) {
			d = dst[x][y];
			if (c[x][y] == 'X') {
				maxd = max(maxd, d);
			}
		}

		for (int dx = -1; dx <= 1; dx++) {
			for (int dy = -1; dy <= 1; dy++) {
				if (dx == 0 && dy == 0) {
					continue;
				}

				int nx = x + dx;
				int ny = y + dy;

				if (0 <= nx && nx < n && 0 <= ny && ny < m && dst[nx][ny] == -1) {
					dst[nx][ny] = d + 1;
					q.push(mp(nx, ny));
				}
			}
		}
	}

	int l = 1;
	int r = maxd;

	while (l != r) {
		int md = (l + r + 1) >> 1;

		queue <pii> q;

		vector <vector <int> > ds(n, vector <int>(m, -1));

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (c[i][j] == 'X' && dst[i][j] >= md) {
					q.push(mp(i, j));
					ds[i][j] = 0;
				}
			}
		}

		bool ok = true;

		while (!q.empty()) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();

			int d = ds[x][y];

			if (c[x][y] == 'X' && d > md - 1) {
				ok = false;
				break;
			}

			for (int dx = -1; dx <= 1; dx++) {
				for (int dy = -1; dy <= 1; dy++) {
					if (dx == 0 && dy == 0) {
						continue;
					}

					int nx = x + dx;
					int ny = y + dy;

					if (0 <= nx && nx < n && 0 <= ny && ny < m && ds[nx][ny] == -1) {
						ds[nx][ny] = d + 1;
						q.push(mp(nx, ny));
					}
				}
			}
		}

		if (ok) {
			l = md;
		}
		else {
			r = md - 1;
		}
	}

	cout << l - 1 << endl;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (c[i][j] == '.' || dst[i][j] < l) {
				printf(".");
			}
			else {
				printf("X");
			}
		}
		printf("\n");
	}

	//system("pause");
}


//#pragma comment(linker, "/STACK:1000000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <random>
#include <bitset>
#include <cstring>
#include <memory.h>
#include <cassert>

#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define pll pair <ll, ll>
#define ld double

using namespace std;

int main() {
	int n, m;

	cin >> n >> m;

	vector <vector <char> > c(n, vector <char>(m));

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf(" %c", &c[i][j]);
		}
	}

	queue <pii> q;

	for (int i = 0; i < n; i++) {
		q.push(mp(i, -1));
		q.push(mp(i, m));
	}

	for (int i = 0; i < m; i++) {
		q.push(mp(-1, i));
		q.push(mp(n, i));
	}

	vector <vector <int> > dst(n, vector <int>(m, -1));

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (c[i][j] == '.') {
				q.push(mp(i, j));
				dst[i][j] = 0;
			}
		}
	}

	int maxd = 0;

	while (!q.empty()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();

		int d = 0;

		if (0 <= x && x < n && 0 <= y && y < m) {
			d = dst[x][y];

			maxd = max(maxd, d);
		}

		for (int dx = -1; dx <= 1; dx++) {
			for (int dy = -1; dy <= 1; dy++) {
				if (dx == 0 && dy == 0) {
					continue;
				}

				int nx = x + dx;
				int ny = y + dy;

				if (0 <= nx && nx < n && 0 <= ny && ny < m && dst[nx][ny] == -1) {
					dst[nx][ny] = d + 1;
					q.push(mp(nx, ny));
				}
			}
		}
	}

	int l = 1;
	int r = maxd;

	while (l != r) {
		int md = (l + r + 1) >> 1;

		queue <pii> q;

		vector <vector <int> > ds(n, vector <int>(m, -1));

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (c[i][j] == 'X' && dst[i][j] >= md) {
					q.push(mp(i, j));
					ds[i][j] = 0;
				}
			}
		}

		bool ok = true;

		while (!q.empty()) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();

			int d = ds[x][y];

			if (c[x][y] == 'X' && d > md) {
				ok = false;
				break;
			}

			for (int dx = -1; dx <= 1; dx++) {
				for (int dy = -1; dy <= 1; dy++) {
					if (dx == 0 && dy == 0) {
						continue;
					}

					int nx = x + dx;
					int ny = y + dy;

					if (0 <= nx && nx < n && 0 <= ny && ny < m && ds[nx][ny] == -1) {
						ds[nx][ny] = d + 1;
						q.push(mp(nx, ny));
					}
				}
			}
		}

		if (ok) {
			l = md;
		}
		else {
			r = md - 1;
		}
	}

	cout << l - 1 << endl;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (c[i][j] == '.' || dst[i][j] < l) {
				printf(".");
			}
			else {
				printf("X");
			}
		}
		printf("\n");
	}

	//system("pause");
}


//#pragma comment(linker, "/STACK:1000000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <random>
#include <bitset>
#include <cstring>
#include <memory.h>
#include <cassert>

#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define pll pair <ll, ll>
#define ld double

using namespace std;

int main() {
	int n, m;

	cin >> n >> m;

	vector <vector <char> > c(n, vector <char>(m));

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf(" %c", &c[i][j]);
		}
	}

	queue <pii> q;

	for (int i = 0; i < n; i++) {
		q.push(mp(i, -1));
		q.push(mp(i, m));
	}

	for (int i = 0; i < m; i++) {
		q.push(mp(-1, i));
		q.push(mp(n, i));
	}

	vector <vector <int> > dst(n, vector <int>(m, -1));

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (c[i][j] == '.') {
				q.push(mp(i, j));
				dst[i][j] = 0;
			}
		}
	}

	int maxd = 0;

	while (!q.empty()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();

		int d = 0;

		if (0 <= x && x < n && 0 <= y && y < m) {
			d = dst[x][y];
			if (c[x][y] == 'X') {
				maxd = max(maxd, d);
			}
		}

		for (int dx = -1; dx <= 1; dx++) {
			for (int dy = -1; dy <= 1; dy++) {
				if (dx == 0 && dy == 0) {
					continue;
				}

				int nx = x + dx;
				int ny = y + dy;

				if (0 <= nx && nx < n && 0 <= ny && ny < m && dst[nx][ny] == -1) {
					dst[nx][ny] = d + 1;
					q.push(mp(nx, ny));
				}
			}
		}
	}

	int l = 1;
	int r = maxd;

	while (l != r) {
		int md = (l + r + 1) >> 1;

		queue <pii> q;

		vector <vector <int> > ds(n, vector <int>(m, -1));

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (c[i][j] == 'X' && dst[i][j] >= md) {
					q.push(mp(i, j));
					ds[i][j] = 0;
				}
			}
		}

		bool ok = true;

		while (!q.empty()) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();

			int d = ds[x][y];

			if (c[x][y] == 'X' && d > md) {
				ok = false;
				break;
			}

			for (int dx = -1; dx <= 1; dx++) {
				for (int dy = -1; dy <= 1; dy++) {
					if (dx == 0 && dy == 0) {
						continue;
					}

					int nx = x + dx;
					int ny = y + dy;

					if (0 <= nx && nx < n && 0 <= ny && ny < m && ds[nx][ny] == -1) {
						ds[nx][ny] = d + 1;
						q.push(mp(nx, ny));
					}
				}
			}
		}

		if (ok) {
			l = md;
		}
		else {
			r = md - 1;
		}
	}

	cout << l - 1 << endl;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (c[i][j] == '.' || dst[i][j] < l) {
				printf(".");
			}
			else {
				printf("X");
			}
		}
		printf("\n");
	}

	//system("pause");
}


#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

class DebugStream {}LOG;
template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; }
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29;

int n, m, dr[8][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};

bool ins(int x, int y) {
	return x >= 0 && y >= 0 && x < n && y < m;
}

vector<string> vlu(int x, vector<vector<int>>& shd, vector<string>& one) {
	vector<string> grid(n, string(m, '0'));
	queue<ii> sm, bg;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(shd[i][j] >= x)
				bg.push(ii(i, j)), grid[i][j] = 'X';
	for(int u = 0; u < x; u++) {
		swap(bg, sm);
		while(sm.size()) {
			auto cr = sm.front(); sm.pop();
			for(int i = 0; i < 8; i++) {
				int x = cr.fi + dr[i][0], y = cr.se + dr[i][1];
				if(ins(x, y) && grid[x][y] == '0') {
					bg.push(ii(x, y));
					grid[x][y] = 'X';
				}
			}
		}
	}
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(grid[i][j] == '0')
				grid[i][j] = '.';
	return grid;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	cin >> n >> m;
	vector<vector<int>> shd(n, vector<int>(m, -2));
	vector<string> one(n, string(m, '0'));
	for(int i = 0; i < n; i++)
		cin >> one[i];
	queue<ii> q;
	for(int i = -1; i <= n; i++)
		for(int j = -1; j <= m; j++) {
			if(!ins(i, j)) {
				q.push(ii(i, j));
			} else if(one[i][j] == '.') {
				shd[i][j] = -1, q.push(ii(i, j));
			}
		}

	while(q.size()) {
		auto cr = q.front(); q.pop();
		auto mind = -1;
		if(ins(cr.fi, cr.se)) mind = shd[cr.fi][cr.se];
		bool resu = (ins(cr.fi, cr.se) && one[cr.fi][cr.se] == 'X');
		for(int i = 0; i < 8; i++) {
			int x = cr.fi + dr[i][0], y = cr.se + dr[i][1];
			if(ins(x, y) && shd[x][y] == -2) {
				shd[x][y] = mind + 1;
				q.push(ii(x, y));
			}
		}
	}

	int le = 0, ri = n + m, mi;
	while(le + 1 < ri) {
		mi = (le + ri) / 2;
		auto eval = vlu(mi, shd, one);
		if(eval == one) le = mi;
		else ri = mi;
	}
	cout << le << '\n';
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(shd[i][j] >= le) cout << 'X';
			else cout << '.';
		}
		cout << '\n';
	}
}


#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

class DebugStream {}LOG;
template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; }
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29;

int n, m, dr[8][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};

bool ins(int x, int y) {
	return x >= 0 && y >= 0 && x < n && y < m;
}

int getmini(vector<string> vc) {
	int ans = vc.size();
	for(int u = 0; u < vc.size(); u++) {
		for(int i = 0, j = 0; i < vc[u].size(); i = j) {
			for(; j < vc[u].size() && vc[u][i] == vc[u][j]; j++);
			if(vc[u][i] == 'X') ans = min(ans, j - i);
		}
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	cin >> n >> m;
	vector<string> one(n, string(m, '0')), two(m, string(n, '0'));
	vector<vector<int>> shd(n, vector<int>(m, -2));
	for(int i = 0; i < n; i++) {
		cin >> one[i];
		for(int j = 0; j < m; j++)
			two[j][i] = one[i][j];
	}
	int cr = min(getmini(one), getmini(two));
	if(cr % 2 == 0) cr--;

	queue<ii> q;
	for(int i = -1; i <= n; i++)
		for(int j = -1; j <= m; j++) {
			if(!ins(i, j)) {
				q.push(ii(i, j));
			} else if(one[i][j] == '.') {
				shd[i][j] = -1, q.push(ii(i, j));
			}
		}

	while(q.size()) {
		auto cr = q.front(); q.pop();
		auto mind = -1;
		if(ins(cr.fi, cr.se)) mind = shd[cr.fi][cr.se];
		for(int i = 0; i < 8; i++) {
			int x = cr.fi + dr[i][0], y = cr.se + dr[i][1];
			if(ins(x, y) && shd[x][y] == -2) {
				shd[x][y] = mind + 1;
				q.push(ii(x, y));
			}
		}
	}
	cr /= 2;
	cout << cr << '\n';
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			if(shd[i][j] >= cr)
				one[i][j] = 'X';
			else
				one[i][j] = '.';
		cout << one[i] << '\n';
	}
}


#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

class DebugStream {}LOG;
template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; }
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29;

int getmini(vector<string> vc) {
	int ans = vc.size();
	for(int u = 0; u < vc.size(); u++) {
		for(int i = 0, j = 0; i < vc[u].size(); i = j) {
			for(; j < vc[u].size() && vc[u][i] == vc[u][j]; j++);
			if(vc[u][i] == 'X') ans = min(ans, j - i);
		}
	}
	return ans;
}

void solve(vector<string>& vc, int cr) {
	for(int u = 0; u < vc.size(); u++) {
		for(int i = 0, j = 0; i < vc[u].size(); i = j) {
			for(; j < vc[u].size() && vc[u][i] == vc[u][j]; j++);
			if(vc[u][i] == '.') continue;
			for(int k = i; k < i + cr; k++) vc[u][k] = '.';
			for(int k = j - cr; k < j; k++) vc[u][k] = '.';
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n, m;
	cin >> n >> m;
	vector<string> one(n, string(m, '0')), two(m, string(n, '0'));
	for(int i = 0; i < n; i++) {
		cin >> one[i];
		for(int j = 0; j < m; j++)
			two[j][i] = one[i][j];
	}
	int cr = min(getmini(one), getmini(two));
	if(cr % 2 == 0) cr--;
	solve(one, cr / 2), solve(two, cr / 2);

	cout << (cr - 1) / 2 << '\n';
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			if(two[j][i] == '.') one[i][j] = '.';
		cout << one[i] << '\n';
	}
}


// Code by H~$~C
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma GCC optimize("Ofast", "unroll-loops")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define rint register int
#define rep(i, n) for (rint i = 0; i < (n); ++i)
#define rep1(i, n) for (rint i = (1); i <= (n); ++i)
#define Rep(i, a, b) for (rint i = (a); i <= (b); ++i)
#define per(i, n) for (rint i = ((n) - 1); ~i; --i)
#define per1(i, n) for (rint i = (n); i; --i)
#define Per(i, a, b) for (rint i = (a); i >= (b); --i)
#define repit(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define eprintf(...) (fprintf(stderr, __VA_ARGS__) & fflush(stderr))
#define fillset(a, val) memset(a, val, sizeof(a))
#define All(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define fi first
#define se second
#define pb push_back
#define y0 y19260817
#define y1 y19268107
#define j0 j19260817
#define j1 j19268107
#define x0 x19260817
#define x1 x19268107
using namespace std;
#ifndef LOCAL_JUDGE
#define Debug(...)
#else
template <typename T> inline void _debug(const char *names, T &&t) {
(cerr << names << " = " << t << endl).flush();}
template <typename T1, typename ...T>
inline void _debug(const char *names, T1 &&t1, T &&... t) {
const char *comma = strchr(names, ',');
(cerr.write(names, comma - names) << " = " << t1 << ", ").flush();
_debug(comma + 1, t ...);}
#define Debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
#endif
struct __STARTER_OF_PROGRAM__ { __STARTER_OF_PROGRAM__() {
#ifndef ONLINE_JUDGE
  cerr << "Code by H~$~C: \n" << flush;
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(12);
} } __starter_of_program__;
#ifdef LOCAL_JUDGE
struct __ENDDER_OF_PROGRAM__ { ~__ENDDER_OF_PROGRAM__() {
  cerr << "\n\nTime elasped: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" << flush;
} } __endder_of_program__;
#endif
using ll = long long; using ull = unsigned long long; using uint = unsigned int;
using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>;
using pli = pair<ll, int>; using pil = pair<int, ll>;
template <typename T> using min_queue = priority_queue<T, vector <T>, greater <T> >;
template <typename T> using max_queue = priority_queue<T>;
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
static uniform_int_distribution<int> __disint__;
inline int randint(void) { return __disint__(gen); }
inline int randint(int l, int r) { return __disint__(gen) % (r - l + 1) + l; }
template <typename T> inline void reinit(T &t) { t.~T(); new (&t) T(); }
template <typename T> inline T chmax(T &x, T y) { return x = max(x, y); }
template <typename T> inline T chmin(T &x, T y) { return x = min(x, y); }
template <typename T> inline T chmax(T &x, initializer_list <T> i) { return x = max(x, max(i)); }
template <typename T> inline T chmin(T &x, initializer_list <T> i) { return x = min(x, min(i)); }
inline void read() { } inline void write() { }
template <typename T> inline void read(T &x) { cin >> x; }
template <typename T> inline void write(const T &x) { cout << x; }
template <typename T, typename U>
inline void write(const pair<T, U> &x) { write(x.first), cout.put(' '), write(x.second);}
template <typename T, typename U>
inline void read(pair<T, U> &x) { read(x.first), read(x.second);}
template <typename T, typename ...Ts> inline void read(T &x, Ts &...y) { read(x), read(y...); }
template <typename T, typename ...Ts> inline void write(const T &x, const Ts &...y) {
write(x), (sizeof...(y) && (cout.put(' '), 0)), write(y...); }
#define writeln(...) (write(__VA_ARGS__), cout.put('\n'), 0)
template <typename It> inline void _write(It a, It b) {
register char flag = 0; while (a != b) { flag && (cout.put(' '), 0); write(*a ++), flag = 1; } }
#define _writeln(...) (_write(__VA_ARGS__), cout.put('\n'), 0)
const int mod1 = 1000000007, mod2 = 998244353, mod3 = 1000000009;
const double EPS = 1e-9, PI = acos(-1.0), _E_ = exp(1.0);
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;

const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

int n, m;
vector<string> grid;

signed main(int argc, const char *argv[]) {
  read(n, m);
  grid.resize(n + 1);
  grid[0] = string(m + 2, '.');
  rep1(i, n) cin >> grid[i], grid[i] = '.' + grid[i] + '.';
  grid.push_back(string(m + 2, '.'));
  n += 2, m += 2;
  queue<pii> que;
  vector<pii> outer;
  vector<vector<bool> > vis(n, vector<bool>(m, 0));
  rep(i, n) rep(j, m) if (grid[i][j] == '.' && !vis[i][j]) {
    que.push(pii(i, j));
    vis[i][j] = true;
    while (!que.empty()) {
      int x = que.front().fi;
      int y = que.front().se;
      que.pop();
      if (grid[x][y] == 'X') {
        outer.push_back(pii(x, y));
        continue;
      }
      rep(f, 8) {
        int nx = x + dx[f];
        int ny = y + dy[f];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
        if (vis[nx][ny]) continue;
        vis[nx][ny] = true;
        que.push({nx, ny});
      }
    }
  }
  vector<vector<int> > dist(n, vector<int>(m, inf));
  for (auto &x: outer) {
    que.push(x), dist[x.fi][x.se] = 0;
  }
  while (!que.empty()) {
    int x = que.front().fi;
    int y = que.front().se;
    que.pop();
    rep(f, 8) {
      int nx = x + dx[f];
      int ny = y + dy[f];
      if (grid[nx][ny] != 'X') continue;
      if (dist[nx][ny] != inf) continue;
      dist[nx][ny] = dist[x][y] + 1;
      que.push({nx, ny});
    }
  }
  vis.clear();
  vis.resize(n, vector<bool>(m, 0));
  int mx = 0, l = 0, r = 0;
  rep(i, n) rep(j, m) if (dist[i][j] != inf) r = max(r, dist[i][j]);
  while (l <= r) {
    int mid = l + r >> 1;
    bool ok = true;
    {
      vis.clear();
      vis.resize(n, vector<bool>(m, 0));
      vector<vector<int> > pd(n, vector<int>(m, inf));
      rep(i, n) rep(j, m)
        if (dist[i][j] != inf && dist[i][j] >= mid)
          que.push({i, j}), vis[i][j] = true, pd[i][j] = mid;
      while (!que.empty()) {
        int x = que.front().fi;
        int y = que.front().se;
        que.pop();
        if (pd[x][y] == 0) continue;
        rep(f, 8) {
          int nx = x + dx[f];
          int ny = y + dy[f];
          if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
          if (vis[nx][ny]) continue;
          pd[nx][ny] = pd[x][y] - 1;
          vis[nx][ny] = true;
          que.push({nx, ny});
        }
      }
      rep(i, n) rep(j, m)
        if (grid[i][j] == 'X' && !vis[i][j])
          ok = false;
    }
    if (ok) l = mid + 1, mx = mid;
    else r = mid - 1;
  }
  writeln(mx);
  rep1(i, n - 2) {
    rep1(j, m - 2)
      write(".X"[dist[i][j] >= mx && dist[i][j] != inf]);
    writeln();
  }
	return 0;
}


// Code by H~$~C
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma GCC optimize("Ofast", "unroll-loops")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define rint register int
#define rep(i, n) for (rint i = 0; i < (n); ++i)
#define rep1(i, n) for (rint i = (1); i <= (n); ++i)
#define Rep(i, a, b) for (rint i = (a); i <= (b); ++i)
#define per(i, n) for (rint i = ((n) - 1); ~i; --i)
#define per1(i, n) for (rint i = (n); i; --i)
#define Per(i, a, b) for (rint i = (a); i >= (b); --i)
#define repit(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define eprintf(...) (fprintf(stderr, __VA_ARGS__) & fflush(stderr))
#define fillset(a, val) memset(a, val, sizeof(a))
#define All(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define fi first
#define se second
#define pb push_back
#define y0 y19260817
#define y1 y19268107
#define j0 j19260817
#define j1 j19268107
#define x0 x19260817
#define x1 x19268107
using namespace std;
#ifndef LOCAL_JUDGE
#define Debug(...)
#else
template <typename T> inline void _debug(const char *names, T &&t) {
(cerr << names << " = " << t << endl).flush();}
template <typename T1, typename ...T>
inline void _debug(const char *names, T1 &&t1, T &&... t) {
const char *comma = strchr(names, ',');
(cerr.write(names, comma - names) << " = " << t1 << ", ").flush();
_debug(comma + 1, t ...);}
#define Debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
#endif
struct __STARTER_OF_PROGRAM__ { __STARTER_OF_PROGRAM__() {
#ifndef ONLINE_JUDGE
  cerr << "Code by H~$~C: \n" << flush;
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(12);
} } __starter_of_program__;
#ifdef LOCAL_JUDGE
struct __ENDDER_OF_PROGRAM__ { ~__ENDDER_OF_PROGRAM__() {
  cerr << "\n\nTime elasped: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" << flush;
} } __endder_of_program__;
#endif
using ll = long long; using ull = unsigned long long; using uint = unsigned int;
using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>;
using pli = pair<ll, int>; using pil = pair<int, ll>;
template <typename T> using min_queue = priority_queue<T, vector <T>, greater <T> >;
template <typename T> using max_queue = priority_queue<T>;
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
static uniform_int_distribution<int> __disint__;
inline int randint(void) { return __disint__(gen); }
inline int randint(int l, int r) { return __disint__(gen) % (r - l + 1) + l; }
template <typename T> inline void reinit(T &t) { t.~T(); new (&t) T(); }
template <typename T> inline T chmax(T &x, T y) { return x = max(x, y); }
template <typename T> inline T chmin(T &x, T y) { return x = min(x, y); }
template <typename T> inline T chmax(T &x, initializer_list <T> i) { return x = max(x, max(i)); }
template <typename T> inline T chmin(T &x, initializer_list <T> i) { return x = min(x, min(i)); }
inline void read() { } inline void write() { }
template <typename T> inline void read(T &x) { cin >> x; }
template <typename T> inline void write(const T &x) { cout << x; }
template <typename T, typename U>
inline void write(const pair<T, U> &x) { write(x.first), cout.put(' '), write(x.second);}
template <typename T, typename U>
inline void read(pair<T, U> &x) { read(x.first), read(x.second);}
template <typename T, typename ...Ts> inline void read(T &x, Ts &...y) { read(x), read(y...); }
template <typename T, typename ...Ts> inline void write(const T &x, const Ts &...y) {
write(x), (sizeof...(y) && (cout.put(' '), 0)), write(y...); }
#define writeln(...) (write(__VA_ARGS__), cout.put('\n'), 0)
template <typename It> inline void _write(It a, It b) {
register char flag = 0; while (a != b) { flag && (cout.put(' '), 0); write(*a ++), flag = 1; } }
#define _writeln(...) (_write(__VA_ARGS__), cout.put('\n'), 0)
const int mod1 = 1000000007, mod2 = 998244353, mod3 = 1000000009;
const double EPS = 1e-9, PI = acos(-1.0), _E_ = exp(1.0);
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;

const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

int n, m;
vector<string> grid;

signed main(int argc, const char *argv[]) {
  read(n, m);
  grid.resize(n + 1);
  grid[0] = string(m + 2, '.');
  rep1(i, n) cin >> grid[i], grid[i] = '.' + grid[i] + '.';
  grid.push_back(string(m + 2, '.'));
  n += 2, m += 2;
  queue<pii> que;
  vector<pii> outer;
  vector<vector<bool> > vis(n, vector<bool>(m, 0));
  rep(i, n) rep(j, m) if (grid[i][j] == '.' && !vis[i][j]) {
    que.push(pii(i, j));
    vis[i][j] = true;
    while (!que.empty()) {
      int x = que.front().fi;
      int y = que.front().se;
      que.pop();
      if (grid[x][y] == 'X') {
        outer.push_back(pii(x, y));
        continue;
      }
      rep(f, 8) {
        int nx = x + dx[f];
        int ny = y + dy[f];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
        if (vis[nx][ny]) continue;
        vis[nx][ny] = true;
        que.push({nx, ny});
      }
    }
  }
  vector<vector<int> > dist(n, vector<int>(m, inf));
  for (auto &x: outer) {
    que.push(x), dist[x.fi][x.se] = 0;
  }
  while (!que.empty()) {
    int x = que.front().fi;
    int y = que.front().se;
    que.pop();
    rep(f, 8) {
      int nx = x + dx[f];
      int ny = y + dy[f];
      if (grid[nx][ny] != 'X') continue;
      if (dist[nx][ny] != inf) continue;
      dist[nx][ny] = dist[x][y] + 1;
      que.push({nx, ny});
    }
  }
  vis.clear();
  vis = vector<vector<bool> >(n, vector<bool>(m, 0));
  int mx = inf;
  rep1(i, n - 2) rep1(j, m - 2)
    if (dist[i][j] != inf && !vis[i][j]) {
      int now = 0;
      que.push(pii(i, j));
      vis[i][j] = true;
      while (!que.empty()) {
        int x = que.front().fi;
        int y = que.front().se;
        que.pop();
        now = max(now, dist[x][y]);
        rep(f, 8) {
          int nx = x + dx[f];
          int ny = y + dy[f];
          if (dist[nx][ny] == inf) continue;
          if (vis[nx][ny]) continue;
          vis[nx][ny] = true;
          que.push(pii(nx, ny));
        }
      }
      mx = min(mx, now);
    }
  writeln(mx);
  rep1(i, n - 2) {
    rep1(j, m - 2)
      write(".X"[dist[i][j] >= mx && dist[i][j] != inf]);
    writeln();
  }
	return 0;
}