#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> splay;

const int N = 1000010;
const int mod = 998244353;

inline int read()
{
  int p=0; int f=1; char ch=getchar();
  while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();}
  while(ch>='0' && ch<='9'){p=p*10+ch-'0'; ch=getchar();}
  return p*f;
}

int n,m;

int main()
{
//  freopen("a.in","r",stdin);
//  freopen("a.out","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  vector<string> str(n);
  for(int i=0;i<n;i++) cin >> str[i];
  vector<vector<int> > s(n+2 , vector<int> (m+2) );
  for(int i=0;i<n;i++) for(int j=0;j<m;j++)
  {
    s[i+1][j+1] = (str[i][j] == 'X');
    s[i+1][j+1] -= s[i][j];
    s[i+1][j+1] += s[i+1][j];
    s[i+1][j+1] += s[i][j+1];
  }

//  printf("%d %d\n",n,m);

  auto check = [&](int k)
  {
  	k = k * 2; // cout << k << endl;
    vector<vector<int> >t(n+2 , vector<int> (m+2) );
    for(int i=0;i<n-k;i++) for(int j=0;j<m-k;j++) {
      int temp = s[i+k+1][j+k+1];
      temp += s[i][j];
      temp -= s[i][j+k+1];
      temp -= s[i+k+1][j];
//      cout<< k << " " << i << " " << j << " " << temp << endl;
      if(temp == (k+1)*(k+1)){
      	t[i+1][j+1] ++;
      	t[i+k+2][j+1] --; t[i+1][j+k+2] --;
      	t[i+k+2][j+k+2] ++;
      }
    }

    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
	{
	  t[i][j] += t[i-1][j];
	  t[i][j] += t[i][j-1];
	  t[i][j] -= t[i-1][j-1];
//	  printf("%d %d %d %d\n",k,i,j,t[i][j]);s
      if(!t[i][j] && str[i-1][j-1] == 'X') return 0;
      if(t[i][j] && str[i-1][j-1] == '.') return 0;
    }return 1;
  };

  int l = 0; int r = min(n,m) / 2; int res;
  // cout << l << " " << r << endl;
  while(l<=r)
  {
    int mid = (l+r)>>1;
    if(check(mid)){res=mid; l = mid+1;}
    else r=mid-1;
  }
  cout << res << endl; res = res * 2;
  vector<string> ss(n , string(m , '.'));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) ss[i][j] = '.';
  for(int i=0;i<n-res;i++) for(int j=0;j<m-res;j++)
  {
    int temp = s[i+res+1][j+res+1];
	if(i && j) temp += s[i][j];
	if(i) temp -= s[i][j+res+1];
	if(j) temp -= s[i+res+1][j];
	if(temp == (res+1) * (res+1)) ss[i+res/2][j+res/2] = 'X';
  }
  for(int i=0;i<n;i++) cout<<ss[i]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> splay;

const int N = 1000010;
const int mod = 998244353;

inline int read()
{
  int p=0; int f=1; char ch=getchar();
  while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();}
  while(ch>='0' && ch<='9'){p=p*10+ch-'0'; ch=getchar();}
  return p*f;
}

int n,m;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  n = read(); m = read();
  vector<string> str(n+1);
  for(int i=0;i<n;i++) cin >> str[i];
  vector<vector<int> > s(n+1 , vector<int> (m+1) );
  for(int i=0;i<n;i++) for(int j=0;j<m;j++)
  {
    s[i][j] = (str[i][j] == 'X');
    if(i && j) s[i][j] -= s[i-1][j-1];
    if(i) s[i][j] += s[i-1][j];
    if(j) s[i][j] += s[i][j-1];
  }

//  printf("%d %d\n",n,m);

  auto check = [&](int k)
  {
    vector<vector<int> >t(n+1 , vector<int> (m+1) );
    for(int i=0;i<n-2*k;i++) for(int j=0;j<m-2*k;j++) {
      int temp = s[i+2*k][j+2*k];
      if(i && j) temp += s[i-1][j-1];
      if(i) temp -= s[i-1][j+2*k];
      if(j) temp -= s[i+2*k][j-1];
//      printf("%d %d %d %d\n",k,i,j,temp);
      if(temp == (2*k+1)*(2*k+1)){
      	t[i][j] ++;
      	t[i+2*k+1][j] --; t[i][j+2*k+1] --;
      	t[i+2*k+1][j+2*k+1] ++;
      }
    }

    for(int i=0;i<n;i++) for(int j=0;j<m;j++)
	{
	  if(i) t[i][j] += t[i-1][j];
	  if(j) t[i][j] += t[i][j-1];
	  if(i&&j) t[i][j] -= t[i-1][j-1];
//	  printf("%d %d %d %d\n",k,i,j,t[i][j]);
      if(!t[i][j] && str[i][j] == 'X') return 0;
      if(t[i][j] && str[i][j] == '.') return 0;
    }return 1;
  };

  int l = 0; int r = min(n,m) / 2; int res;
  while(l<=r)
  {
    int mid = (l+r)>>1;
    if(check(mid)){res=mid; l = mid+1;}
    else r=mid-1;
  }
  cout << res << endl;
  vector<string> ss(n , string(m , '.'));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) ss[i][j] = '.';
  for(int i=0;i<n-2*res;i++) for(int j=0;j<m-2*res;j++)
  {
    int temp = s[i+2*res][j+2*res];
	if(i && j) temp += s[i-1][j-1];
	if(i) temp -= s[i-1][j+2*res];
	if(j) temp -= s[i+2*res][j-1];
	if(temp == (2*res+1) * (2*res+1)) ss[i+res][j+res] = 'X';
  }
  for(int i=0;i<n;i++) cout<<ss[i]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> splay;

const int N = 1000010;
const int mod = 998244353;

inline int read()
{
  int p=0; int f=1; char ch=getchar();
  while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();}
  while(ch>='0' && ch<='9'){p=p*10+ch-'0'; ch=getchar();}
  return p*f;
}

int n,m;

int main()
{
  n = read(); m = read();
  vector<string> str(n+1);
  for(int i=0;i<n;i++) cin >> str[i];
  vector<vector<int> > s(n+1 , vector<int> (m+1) );
  for(int i=0;i<n;i++) for(int j=0;j<m;j++)
  {
    s[i][j] = (str[i][j] == 'X');
    if(i && j) s[i][j] -= s[i-1][j-1];
    if(i) s[i][j] += s[i-1][j];
    if(j) s[i][j] += s[i][j-1];
  }

//  printf("%d %d\n",n,m);

  auto check = [&](int k)
  {
    vector<vector<int> >t(n+1 , vector<int> (m+1) );
    for(int i=0;i<n-2*k;i++) for(int j=0;j<m-2*k;j++) {
      int temp = s[i+2*k][j+2*k];
      if(i && j) temp += s[i-1][j-1];
      if(i) temp -= s[i-1][j+2*k];
      if(j) temp -= s[i+2*k][j-1];
//      printf("%d %d %d %d\n",k,i,j,temp);
      if(temp == (2*k+1)*(2*k+1)){
      	t[i][j] ++;
      	t[i+2*k+1][j] --; t[i][j+2*k+1] --;
      	t[i+2*k+1][j+2*k+1] ++;
      }
    }

    for(int i=0;i<n;i++) for(int j=0;j<m;j++)
	{
	  if(i) t[i][j] += t[i-1][j];
	  if(j) t[i][j] += t[i][j-1];
	  if(i&&j) t[i][j] -= t[i-1][j-1];
//	  printf("%d %d %d %d\n",k,i,j,t[i][j]);
      if(!t[i][j] && str[i][j] == 'X') return 0;
      else if(t[i][j] && str[i][j] == '.') return 0;
    }return 1;
  };

  int l = 0; int r = n*m; int res;
  while(l<=r)
  {
    int mid = (l+r)>>1;
    if(check(mid)){res=mid; l = mid+1;}
    else r=mid-1;
  }
  printf("%d\n",res);
  vector<string> ss(n , string(m , '.'));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) ss[i][j] = '.';
  for(int i=0;i<n-2*res;i++) for(int j=0;j<m-2*res;j++)
  {
    int temp = s[i+2*res][j+2*res];
	if(i && j) temp += s[i-1][j-1];
	if(i) temp -= s[i-1][j+2*res];
	if(j) temp -= s[i+2*res][j-1];
	if(temp == (2*res+1) * (2*res+1)) ss[i+res][j+res] = 'X';
  }
  for(int i=0;i<n;i++) cout<<ss[i]<<endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define x first
#define y second
#define mp make_pair
#define pb push_back
template <typename TYPE> inline void chkmax(TYPE &x,TYPE y){x<y?x=y:0;}
template <typename TYPE> inline void chkmin(TYPE &x,TYPE y){y<x?x=y:0;}
template <typename TYPE> void readint(TYPE &x)
{
    x=0;int f=1;char c;
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=x*10+c-'0';
    x*=f;
}
const int MAXN=3000015,INF=0x3f3f3f3f,dir[8][2]={1,0,0,1,-1,0,0,-1,1,1,-1,-1,1,-1,-1,1};

int n,m;
inline int ei(int x,int y){return (x-1)*m+y;}
char a[MAXN],str[MAXN];
int dis[MAXN],fis[MAXN];
pii q[MAXN];
bool check(int mid)
{
	memset(dis,0x3f,sizeof(dis));
	memset(fis,0x3f,sizeof(fis));
	int front=1,rear=0;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(a[ei(i,j)]=='.')
			{
				if(mid)q[++rear]=mp(i,j);
				dis[ei(i,j)]=0;
			}
	while(front<=rear)
	{
		pii p=q[front++];
		for(int t=0;t<8;++t)
		{
			int tx=p.x+dir[t][0],ty=p.y+dir[t][1];
			if(!tx || !ty || tx>n || ty>m || dis[ei(tx,ty)]<INF)continue;
			dis[ei(tx,ty)]=dis[ei(p.x,p.y)]+1;
			if(dis[ei(tx,ty)]<mid)q[++rear]=mp(tx,ty);
		}
	}
/*cerr<<mid<<":"<<endl;
for(int i=1;i<=n;++i)
{
	for(int j=1;j<=m;++j)
		putchar(dis[ei(i,j)]<INF?'.':'X');
	putchar('\n');
}*/
	front=1,rear=0;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(dis[ei(i,j)]==INF && a[ei(i,j)]=='X')
			{
				if(mid)q[++rear]=mp(i,j);
				fis[ei(i,j)]=0;
			}
	while(front<=rear)
	{
		pii p=q[front++];
		for(int t=0;t<8;++t)
		{
			int tx=p.x+dir[t][0],ty=p.y+dir[t][1];
			if(!tx || !ty || tx>n || ty>m || fis[ei(tx,ty)]<INF)continue;
			fis[ei(tx,ty)]=fis[ei(p.x,p.y)]+1;
			if(fis[ei(tx,ty)]<mid)q[++rear]=mp(tx,ty);
		}
	}
/*cerr<<endl;
for(int i=1;i<=n;++i)
{
	for(int j=1;j<=m;++j)
		putchar(fis[ei(i,j)]==INF?'.':'X');
	putchar('\n');
}*/
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if( (a[ei(i,j)]=='.')!=(fis[ei(i,j)]==INF) )return 0;
	return 1;
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("code.in","r",stdin);
	//freopen("code.out","w",stdout);
	#endif
	readint(n),readint(m);
	n+=2;m+=2;
	for(int j=1;j<=m;++j)a[ei(1,j)]=a[ei(n,j)]='.';
	for(int i=2;i<n;++i)
	{
		a[ei(i,1)]='.';
		scanf("%s",str+1);
		for(int j=2;j<m;++j)
			a[ei(i,j)]=str[j-1];
		a[ei(i,m)]='.';
	}
	int L=1,R=n+m,mid,ans=0;
	while(L<=R)
	{
		mid=(L+R)>>1;
		if(check(mid))L=mid+1,ans=mid;
		else R=mid-1;
	}
	printf("%d\n",ans);
	check(ans);
	for(int i=2;i<n;++i)
	{
		for(int j=2;j<m;++j)
			putchar(dis[ei(i,j)]==INF?'X':'.');
		putchar('\n');
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define x first
#define y second
#define mp make_pair
#define pb push_back
template <typename TYPE> inline void chkmax(TYPE &x,TYPE y){x<y?x=y:0;}
template <typename TYPE> inline void chkmin(TYPE &x,TYPE y){y<x?x=y:0;}
template <typename TYPE> void readint(TYPE &x)
{
    x=0;int f=1;char c;
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=x*10+c-'0';
    x*=f;
}
const int MAXN=1000005,INF=0x3f3f3f3f,dir[8][2]={1,0,0,1,-1,0,0,-1,1,1,-1,-1,1,-1,-1,1};

int n,m;
inline int ei(int x,int y){return (x-1)*m+y;}
char a[MAXN],str[MAXN];
int dis[MAXN],fis[MAXN];
pii q[MAXN];
bool check(int mid)
{
	memset(dis,0x3f,sizeof(dis));
	memset(fis,0x3f,sizeof(fis));
	int front=1,rear=0;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(a[ei(i,j)]=='.')
			{
				if(mid)q[++rear]=mp(i,j);
				dis[ei(i,j)]=0;
			}
	while(front<=rear)
	{
		pii p=q[front++];
		for(int t=0;t<8;++t)
		{
			int tx=p.x+dir[t][0],ty=p.y+dir[t][1];
			if(!tx || !ty || tx>n || ty>m || dis[ei(tx,ty)]<INF)continue;
			dis[ei(tx,ty)]=dis[ei(p.x,p.y)]+1;
			if(dis[ei(tx,ty)]<mid)q[++rear]=mp(tx,ty);
		}
	}
/*cerr<<mid<<":"<<endl;
for(int i=1;i<=n;++i)
{
	for(int j=1;j<=m;++j)
		putchar(dis[ei(i,j)]<INF?'.':'X');
	putchar('\n');
}*/
	front=1,rear=0;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(dis[ei(i,j)]==INF && a[ei(i,j)]=='X')
			{
				if(mid)q[++rear]=mp(i,j);
				fis[ei(i,j)]=0;
			}
	while(front<=rear)
	{
		pii p=q[front++];
		for(int t=0;t<8;++t)
		{
			int tx=p.x+dir[t][0],ty=p.y+dir[t][1];
			if(!tx || !ty || tx>n || ty>m || fis[ei(tx,ty)]<INF)continue;
			fis[ei(tx,ty)]=fis[ei(p.x,p.y)]+1;
			if(fis[ei(tx,ty)]<mid)q[++rear]=mp(tx,ty);
		}
	}
/*cerr<<endl;
for(int i=1;i<=n;++i)
{
	for(int j=1;j<=m;++j)
		putchar(fis[ei(i,j)]==INF?'.':'X');
	putchar('\n');
}*/
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if( (a[ei(i,j)]=='.')!=(fis[ei(i,j)]==INF) )return 0;
	return 1;
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("code.in","r",stdin);
	//freopen("code.out","w",stdout);
	#endif
	readint(n),readint(m);
	n+=2;m+=2;
	for(int j=1;j<=m;++j)a[ei(1,j)]=a[ei(n,j)]='.';
	for(int i=2;i<n;++i)
	{
		a[ei(i,1)]='.';
		scanf("%s",str+1);
		for(int j=2;j<m;++j)
			a[ei(i,j)]=str[j-1];
		a[ei(i,m)]='.';
	}
	int L=1,R=n+m,mid,ans=0;
	while(L<=R)
	{
		mid=(L+R)>>1;
		if(check(mid))L=mid+1,ans=mid;
		else R=mid-1;
	}
	printf("%d\n",ans);
	check(ans);
	for(int i=2;i<n;++i)
	{
		for(int j=2;j<m;++j)
			putchar(dis[ei(i,j)]==INF?'X':'.');
		putchar('\n');
	}
	return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

using namespace std;

const int N = 1e6 + 5;
string s[N], t[N];
vector<int> sum[N];

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

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
    	cin >> s[i];
    	t[i].resize(m, '.');
    	sum[i].resize(m + 1, 0);
    }
    sum[n].resize(m + 1, 0);

    for (int i = n - 1; i >= 0; i--) {
    	for (int j = m - 1; j >= 0; j--) {
    		sum[i][j] = sum[i + 1][j] + sum[i][j + 1] - sum[i + 1][j + 1];
    		sum[i][j] += (s[i][j] == 'X');
    	}
    }

    int mn = n + m;

    for (int i = 0; i < n; i++) {
    	for (int j = 0, k = -1; j < m; j++) {
    		if (s[i][j] == 'X') {
    			if (j == m - 1 || s[i][j + 1] == '.')
    				mn = min(mn, j - k);
    		}
    		else {
    			k = j;
    		}
    	}
    }

    for (int i = 0; i < m; i++) {
    	for (int j = 0, k = -1; j < n; j++) {
    		if (s[j][i] == 'X') {
    			if (j == n - 1 || s[j + 1][i] == '.')
    				mn = min(mn, j - k);
    		}
    		else {
    			k = j;
    		}
    	}
    }

    if (mn % 2 == 0)
    	mn--;

    cout << mn / 2 << "\n";

    for (int i = 0; i + mn <= n; i++) {
    	for (int j = 0; j + mn <= m; j++) {
    		if (sum[i][j] - sum[i + mn][j]
    			- sum[i][j + mn] + sum[i + mn][j + mn] == mn * mn) {
    			t[i + mn / 2][j + mn / 2] = 'X';
    		}
    	}
    }

    for (int i = 0; i < n; i++) {
    	cout << t[i] << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

using namespace std;

const int N = 1e6 + 5;
string s[N], t[N];

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

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
    	cin >> s[i];
    	t[i].resize(m, '.');
    }

    int mn = n + m;

    for (int i = 0; i < n; i++) {
    	for (int j = 0, k = -1; j < m; j++) {
    		if (s[i][j] == 'X') {
    			if (j == m - 1 || s[i][j + 1] == '.')
    				mn = min(mn, j - k);
    		}
    		else {
    			k = j;
    		}
    	}
    }

    for (int i = 0; i < m; i++) {
    	for (int j = 0, k = -1; j < n; j++) {
    		if (s[j][i] == 'X') {
    			if (j == n - 1 || s[j + 1][i] == '.')
    				mn = min(mn, j - k);
    		}
    		else {
    			k = j;
    		}
    	}
    }

    if (mn % 2 == 0)
    	mn--;

    cout << mn / 2 << "\n";

    for (int i = 0; i < n; i++) {
    	for (int j = 0; j < m; j++) {
    		if (s[i][j] == 'X' && s[i + mn - 1][j] == 'X' &&
    			s[i][j + mn - 1] == 'X') {
    			t[i + mn / 2][j + mn / 2] = 'X';
    		}
    	}
    }

    for (int i = 0; i < n; i++) {
    	cout << t[i] << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

using namespace std;

const int N = 1e6 + 5;
string s[N], t[N];
vector<int> sum[N], w[N];

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

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
    	cin >> s[i];
    	t[i].resize(m, '.');
    	sum[i].resize(m + 1, 0);
    	w[i].resize(m + 1, 0);
    }
    sum[n].resize(m + 1, 0);
    w[n].resize(m + 1, 0);

    for (int i = n - 1; i >= 0; i--) {
    	for (int j = m - 1; j >= 0; j--) {
    		sum[i][j] = sum[i + 1][j] + sum[i][j + 1] - sum[i + 1][j + 1];
    		sum[i][j] += (s[i][j] == 'X');
    	}
    }

    int l = 0, r = min(n, m) + 1;
    while (r - l > 1) {
    	int p = (l + r) >> 1;
    	bool ok = true;

    	for (int i = 0; i <= n; i++) {
    		fill(all(w[i]), 0);
    	}

    	for (int i = 0; i + p <= n; i++) {
			for (int j = 0; j + p <= m; j++) {
				if (sum[i][j] - sum[i + p][j]
					- sum[i][j + p] + sum[i + p][j + p] == p * p) {
					// cout << i << " " << j << "\n";
					w[i][j]++;
					w[i + p][j]--;
					w[i][j + p]--;
					w[i + p][j + p]++;
				}
			}
		}

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (i > 0)
					w[i][j] += w[i - 1][j];
				if (j > 0)
					w[i][j] += w[i][j - 1];
				if (i > 0 && j > 0)
					w[i][j] -= w[i - 1][j - 1];
				if (w[i][j] == 0 && s[i][j] == 'X')
					ok = false;
			}
		}

		if (ok)
			l = p;
		else
			r = p;
    }

    int mn = (l % 2 == 0 ? l - 1 : l);

    cout << mn / 2 << "\n";

    for (int i = 0; i + mn <= n; i++) {
    	for (int j = 0; j + mn <= m; j++) {
    		if (sum[i][j] - sum[i + mn][j]
    			- sum[i][j + mn] + sum[i + mn][j + mn] == mn * mn) {
    			t[i + mn / 2][j + mn / 2] = 'X';
    		}
    	}
    }

    for (int i = 0; i < n; i++) {
    	cout << t[i] << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

using namespace std;

const int N = 1e6 + 5;
string s[N], t[N];

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

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
    	cin >> s[i];
    	t[i].resize(m, '.');
    }

    int mn = n + m;

    for (int i = 0; i < n; i++) {
    	for (int j = 0, k = -1; j < m; j++) {
    		if (s[i][j] == 'X') {
    			if (j == m - 1 || s[i][j + 1] == '.')
    				mn = min(mn, j - k);
    		}
    		else {
    			k = j;
    		}
    	}
    }

    for (int i = 0; i < m; i++) {
    	for (int j = 0, k = -1; j < n; j++) {
    		if (s[j][i] == 'X') {
    			if (j == n - 1 || s[j + 1][i] == '.')
    				mn = min(mn, j - k);
    		}
    		else {
    			k = j;
    		}
    	}
    }

    if (mn % 2 == 0)
    	mn--;

    cout << mn / 2 << "\n";

    for (int i = 0; i + mn <= n; i++) {
    	for (int j = 0; j + mn <= m; j++) {
    		if (s[i][j] == 'X' && s[i + mn - 1][j] == 'X' &&
    			s[i][j + mn - 1] == 'X') {
    			t[i + mn / 2][j + mn / 2] = 'X';
    		}
    	}
    }

    for (int i = 0; i < n; i++) {
    	cout << t[i] << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> pref, r;
vector<string> out_init;

bool check(int x, vector<string>& v, bool output) {
  int n = v.size(), m = v[0].size();
  for (int i = 0; i < n; ++i) {
    fill(pref[i].begin(), pref[i].end(), 0);
    fill(r[i].begin(), r[i].end(), 0);
    fill(out_init[i].begin(), out_init[i].end(), '.');
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (v[i][j] == '.') pref[i][j] += 1;
      if (i > 0) pref[i][j] += pref[i - 1][j];
      if (j > 0) pref[i][j] += pref[i][j - 1];
      if (i > 0 && j > 0) pref[i][j] -= pref[i - 1][j - 1];
    }
  }

  auto try_place = [&](int i, int j) {
    if (i - x < 0 || j - x < 0 || i + x >= n || j + x >= m)
      return false;
    int s = pref[i + x][j + x];
    if (i - x > 0) s -= pref[i - x - 1][j + x];
    if (j - x > 0) s -= pref[i + x][j - x - 1];
    if (i - x > 0 && j - x > 0) s += pref[i - x - 1][j - x - 1];
    if (s > 0)
      return false;
    // cerr << "PLACING: " << i << " " << j << endl;
    out_init[i][j] = 'X';
    r[i - x][j - x] += 1;
    if (i + x + 1 < n) r[i + x + 1][j - x] -= 1;
    if (j + x + 1 < m) r[i - x][j + x + 1] -= 1;
    if (i + x + 1 < n && j + x + 1 < m) r[i + x + 1][j + x + 1] += 1;

    return true;
  };

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      try_place(i, j);
    }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (i > 0) r[i][j] += r[i - 1][j];
      if (j > 0) r[i][j] += r[i][j - 1];
      if (i > 0 && j > 0) r[i][j] -= r[i - 1][j - 1];

      if (r[i][j] == 0 && v[i][j] == '.') continue;
      if (r[i][j] > 0 && v[i][j] == 'X') continue;
      return false;
    }
  }

  if (output) {
    for (int i = 0; i < n; ++i) {
      cout << out_init[i] << '\n';
    }
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m; cin >> n >> m;
  vector<string> v(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i];

  // check(1, v, true);

  out_init.resize(n, string(m, '.'));
  r.resize(n, vector<int>(m, 0));
  pref = r;

  int ans = 0;
  for (int step = (1 << 10); step; step /= 2) {
    if (ans + step > n && ans + step > m) continue;
    if (check(ans + step, v, false)) {
      ans += step;
    }
  }
  cout << ans << '\n';
  check(ans, v, true);

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool check(int x, vector<string>& v, bool output) {
  int n = v.size(), m = v[0].size();
  vector<string> out_init(n, string(m, '.'));

  vector<vector<int>> pref(n, vector<int>(m, 0));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (v[i][j] == '.') pref[i][j] += 1;
      if (i > 0) pref[i][j] += pref[i - 1][j];
      if (j > 0) pref[i][j] += pref[i][j - 1];
      if (i > 0 && j > 0) pref[i][j] -= pref[i - 1][j - 1];
    }
  }
  auto try_place = [&](int i, int j) {
    if (i - x < 0 || j - x < 0 || i + x >= n || j + x >= m)
      return false;
    int s = pref[i + x][j + x];
    if (i - x > 0) s -= pref[i - x - 1][j + x];
    if (j - x > 0) s -= pref[i + x][j - x - 1];
    if (i - x > 0 && j - x > 0) s += pref[i - x - 1][j - x - 1];
    if (s > 0)
      return false;
    // cerr << "PLACING: " << i << " " << j << endl;
    out_init[i][j] = 'X';
    return true;
  };

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      try_place(i, j);
    }
  }

  vector<string> out_fin = out_init;
  vector<vector<int>> dist(n, vector<int>(m, -1));
  vector<pair<int, int>> q;

  // for (int i = 0; i < n; ++i) cerr << out_init[i] << endl;

  auto push = [&](int i, int j, int d) {
    // cerr << "PUSH: " << i << " " << j << " " << d << endl;
    if (d > x) return;
    if (dist[i][j] != -1) return;
    // cerr << "SUCCESS" << endl;
    dist[i][j] = d;
    out_fin[i][j] = 'X';
    q.emplace_back(i, j);
  };

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (out_init[i][j] == 'X') {
        push(i, j, 0);
      }
    }
  }

  for (int at = 0; at < (int)q.size(); ++at) {
    int i, j; tie(i, j) = q[at];
    for (int di = -1; di <= 1; ++di) {
      for (int dj = -1; dj <= 1; ++dj) {
        push(i + di, j + dj, dist[i][j] + 1);
      }
    }
  }
  /*
  for (int i = 0; i < n; ++i) {
    cerr << out_fin[i] << endl;
  }
  */
  if (output) {
    assert(out_fin == v);
    for (int i = 0; i < n; ++i) {
      cout << out_init[i] << '\n';
    }
  }
  return out_fin == v;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m; cin >> n >> m;
  vector<string> v(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i];

  // check(1, v, true);

  int ans = 0;
  for (int step = (1 << 10); step; step /= 2) {
    if (ans + step > n && ans + step > m) continue;
    if (check(ans + step, v, false)) {
      ans += step;
    }
  }
  cout << ans << '\n';
  check(ans, v, true);

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool check(int x, vector<string>& v, bool output) {
  int n = v.size(), m = v[0].size();
  vector<string> out_init(n, string(m, '.'));

  vector<vector<int>> pref(n, vector<int>(m, 0));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (v[i][j] == '.') pref[i][j] += 1;
      if (i > 0) pref[i][j] += pref[i - 1][j];
      if (j > 0) pref[i][j] += pref[i][j - 1];
      if (i > 0 && j > 0) pref[i][j] -= pref[i - 1][j - 1];
    }
  }
  auto try_place = [&](int i, int j) {
    if (i - x < 0 || j - x < 0 || i + x >= n || j + x >= m)
      return false;
    int s = pref[i + x][j + x];
    if (i - x > 0) s -= pref[i - x - 1][j + x];
    if (j - x > 0) s -= pref[i + x][j - x - 1];
    if (i - x > 0 && j - x > 0) s += pref[i - x - 1][j - x - 1];
    if (s > 0)
      return false;
    // cerr << "PLACING: " << i << " " << j << endl;
    out_init[i][j] = 'X';
    return true;
  };

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      try_place(i, j);
    }
  }

  vector<string> out_fin = out_init;
  vector<vector<int>> dist(n, vector<int>(m, -1));
  vector<pair<int, int>> q;

  // for (int i = 0; i < n; ++i) cerr << out_init[i] << endl;

  auto push = [&](int i, int j, int d) {
    // cerr << "PUSH: " << i << " " << j << " " << d << endl;
    if (d > x) return;
    if (dist[i][j] != -1) return;
    // cerr << "SUCCESS" << endl;
    dist[i][j] = d;
    out_fin[i][j] = 'X';
    q.emplace_back(i, j);
  };

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (out_init[i][j] == 'X') {
        push(i, j, 0);
      }
    }
  }

  for (int at = 0; at < (int)q.size(); ++at) {
    int i, j; tie(i, j) = q[at];
    for (int di = -1; di <= 1; ++di) {
      for (int dj = -1; dj <= 1; ++dj) {
        push(i + di, j + dj, dist[i][j] + 1);
      }
    }
  }
  /*
  for (int i = 0; i < n; ++i) {
    cerr << out_fin[i] << endl;
  }
  */
  if (output) {
    assert(out_fin == v);
    for (int i = 0; i < n; ++i) {
      cout << out_init[i] << '\n';
    }
  }
  return out_fin == v;
}

int main() {
  int n, m; cin >> n >> m;
  vector<string> v(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i];

  // check(1, v, true);

  int ans = 0;
  for (int step = (1 << 10); step; step /= 2) {
    if (ans + step > n && ans + step > m) continue;
    if (check(ans + step, v, false)) {
      ans += step;
    }
  }
  cout << ans << '\n';
  check(ans, v, true);

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define sz(x) (int) ((x).size())
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define fornr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define forab(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) static_cast<void>(0)
#endif

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using uint = unsigned int;
using vi = vector<int>;
using pii = pair<int, int>;

#define FNAME ""

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s); // do not save '\n'
inline bool isEof();
inline int peekChar();
inline bool seekEof();

template <class T> inline void writeInt(T x, int len);
template <class T> inline void writeUInt(T x, int len);
template <class T> inline void writeInt(T x) { writeInt(x, -1); };
template <class T> inline void writeUInt(T x) { writeUInt(x, -1); };
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

const int MAX_N = 1e6 + 5;

vector<char> a[MAX_N], b[MAX_N], c[MAX_N];
vi sum[MAX_N], sum2[MAX_N];
char r[MAX_N];

bool check(int n, int m, int bm) {
 	int k = (2 * bm + 1) * (2 * bm + 1);
 	forn (i, n + 1)
 		forn (j, m + 1)
			b[i][j] = c[i][j] = 0;
 	forab (i, bm, n - bm)
 		forab (j, bm, m - bm)
 			if (sum[i + bm + 1][j + bm + 1] + sum[i - bm][j - bm] - sum[i - bm][j + bm + 1] - sum[i + bm + 1][j - bm] == k) {
 			 	b[i + bm + 1][j + bm + 1]++, b[i - bm][j - bm]++;
 			 	b[i - bm][j + bm + 1]--, b[i + bm + 1][j - bm]--;
 			 	c[i][j] = 1;
 			}
 	forn (i, n)
 		forn (j, m) {
 			sum2[i + 1][j + 1] = sum2[i + 1][j] + sum2[i][j + 1] - sum2[i][j] + b[i][j];
			if (a[i][j] != (sum2[i + 1][j + 1] >= 1))
				return 0;
		}
	return 1;
}

int main() {
#ifdef LOCAL
	freopen(FNAME".in", "r", stdin);
	freopen(FNAME".out", "w", stdout);
#endif
    cin.tie(0);
    ios_base::sync_with_stdio(0);

	int n = readInt(), m = readInt();
	//cin >> n >> m;
	forn (i, n + 1)
		a[i].resize(m + 1), b[i].resize(m + 1), c[i].resize(m + 1), sum[i].resize(m + 1), sum2[i].resize(m + 1);
	//string s;
	forn (i, n) {
		readLine(r);
		//cin >> s;
		forn (j, m)
			a[i][j] = (r[j] == 'X');
	}
	forn (i, n)
		forn (j, m)
			sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + a[i][j];
	int bl = 0, br = min(n, m) + 1;
	while (br - bl > 1) {
		int bm = (bl + br) / 2;
	 	bool ok = check(n, m, bm);
		if (ok)
			bl = bm;
		else
			br = bm;
	}
	check(n, m, 1);
	check(n, m, bl);
	//cout << bl << "\n";
	writeInt(bl);
	writeChar('\n');
	forn (i, n) {
		forn (j, m)
			if (c[i][j])
				r[j] = 'X';
			else
				r[j] = '.';
		r[m] = '\n';
		writeWord(r);
		//cout << s << "\n";
	}
	flush();
	return 0;
}

const int BUF_SIZE = 4096;

char buf[BUF_SIZE];
int bufLen = 0, pos = 0;

inline bool isEof() {
	if (pos == bufLen) {
		pos = 0, bufLen = fread(buf, 1, BUF_SIZE, stdin);
		if (pos == bufLen)
			return 1;
	}
	return 0;
}

inline int getChar() {
	return isEof() ? -1 : buf[pos++];
}

inline int peekChar() {
	return isEof() ? -1 : buf[pos];
}

inline bool seekEof() {
	int c;
	while ((c = peekChar()) != -1 && c <= 32)
		pos++;
	return c == -1;
}

inline int readChar() {
	int c = getChar();
	while (c != -1 && c <= 32)
		c = getChar();
	return c;
}

inline int readUInt() {
	int c = readChar(), x = 0;
	while ('0' <= c && c <= '9')
		x = x * 10 + c - '0', c = getChar();
	return x;
}

template <class T>
inline T readInt() {
	int s = 1, c = readChar();
	T x = 0;
	if (c == '-')
		s = -1, c = getChar();
	while ('0' <= c && c <= '9')
		x = x * 10 + c - '0', c = getChar();
	return s == 1 ? x : -x;
}

inline double readDouble() {
	int s = 1, c = readChar();
	double x = 0;
	if (c == '-')
		s = -1, c = getChar();
	while ('0' <= c && c <= '9')
	x = x * 10 + c - '0', c = getChar();
	if (c == '.') {
		c = getChar();
		double coef = 1;
		while ('0' <= c && c <= '9')
			x += (c - '0') * (coef *= 1e-1), c = getChar();
	}
	return s == 1 ? x : -x;
}

inline void readWord(char *s) {
	int c = readChar();
	while (c > 32)
		*s++ = c, c = getChar();
	*s = 0;
}

inline bool readLine(char *s) {
	int c = getChar();
	while (c != '\n' && c != -1)
		*s++ = c, c = getChar();
	*s = 0;
	return c != -1;
}

int writePos = 0;
char writeBuf[BUF_SIZE];

inline void writeChar(int x) {
	if (writePos == BUF_SIZE)
		fwrite(writeBuf, 1, BUF_SIZE, stdout), writePos = 0;
	writeBuf[writePos++] = x;
}

inline void flush() {
	if (writePos)
		fwrite(writeBuf, 1, writePos, stdout), writePos = 0;
}

template <class T>
inline void writeInt(T x, int outputLen) {
	if (x < 0)
		writeChar('-'), x = -x;

	char s[24];
	int n = 0;
	while (x || !n)
		s[n++] = '0' + x % 10, x /= 10;
	while (n < outputLen)
		s[n++] = '0';
	while (n--)
		writeChar(s[n]);
}

template <class T>
inline void writeUInt(T x, int outputLen) {
	char s[24];
	int n = 0;
	while (x || !n)
		s[n++] = '0' + char(x % 10), x /= 10;
	while (n < outputLen)
		s[n++] = '0';
	while (n--)
		writeChar(s[n]);
}

inline void writeWord(const char *s) {
	while (*s)
		writeChar(*s++);
}

inline void writeDouble(double x, int outputLen) {
	if (x < 0)
		writeChar('-'), x = -x;
	int t = (int) x;
	writeUInt(t), x -= t;
	writeChar('.');
	for (int i = outputLen - 1; i > 0; i--) {
		x *= 10;
		t = std::min(9, (int) x);
		writeChar('0' + t), x -= t;
	}
	x *= 10;
	t = std::min(9, (int)(x + 0.5));
	writeChar('0' + t);
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define sz(x) (int) ((x).size())
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define fornr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define forab(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) static_cast<void>(0)
#endif

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using uint = unsigned int;
using vi = vector<int>;
using pii = pair<int, int>;

#define FNAME ""

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s); // do not save '\n'
inline bool isEof();
inline int peekChar();
inline bool seekEof();

template <class T> inline void writeInt(T x, int len);
template <class T> inline void writeUInt(T x, int len);
template <class T> inline void writeInt(T x) { writeInt(x, -1); };
template <class T> inline void writeUInt(T x) { writeUInt(x, -1); };
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

const int MAX_N = 1e6 + 5;

vector<char> a[MAX_N], b[MAX_N], c[MAX_N];
vi sum[MAX_N], sum2[MAX_N];
char r[MAX_N];

bool check(int n, int m, int bm) {
 	int k = (2 * bm + 1) * (2 * bm + 1);
 	forn (i, n + 1)
 		forn (j, m + 1)
			b[i][j] = c[i][j] = 0;
 	forab (i, bm, n - bm)
 		forab (j, bm, m - bm)
 			if (sum[i + bm + 1][j + bm + 1] + sum[i - bm][j - bm] - sum[i - bm][j + bm + 1] - sum[i + bm + 1][j - bm] == k) {
 			 	b[i + bm + 1][j + bm + 1] = b[i - bm][j - bm] = 1;
 			 	b[i - bm][j + bm + 1] = b[i + bm + 1][j - bm] = -1;
 			 	c[i][j] = 1;
 			}
 	forn (i, n)
 		forn (j, m) {
 			sum2[i + 1][j + 1] = sum2[i + 1][j] + sum2[i][j + 1] - sum2[i][j] + b[i][j];
			if (a[i][j] != (sum2[i + 1][j + 1] >= 1))
				return 0;
		}
	return 1;
}

int main() {
#ifdef LOCAL
	freopen(FNAME".in", "r", stdin);
	freopen(FNAME".out", "w", stdout);
#endif
    cin.tie(0);
    ios_base::sync_with_stdio(0);

	int n = readInt(), m = readInt();
	//cin >> n >> m;
	forn (i, n + 1)
		a[i].resize(m + 1), b[i].resize(m + 1), c[i].resize(m + 1), sum[i].resize(m + 1), sum2[i].resize(m + 1);
	//string s;
	forn (i, n) {
		readLine(r);
		//cin >> s;
		forn (j, m)
			a[i][j] = (r[j] == 'X');
	}
	forn (i, n)
		forn (j, m)
			sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + a[i][j];
	int bl = 0, br = min(n, m) + 1;
	while (br - bl > 1) {
		int bm = (bl + br) / 2;
	 	bool ok = check(n, m, bm);
		if (ok)
			bl = bm;
		else
			br = bm;
	}
	check(n, m, bl);
	//cout << bl << "\n";
	writeInt(bl);
	writeChar('\n');
	forn (i, n) {
		forn (j, m)
			if (c[i][j])
				r[j] = 'X';
			else
				r[j] = '.';
		r[m] = '\n';
		writeWord(r);
		//cout << s << "\n";
	}
	flush();
	return 0;
}

const int BUF_SIZE = 4096;

char buf[BUF_SIZE];
int bufLen = 0, pos = 0;

inline bool isEof() {
	if (pos == bufLen) {
		pos = 0, bufLen = fread(buf, 1, BUF_SIZE, stdin);
		if (pos == bufLen)
			return 1;
	}
	return 0;
}

inline int getChar() {
	return isEof() ? -1 : buf[pos++];
}

inline int peekChar() {
	return isEof() ? -1 : buf[pos];
}

inline bool seekEof() {
	int c;
	while ((c = peekChar()) != -1 && c <= 32)
		pos++;
	return c == -1;
}

inline int readChar() {
	int c = getChar();
	while (c != -1 && c <= 32)
		c = getChar();
	return c;
}

inline int readUInt() {
	int c = readChar(), x = 0;
	while ('0' <= c && c <= '9')
		x = x * 10 + c - '0', c = getChar();
	return x;
}

template <class T>
inline T readInt() {
	int s = 1, c = readChar();
	T x = 0;
	if (c == '-')
		s = -1, c = getChar();
	while ('0' <= c && c <= '9')
		x = x * 10 + c - '0', c = getChar();
	return s == 1 ? x : -x;
}

inline double readDouble() {
	int s = 1, c = readChar();
	double x = 0;
	if (c == '-')
		s = -1, c = getChar();
	while ('0' <= c && c <= '9')
	x = x * 10 + c - '0', c = getChar();
	if (c == '.') {
		c = getChar();
		double coef = 1;
		while ('0' <= c && c <= '9')
			x += (c - '0') * (coef *= 1e-1), c = getChar();
	}
	return s == 1 ? x : -x;
}

inline void readWord(char *s) {
	int c = readChar();
	while (c > 32)
		*s++ = c, c = getChar();
	*s = 0;
}

inline bool readLine(char *s) {
	int c = getChar();
	while (c != '\n' && c != -1)
		*s++ = c, c = getChar();
	*s = 0;
	return c != -1;
}

int writePos = 0;
char writeBuf[BUF_SIZE];

inline void writeChar(int x) {
	if (writePos == BUF_SIZE)
		fwrite(writeBuf, 1, BUF_SIZE, stdout), writePos = 0;
	writeBuf[writePos++] = x;
}

inline void flush() {
	if (writePos)
		fwrite(writeBuf, 1, writePos, stdout), writePos = 0;
}

template <class T>
inline void writeInt(T x, int outputLen) {
	if (x < 0)
		writeChar('-'), x = -x;

	char s[24];
	int n = 0;
	while (x || !n)
		s[n++] = '0' + x % 10, x /= 10;
	while (n < outputLen)
		s[n++] = '0';
	while (n--)
		writeChar(s[n]);
}

template <class T>
inline void writeUInt(T x, int outputLen) {
	char s[24];
	int n = 0;
	while (x || !n)
		s[n++] = '0' + char(x % 10), x /= 10;
	while (n < outputLen)
		s[n++] = '0';
	while (n--)
		writeChar(s[n]);
}

inline void writeWord(const char *s) {
	while (*s)
		writeChar(*s++);
}

inline void writeDouble(double x, int outputLen) {
	if (x < 0)
		writeChar('-'), x = -x;
	int t = (int) x;
	writeUInt(t), x -= t;
	writeChar('.');
	for (int i = outputLen - 1; i > 0; i--) {
		x *= 10;
		t = std::min(9, (int) x);
		writeChar('0' + t), x -= t;
	}
	x *= 10;
	t = std::min(9, (int)(x + 0.5));
	writeChar('0' + t);
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

vector<VI> grid,mark,sm;
int n,m;
char s[1010000];

bool check(int fl,int fr,int gl,int gr) {
	if (fl<=0||fr>n||gl<=0||gr>m) return 0;
	return sm[fr][gr]-sm[fl-1][gr]-sm[fr][gl-1]+sm[fl-1][gl-1]==(fr-fl+1)*(gr-gl+1);
}
bool gao(int md) {
	mark=vector<VI>(n+1,VI(m+1,0));
	vector<VI> ff(n+2,VI(m+2,0));
	rep(i,1,n+1) rep(j,1,m+1) {
		if (check(i-md,i+md,j-md,j+md)) {
			ff[i-md][j-md]+=1;
			ff[i+md+1][j-md]-=1;
			ff[i-md][j+md+1]-=1;
			ff[i+md+1][j+md+1]+=1;
			mark[i][j]=1;
		}
	}
	rep(i,1,n+1) rep(j,1,m+1) ff[i][j]+=ff[i][j-1];
	rep(i,1,n+1) rep(j,1,m+1) ff[i][j]+=ff[i-1][j];
	rep(i,1,n+1) rep(j,1,m+1) if ((ff[i][j]>0)!=grid[i][j]) {
		return 0;
	}
	return 1;
}

int main() {
	scanf("%d%d",&n,&m);
	grid=vector<VI>(n+1,VI(m+1,0));
	sm=vector<VI>(n+1,VI(m+1,0));
	rep(i,1,n+1) {
		scanf("%s",s);
		rep(j,1,m+1) {
			sm[i][j]=grid[i][j]=(s[j-1]=='X');
		}
	}
	rep(i,1,n+1) rep(j,1,m+1) sm[i][j]+=sm[i][j-1];
	rep(i,1,n+1) rep(j,1,m+1) sm[i][j]+=sm[i-1][j];
	int l=0,r=600;
	while (l+1<r) {
		int md=(l+r)>>1;
		if (gao(md)) l=md; else r=md;
	}
	gao(l);
	printf("%d\n",l);
	rep(i,1,n+1) {
		rep(j,1,m+1) if (mark[i][j]) putchar('X'); else putchar('.');
		puts("");
	}
}


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

const int maxn = 1e6;
const int maxm = 1e6;
int n,m;
char cstr[maxm + 2];

vector<int> mp[maxn + 2];
vector<int> sum[maxn + 2];

bool check(int xl, int xr, int yl, int yr)
{
	if (xl - 1 > n || xr > n || yl - 1 > m || yr > m) return false;
	else return sum[xr][yr] - sum[xr][yl - 1] - sum[xl - 1][yr] + sum[xl - 1][yl - 1] == (xr - xl + 1) * (yr - yl + 1);
}

int main()
{
	scanf("%u%u", &n, &m);
	for (int i = 0; i <= n + 1; ++i)
	{
		mp[i].resize(m + 2);
		sum[i].resize(m + 2);
	}
	for (int i = 1; i <= n; ++i)
	{
		scanf("%s", cstr + 1);
		for (int j = 1; j <= m; ++j)
		{
			sum[i][j] = mp[i][j] = (cstr[j] == 'X');
		}
	}
	for (int i = 1; i <= n + 1; ++i)
		for (int j = 1; j <= m + 1; ++j)
			sum[i][j] += sum[i][j - 1];

	for (int i = 1; i <= n + 1; ++i)
		for (int j = 1; j <= m + 1; ++j)
			sum[i][j] += sum[i - 1][j];

	int l = 0, r = min(n,m);
	while (l <= r)
	{
		int mid = l + r >> 1;
		int d = mid + mid + 1;
		bool flag = 1;
		for (int i = 1; i <= n && flag; ++i)
		{
			for (int j = 1; j <= m && flag; ++j)
			{
				if (!mp[i][j])  continue;
				if (!mp[i - 1][j] && !check(i, i + d - 1, j, j))
				{
					flag = false;
					continue;
				}
				if (!mp[i + 1][j] && !check(i - d + 1, i, j, j))
				{
					flag = false;
					continue;
				}
				if (!mp[i][j - 1] && !check(i, i, j, j + d - 1))
				{
					flag = false;
					continue;
				}
				if (!mp[i][j + 1] && !check(i, i, j - d + 1, j))
				 {
					flag = false;
					continue;
				}
				if (!mp[i - 1][j] && !mp[i][j - 1] && !check(i, i + d - 1, j, j + d - 1))
				 {
					flag = false;
					continue;
				}
				if (!mp[i + 1][j] && !mp[i][j - 1] && !check(i - d + 1, i, j, j + d - 1))
				{
					flag = false;
					continue;
				}
				if (!mp[i - 1][j] && !mp[i][j + 1] && !check(i, i + d - 1, j - d + 1, j))
				{
					flag = false;
					continue;
				}
				if (!mp[i + 1][j] && !mp[i][j + 1] && !check(i - d + 1, i, j - d + 1, j))
				{
					flag = false;
					continue;
				}
			}
		}
		if (flag) l = mid + 1; else r = mid - 1;
	}
	std::printf("%u\n", l);
	for (int i = 1; i <= n; ++i,putchar(10))
	{
		for (int j = 1; j <= m; ++j)
		{
			if (check(i - l, i + l, j - l, j + l)) {
				std::putchar('X');
			} else {
				std::putchar('.');
			}
		}

	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll score2[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n;
ll k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,min(mn[v],score2[v])+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v];
	int p=lower_bound(memo,memo+sz,dep[v]-k)-memo;
	int last=p==0?n+1:vertex[p-1];
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v],last);
	int pq=vertex[seg2.right(q,seg2.mum)];
	if(score2[pq]+dep[pq]==q)
	{
		ll vl=mn[pq]+dep[pq];
		if(pq+1<sz) vl=min(vl,seg2.get(pq+1,sz));
		if(sum[nxt[pq]-1]<=vl) to[v]=max(to[v],nxt[pq]-1);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %lld : %d %lld\n",v,nxt[v],to[v],score2[v],p,q);
}
int main()
{
	scanf("%d %lld",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[i]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		score2[i]=sum[nxt[i]-1]-pl[i];
		pos[sz++]=P(sum2[i],i);
	}
	//seg.init(n+3);//�g���܂킷
	//for(int i=0;i<=n;i++) seg.ins(i,sum[i]);
	seg2.init(n+3);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll score2[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n;
ll k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,min(mn[v],score2[v])+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v];
	int p=lower_bound(memo,memo+sz,dep[v]-k)-memo;
	int last=p==0?n+1:vertex[p-1];
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v],last);
	int qr=seg2.right(q,seg2.mum);
	int pq=vertex[qr];
	if(score2[pq]+dep[pq]==q)
	{
		ll vl=mn[pq]+dep[pq];
		if(pq+1<sz) vl=min(vl,seg2.get(qr+1,sz));
		//printf("%d %d : %lld\n",v,qr,vl);
		if(sum[nxt[pq]-1]<=vl+k-dep[v]) to[v]=max(to[v],nxt[pq]-1);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %d : %d %lld\n",v,nxt[v],to[v],pq,p,q);
}
int main()
{
	scanf("%d %lld",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[i]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		score2[i]=sum[nxt[i]-1]-pl[i];
		pos[sz++]=P(sum2[i],i);
	}
	//seg.init(n+3);//�g���܂킷
	//for(int i=0;i<=n;i++) seg.ins(i,sum[i]);
	seg2.init(n+3);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll score2[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n;
ll k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,min(mn[v],score2[v])+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v];
	int p=lower_bound(memo,memo+sz,dep[v]-k)-memo;
	int last=p==0?n+1:vertex[p-1]+1;
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v],last);
	if(to[v]==-1)
	{
		int pq=vertex[seg2.right(q,seg.mum)];
		to[v]=nxt[pq]-1;
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %lld : %d %lld\n",v,nxt[v],to[v],score2[v],p,q);
}
int main()
{
	scanf("%d %lld",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[i]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		score2[i]=sum[nxt[i]-1]-pl[i];
		pos[sz++]=P(sum2[i],i);
	}
	//seg.init(n+3);//�g���܂킷
	//for(int i=0;i<=n;i++) seg.ins(i,sum[i]);
	seg2.init(n+3);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll score2[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n;
ll k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,min(mn[v],score2[v])+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v];
	int p=lower_bound(memo,memo+sz,dep[v]-k)-memo;
	//p �̍Ōゾ���ЂƂ܂����O
	seg2.ins(p,mn[vertex[p]]+dep[vertex[p]]);
	int last=p==0?n+1:vertex[p-1];
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v],last);
	seg2.ins(p,min(mn[vertex[p]],score2[vertex[p]])+dep[vertex[p]]);
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %d : %d %lld\n",v,nxt[v],to[v],pq,p,q);
}
int main()
{
	scanf("%d %lld",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[i]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		score2[i]=sum[nxt[i]-1]-pl[i];
		pos[sz++]=P(sum2[i],i);
	}
	//seg.init(n+3);//�g���܂킷
	//for(int i=0;i<=n;i++) seg.ins(i,sum[i]);
	seg2.init(n+3);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll score2[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n;
ll k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,min(mn[v],score2[v])+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v];
	int p=lower_bound(memo,memo+sz,dep[v]-k)-memo;
	int last=p==0?n+1:vertex[p-1];
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v],last);
	int pq=vertex[seg2.right(q,seg2.mum)];
	if(score2[pq]+dep[pq]==q)
	{
		ll vl=mn[pq]+dep[pq];
		if(pq>p) vl=min(vl,seg2.get(p,pq));
		if(sum[nxt[pq]-1]<=vl) to[v]=max(to[v],nxt[pq]-1);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %lld : %d %lld\n",v,nxt[v],to[v],score2[v],p,q);
}
int main()
{
	scanf("%d %lld",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[i]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		score2[i]=sum[nxt[i]-1]-pl[i];
		pos[sz++]=P(sum2[i],i);
	}
	//seg.init(n+3);//�g���܂킷
	//for(int i=0;i<=n;i++) seg.ins(i,sum[i]);
	seg2.init(n+3);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n,k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,mn[v]+dep[v]);
	vertex[sz]=-v;
	memo[sz++]=dep[v]+pl[v];
	int p=lower_bound(memo,memo+sz,dep[v]+pl[v]-k)-memo;
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v]-pl[v],p==0?n+1:-vertex[p-1]);
	//printf("%d %d\n",v,to[v]);
	int back=lower_bound(vertex,vertex+sz,-to[v])-vertex;
	if(back==sz||back==p||seg2.get(p,back)!=q)
	{
		if(back==sz) back=seg2.right(q,seg2.mum);//back==sz�ɂȂ�̂́A�ŏ��l���S�������Ƃ���ɑ����Ă�Ƃ�
		to[v]=seg.right(q+k-dep[v]-pl[v]+pl[back],back==0?n+1:-vertex[back-1]);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %d: %d %lld\n",v,nxt[v],to[v],back,p,q);
}
int main()
{
	scanf("%d %d",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[nxt[i]]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		pos[sz++]=P(sum2[i],i);
	}
	seg.init(n+2);//�g���܂킷
	for(int i=0;i<=n;i++) seg.ins(i,mn[i]);
	seg2.init(n+2);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)//root
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll score2[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n;
ll k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,min(mn[v],score2[v])+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v];
	int p=lower_bound(memo,memo+sz,dep[v]-k)-memo;
	int last=p==0?n+1:vertex[p-1];
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v],last);
	int qr=seg2.right(q,seg2.mum);
	int pq=vertex[qr];
	if(score2[pq]+dep[pq]==q)
	{
		ll vl=mn[pq]+dep[pq];
		if(qr+1<sz) vl=min(vl,seg2.get(qr+1,sz));
		//printf("%d %d : %lld\n",v,qr,vl);
		if(sum[nxt[pq]-1]<=vl+k-dep[v]) to[v]=max(to[v],nxt[pq]-1);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %d : %d %lld\n",v,nxt[v],to[v],pq,p,q);
}
int main()
{
	scanf("%d %lld",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[i]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		score2[i]=sum[nxt[i]-1]-pl[i];
		pos[sz++]=P(sum2[i],i);
	}
	//seg.init(n+3);//�g���܂킷
	//for(int i=0;i<=n;i++) seg.ins(i,sum[i]);
	seg2.init(n+3);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n,k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,mn[v]+dep[v]);
	vertex[sz]=-v;
	memo[sz++]=dep[v]+pl[v];
	int p=lower_bound(memo,memo+sz,dep[v]+pl[v]-k)-memo;
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v]-pl[v],p==0?n+1:-vertex[p-1]);
	//printf("%d %d\n",v,to[v]);
	int back=lower_bound(vertex,vertex+sz,-to[v])-vertex;
	if(back==sz||seg2.get(p,back)!=q)
	{
		if(back==sz) back=seg2.right(q,seg2.mum);//back==sz�ɂȂ�̂́A�ŏ��l���S�������Ƃ���ɑ����Ă�Ƃ�
		to[v]=seg.right(q+k-dep[v]-pl[v]+pl[back],back==0?n+1:-vertex[back-1]);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %d: %d %lld\n",v,nxt[v],to[v],back,p,q);
}
int main()
{
	scanf("%d %d",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[nxt[i]]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		pos[sz++]=P(sum2[i],i);
	}
	seg.init(n+2);//�g���܂킷
	for(int i=0;i<=n;i++) seg.ins(i,mn[i]);
	seg2.init(n+2);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)//root
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n,k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,mn[v]+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v]+pl[v];
	int p=lower_bound(memo,memo+sz,dep[v]+pl[v]-k)-memo;
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v]-pl[v],p==0?n+1:vertex[p-1]);
	int back=upper_bound(vertex,vertex+sz,to[v])-vertex;
	if(back==p||seg2.get(p,back)!=q)
	{
		to[v]=seg.right(q+k-dep[v]-pl[v]+pl[back],p==0?n+1:vertex[p-1]);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d : %d %lld\n",v,nxt[v],to[v],p,q);
}
int main()
{
	scanf("%d %d",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[nxt[i]]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		pos[sz++]=P(sum2[i],i);
	}
	seg.init(n+2);//�g���܂킷
	for(int i=0;i<=n;i++) seg.ins(i,mn[i]);
	seg2.init(n+2);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)//root
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n;
ll k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,mn[v]+dep[v]);
	vertex[sz]=-v;
	memo[sz++]=dep[v]+pl[v];
	int p=lower_bound(memo,memo+sz,dep[v]+pl[v]-k)-memo;
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v]-pl[v],p==0?n+1:-vertex[p-1]);
	//printf("%d %d\n",v,to[v]);
	int back=lower_bound(vertex,vertex+sz,-to[v])-vertex;
	if(back==sz||back==p||seg2.get(p,back)!=q)
	{
		if(back==sz) back=seg2.right(q,sz);//back==sz�ɂȂ�̂́A�ŏ��l���S�������Ƃ���ɑ����Ă�Ƃ�
		to[v]=seg.right(q+k-dep[v]-pl[v]+pl[back],back==0?n+1:-vertex[back-1]);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %d: %d %lld\n",v,nxt[v],to[v],back,p,q);
}
int main()
{
	scanf("%d %lld",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[nxt[i]]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		pos[sz++]=P(sum2[i],i);
	}
	seg.init(n+2);//�g���܂킷
	for(int i=0;i<=n;i++) seg.ins(i,mn[i]);
	seg2.init(n+2);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)//root
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n,k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,mn[v]+dep[v]);
	vertex[sz]=-v;
	memo[sz++]=dep[v]+pl[v];
	int p=lower_bound(memo,memo+sz,dep[v]+pl[v]-k)-memo;
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v]-pl[v],p==0?n+1:-vertex[p-1]);
	//printf("%d %d\n",v,to[v]);
	int back=lower_bound(vertex,vertex+sz,-to[v])-vertex;
	if(back==sz) back=p;
	if(back==p||seg2.get(p,back)!=q)
	{
		to[v]=seg.right(q+k-dep[v]-pl[v]+pl[back],back==0?n+1:-vertex[back-1]);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %d: %d %lld\n",v,nxt[v],to[v],back,p,q);
}
int main()
{
	scanf("%d %d",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[nxt[i]]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		pos[sz++]=P(sum2[i],i);
	}
	seg.init(n+2);//�g���܂킷
	for(int i=0;i<=n;i++) seg.ins(i,mn[i]);
	seg2.init(n+2);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)//root
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n,k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,mn[v]+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v];
	int p=lower_bound(memo,memo+sz,dep[v]+pl[v]-k)-memo;
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v]-pl[v],p==0?n+1:vertex[p-1]);
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d : %d %lld\n",v,nxt[v],to[v],p,q);
}
int main()
{
	scanf("%d %d",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[nxt[i]]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		pos[sz++]=P(sum2[i],i);
	}
	seg.init(n+2);//�g���܂킷
	for(int i=0;i<=n;i++) seg.ins(i,mn[i]);
	seg2.init(n+2);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)//root
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll score2[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n;
ll k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,min(mn[v],score2[v])+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v];
	int p=lower_bound(memo,memo+sz,dep[v]-k)-memo;
	int last=p==0?n+1:vertex[p-1];
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v],last);
	int pq=vertex[seg2.right(q,seg2.mum)];
	if(score2[pq]+dep[pq]==q)
	{
		ll vl=mn[pq]+dep[pq];
		if(pq+1<sz) vl=min(vl,seg2.get(pq+1,sz));
		if(sum[nxt[pq]-1]<=vl+k-dep[v]) to[v]=max(to[v],nxt[pq]-1);
	}
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %lld : %d %lld\n",v,nxt[v],to[v],score2[v],p,q);
}
int main()
{
	scanf("%d %lld",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[i]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		score2[i]=sum[nxt[i]-1]-pl[i];
		pos[sz++]=P(sum2[i],i);
	}
	//seg.init(n+3);//�g���܂킷
	//for(int i=0;i<=n;i++) seg.ins(i,sum[i]);
	seg2.init(n+3);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 100005
#define BT 1024*128*2
#define INF 100000000000000000LL

using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;

struct segtree
{
	ll seg[BT];
	int mum;

	void init(int n)
	{
		mum=1;
		while(mum<n) mum<<=1;
		for(int i=0;i<mum*2;i++) seg[i]=INF;
	}
	void ins(int k,ll x)
	{
		k+=mum-1;
		seg[k]=x;
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=min(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll get(int a,int b,int k,int l,int r)
	{
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		else
		{
			ll vl=get(a,b,k*2+1,l,(l+r)/2);
			ll vr=get(a,b,k*2+2,(l+r)/2,r);
			return min(vl,vr);
		}
	}
	ll get(int a,int b)
	{
		return get(a,b,0,0,mum);
	}
	int right(int a,int b,int k,int l,int r,ll lim)
	{
		if(b<=l||r<=a) return -1;
		if(a<=l&&r<=b)
		{
			if(seg[k]>lim) return -1;
			if(l+1==r) return l;
			if(seg[k*2+2]<=lim) return right(a,b,k*2+2,(l+r)/2,r,lim);
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
		else
		{
			int vr=right(a,b,k*2+2,(l+r)/2,r,lim);
			if(vr!=-1) return vr;
			return right(a,b,k*2+1,l,(l+r)/2,lim);
		}
	}
	int right(ll lim,int p)//lim�ȉ��Ap��荶
	{
		if(p==0) return -1;
		return right(0,p,0,0,mum,lim);
	}
};
segtree seg,seg2;
vector <int> vec[SIZE];
int W[SIZE],G[SIZE];
ll sum[SIZE],sum2[SIZE];
ll pl[SIZE],mn[SIZE];
ll score2[SIZE];
ll dep[SIZE];
int nxt[SIZE],to[SIZE];
int vertex[SIZE];
P pos[SIZE];
ll memo[SIZE];
int n;
ll k;
int ans;
int sz;

void dfs(int v)
{
	seg2.ins(sz,min(mn[v],score2[v])+dep[v]);
	vertex[sz]=v;
	memo[sz++]=dep[v];
	int p=lower_bound(memo,memo+sz,dep[v]-k)-memo;
	int last=p==0?n+1:vertex[p-1];
	ll q=seg2.get(p,sz);
	to[v]=seg.right(q+k-dep[v],last);
	int pq=vertex[seg2.right(q,seg2.mum)];
	if(score2[pq]+dep[pq]==q) to[v]=max(to[v],nxt[pq]-1);
	ans=max(ans,to[v]-v+1);
	for(int i=0;i<vec[v].size();i++) dfs(vec[v][i]);
	sz--;
	seg2.ins(sz,INF);
	//printf("%d : %d %d %lld : %d %lld\n",v,nxt[v],to[v],score2[v],p,q);
}
int main()
{
	scanf("%d %lld",&n,&k);n--;
	for(int i=0;i<n;i++) scanf("%d",&W[i]);
	for(int i=0;i<=n;i++) scanf("%d",&G[i]);
	seg.init(n+2);
	for(int i=0;i<=n;i++)
	{
		if(i==0) sum[i]=-G[0];
		else sum[i]=sum[i-1]+W[i-1]-G[i];
		seg.ins(i,sum[i]);
	}
	for(int i=n;i>=0;i--)
	{
		if(i==n) sum2[i]=-G[n];
		else sum2[i]=sum2[i+1]+W[i]-G[i];
	}
	//to[i] : i����n�߂ď��߂Ēǉ����K�v�ȂƂ���
	//�܂�Ai<j �ŁAsum2[i]-sum2[j]>0 <=> sum2[i]>sum2[j] �Ȃ�ŏ���j
	int sz=0;
	for(int i=n;i>=0;i--)
	{
		while(sz>0&&pos[sz-1].first>=sum2[i]) sz--;
		if(sz==0)
		{
			nxt[i]=n+1;
			pl[i]=dep[i]=0;
		}
		else
		{
			nxt[i]=pos[sz-1].second;
			pl[i]=sum2[i]-pos[sz-1].first;
			dep[i]=pl[i]+dep[nxt[i]];
			vec[nxt[i]].push_back(i);
		}
		mn[i]=seg.get(i,nxt[i]);
		score2[i]=sum[nxt[i]-1]-pl[i];
		pos[sz++]=P(sum2[i],i);
	}
	//seg.init(n+3);//�g���܂킷
	//for(int i=0;i<=n;i++) seg.ins(i,sum[i]);
	seg2.init(n+3);
	ans=0;
	for(int i=0;i<=n;i++)
	{
		if(nxt[i]==n+1)
		{
			sz=0;
			dfs(i);
		}
	}
	//for(int i=0;i<=n;i++) printf("%d : %d %d\n",i,nxt[i],to[i]);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define For(i,x,y) for(int i=x,_y=y;i<=_y;++i)
#define Rep(i,x,y) for(int i=x,_y=y;i>=_y;--i)
#define pub push_back
#define pob pop_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
inline int read()
{
	int x=0,c=getchar(),f=0;
	for(;c>'9'||c<'0';f=c=='-',c=getchar());
	for(;c>='0'&&c<='9';x=(x<<1)+(x<<3)+c-'0',c=getchar());
	return f?-x:x;
}
inline void write(int x)
{
	if(x<0)putchar('-'),x=-x;
	if(x>=10)write(x/10);
	putchar(x%10+48);
}
const int N=100000+10;
const ll oo=1ll<<62;
int n,m,nn,ecnt,head[N],ans=1;
int Ql,Qr,nxt[N],q[N];
ll Qmx,Qv,suf[N],need[N],a[N],d[N],s[N];
struct node{int o,l,r;}A[N];
struct edge{int to,nxt;}e[N];
void addedge(int u,int v)
{
	e[++ecnt]=(edge){v,head[u]};head[u]=ecnt;
}
#define ls o<<1
#define rs o<<1|1
#define mid ((l+r)>>1)
#define Ls ls,l,mid
#define Rs rs,mid+1,r
struct data{int pos;ll val;};
data min(const data &A,const data &B){return A.val<B.val?A:B;}
struct seg{data ind1,ind2;ll tag,cost,mx;}tr[N*4];
data calc(int o,int l,int r)
{
	if(l==r)return (data){l,Qmx==-oo?oo:Qmx+tr[o].ind1.val};
	if(Qmx<=tr[ls].mx)return min(calc(Ls),tr[o].ind2);
	return min((data){tr[ls].ind1.pos,tr[ls].ind1.val+Qmx},calc(Rs));
}
void down(int o)
{
	ll &x=tr[o].tag;
	if(x)
	{
		tr[ls].tag+=x;tr[rs].tag+=x;
		tr[ls].cost+=x;tr[rs].cost+=x;
		tr[ls].mx+=x;tr[rs].mx+=x;
		x=0;
	}
}
void up(int o,int l,int r)
{
	tr[o].cost=tr[ls].cost;
	tr[o].mx=max(tr[ls].mx,tr[rs].mx);
	tr[o].ind1=min(tr[ls].ind1,tr[rs].ind1);
	Qmx=tr[ls].mx;
	tr[o].ind2=calc(Rs);
}
void build(int o,int l,int r)
{
	if(l==r){tr[o]=(seg){(data){l,-suf[l]},(data){0,0},0,0,suf[l]};return;}
	build(Ls);build(Rs);
	up(o,l,r);
}
void modify(int o,int l,int r)
{
	if(Ql<=l&&Qr>=r)
	{
		tr[o].ind2.val+=Qv;
		tr[o].tag+=Qv;
		tr[o].cost+=Qv;
		tr[o].mx+=Qv;
		return;
	}
	down(o);
	if(Ql<=mid)modify(Ls);
	if(Qr>mid)modify(Rs);
	up(o,l,r);
}
int find(int o,int l,int r)
{
	if(l==r)return l;
	down(o);
	if(tr[rs].cost<=m)return find(Rs);else return find(Ls);
}
void query(int o,int l,int r)
{
	if(Ql<=l&&Qr>=r){A[++nn]=(node){o,l,r};return;}
	down(o);
	if(Ql<=mid)query(Ls);
	if(Qr>mid)query(Rs);
}
int right(int o,int l,int r)
{
	if(l==r)return l;
	down(o);
	ll tmp=Qmx;
	Qmx=max(Qmx,tr[ls].mx);
	if(calc(Rs).val<=m)return right(Rs);
	Qmx=tmp;
	return right(Ls);
}
int getans()
{
	s[0]=-oo;
	For(i,1,nn)s[i]=max(s[i-1],tr[A[i].o].mx);
	Rep(i,nn,1)
	{
		Qmx=s[i-1];
		if(calc(A[i].o,A[i].l,A[i].r).val<=m)return right(A[i].o,A[i].l,A[i].r);
	}
	return 0;
}
void dfs(int u)
{
	if(nxt[u]!=n+1)Ql=nxt[u]-1,Qr=n,Qv=need[u],modify(1,1,n);
	int tmp=tr[1].cost>m?0:find(1,1,n);
	tmp=tmp==n?n-1:tmp;
	nn=0;
	Ql=u,Qr=tmp+1,query(1,1,n);
	ans=max(ans,getans()-u+1);
	for(int i=head[u];i;i=e[i].nxt)dfs(e[i].to);
	if(nxt[u]!=n+1)Ql=nxt[u]-1,Qr=n,Qv=-need[u],modify(1,1,n);
}
int main()
{
	n=read(),m=read();
	For(i,1,n-1)d[i]=read();
	For(i,1,n)a[i]=read();
	For(i,2,n)suf[i]=suf[i-1]+a[i]-d[i-1];
	int h=0,t=-1;
	ll cha=0;
	For(i,1,n-1)
	{
		q[++t]=i;
		cha+=a[i]-d[i];
		for(;h<=t&&cha<0;++h)
		{
			nxt[q[h]]=i+1;
			need[q[h]]=-cha;
			addedge(i+1,q[h]);
			cha-=a[q[h]]-d[q[h]];
		}
	}
	For(i,h,t)nxt[q[i]]=n+1;nxt[n]=n+1;
	build(1,1,n);
	For(i,1,n)if(nxt[i]==n+1)dfs(i);
	printf("%d\n",ans);
}


// pb_ds big CAT
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
#define RG register
#define IL __inline__ __attribute__((always_inline))
#define For(i, a, b) for(RG int i = a, ___u = b; i <= ___u; ++i)
#define Dor(i, a, b) for(RG int i = b, ___d = a; i >= ___d; --i)
#define Rep(i, a, b) for(RG int i = a, ___u = b; i != ___u; ++i)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
#define cmin(a, b) ((a) > (b) ? (a) = (b), 1 : 0)
#define cmax(a, b) ((a) < (b) ? (a) = (b), 1 : 0)
#define diff(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
#define dabs(i) ((i) > 0 ? (i) : -(i))
typedef long long ll;

namespace pb_ds
{
	// 输入输出优化模板从此开始
	namespace io
	{
		const int MaxBuff = 1 << 15;
		const int Output = 1 << 21;
		char B[MaxBuff], *S = B, *T = B;
		#define getc() ((S == T) && (T = (S = B) + fread(B, 1, MaxBuff, stdin), S == T) ? 0 : *S++)
		char Out[Output], *iter = Out;
		IL void flush()
		{
			fwrite(Out, 1, iter - Out, stdout);
			iter = Out;
		}
	}

	template<class Type> IL Type read()
	{
		using namespace io;
		RG char ch; RG Type ans = 0; RG bool neg = 0;
		while(ch = getc(), (ch < '0' || ch > '9') && ch != '-')	 ;
		ch == '-' ? neg = 1 : ans = ch - '0';
		while(ch = getc(), '0' <= ch && ch <= '9') ans = ans * 10 + ch - '0';
		return neg ? -ans : ans;
	}
	template<class Type> IL void print(RG Type x, RG char ch = '\n')
	{
		using namespace io;
		if(!x) *iter++ = '0';
		else
		{
			if(x < 0) *iter++ = '-', x = -x;
			static int S2[100]; RG int t = 0;
			while(x) S2[++t] = x % 10, x /= 10;
			while(t) *iter++ = '0' + S2[t--];
		}
		*iter++ = ch;
	}
	// 输入输出优化模板到此结束
#if 1
	const int MaxN = 131072;
	const ll inf = 1000000000000000000ll;
	int N, K, len[MaxN], gas[MaxN];
	ll old[MaxN], add[MaxN];

	struct Pointer{int to; Pointer *next;} *fir[MaxN], mem[MaxN];

	#define SonL (i << 1)
	#define SonR (i << 1 | 1)
	#define RanL l, m
	#define RanR m + 1, r
	#define NeedL ql <= m
	#define NeedR m < qr

	namespace Input
	{
		IL void main()
		{
			RG int (*F)() = read<int>;
			N = F(); K = F();
			Rep(i, 1, N) len[i] = F();
			For(i, 1, N) gas[i] = F();
		}
	}

	namespace BuildTree
	{
		ll sum[MaxN];
		int stack[MaxN];

		IL void main()
		{
			Rep(i, 1, N) sum[i] = sum[i - 1] + gas[i] - len[i];
			RG int top = 0, fai; RG ll tmp;
			stack[top] = N;
			stack[++top] = N - 1;
			RG Pointer *tot = mem;
			Dor(i, 1, N - 1)
			{
				tmp = sum[i - 1];
				while(top && tmp < sum[stack[top]]) --top;
				fai = stack[top];
				add[i] = (top ? sum[i - 1] - sum[fai++] : 0);
				stack[++top] = i - 1;
				*++tot = (Pointer) {i, fir[fai]}; fir[fai] = tot;
///*lgg*/printf("i = %d tmp = %lld fa = %d add = %lld\n", i, tmp, fai, add[i]);
			}
		}
	}

	struct Data
	{
		ll min_old, max_cur, min_ans, rig_con;
		ll add;
		IL void do_add(RG ll x)
		{
			add += x;
			max_cur += x;
			min_ans += x;
			rig_con += x;
		}
		bool is_leaf;
	}	T[MaxN << 1];

	IL void push_down(RG int i)
	{
		if(T[i].add)
		{
			T[SonL].do_add(T[i].add);
			T[SonR].do_add(T[i].add);
			T[i].add = 0;
		}
	}

	IL ll query_cut(RG int i, RG ll cut)
	{
		RG ll ans = inf;
		while(!T[i].is_leaf && cut < T[i].max_cur)
		{
			if(cut <= T[SonL].max_cur)
			{
				cmin(ans, T[i].rig_con);
				i = SonL;
			}
			else
			{
				cmin(ans, cut + T[SonL].min_old);
				i = SonR;
			}
		}
		cmax(cut, T[i].max_cur);
		cmin(ans, cut + T[i].min_old);
		return ans;
	}

	IL void push_up(RG int i)
	{
		RG Data *p = T + i;
		if(T[SonL].max_cur >= T[SonR].max_cur)
		{
			p->max_cur = T[SonL].max_cur;
			p->rig_con = T[SonL].max_cur + T[SonR].min_old;
		}
		else
		{
			p->max_cur = T[SonR].max_cur;
			p->rig_con = query_cut(SonR, T[SonL].max_cur);
		}
		p->min_ans = dmin(T[SonL].min_ans, p->rig_con);
	}

/*lgg*/ll bf_sum[MaxN];
	void build(RG int i, RG int l, RG int r)
	{
/*lgg*/For(i,1,N) bf_sum[i]=old[i];return;
		if(l == r)
		{
			T[i] = (Data) {-old[i], old[i], 0ll};
			T[i].is_leaf = 1;
		}
		else
		{
			RG int m = (l + r) >> 1;
			build(SonL, RanL);
			build(SonR, RanR);
			T[i].min_old = dmin(T[SonL].min_old, T[SonR].min_old);
			push_up(i);
		}
	}

	int ql, qr, qp; ll qv;
	void modify(RG int i, RG int l, RG int r) // [ql, qr] += qv
	{
/*lgg*/For(i, ql, qr) bf_sum[i] += qv;
///*lgg*/printf("modify bf_sum = "); For(i, 1, N) printf("%lld ", bf_sum[i]); puts("");
/*lg*/ return;
		if(ql <= l && r <= qr)
			return T[i].do_add(qv);
		RG int m = (l + r) >> 1;
		push_down(i);
		if(NeedL) modify(SonL, RanL);
		if(NeedR) modify(SonR, RanR);
		push_up(i);
	}
	void query(RG int i, RG int l, RG int r, RG ll qc)
	{
/*lgg*/RG ll max = -inf; For(i, ql, qr) { if(max - old[i] <= K) qp = i; cmax(max, bf_sum[i]);} return;
		if(ql <= l && r <= qr)
		{
			RG ll qv = query_cut(i, qc);
/*lgg*/printf("query [%d, %d] qv = %lld\n", l, r, qv);
			if(qv > K) {return;}
			if(l == r) {qp = l; return;}
		}
		RG int m = (l + r) >> 1;
		push_down(i);
		// right first
		if(!qp && NeedR) 	query(SonR, RanR, dmax(qc, T[SonL].max_cur));
		if(!qp && NeedL) 	query(SonL, RanL, qc);
	}

	int ans = 1;
	void dfs_make(RG int p)
	{
		qp = 0;
		ql = p; qr = N;
		query(1, 1, N, -inf);
		cmax(ans, qp - p + 1);
///*lgg*/printf("dfs_make p = %d qp = %d\n", p, qp);
		for(RG Pointer *k = fir[p]; k; k = k->next)
		{
			ql = p - 1; qr = N;
			qv = add[k->to];
			modify(1, 1, N);

			dfs_make(k->to);

			ql = p - 1; qr = N;
			qv = -add[k->to];
			modify(1, 1, N);
		}
	}

	IL void main()
	{
		Input::main();
		BuildTree::main();
		For(i, 1, N) old[i] = old[i - 1] + gas[i] - len[i - 1];
///*lgg*/For(i, 1, N) printf("old[%d] = %lld\n", i, old[i]);
		build(1, 1, N);
		dfs_make(N);
		print(ans);
	}

#endif
}

int main()
{
	#define FILE "bin"
	// freopen(FILE ".in", "r", stdin); freopen(FILE ".out", "w", stdout);
	pb_ds::main();
	pb_ds::io::flush();
	fclose(stdin); fclose(stdout);
}

#if 0
#endif