#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
int par[N];
int ans[N];
bool mark[N];
int n,m,q;
int d[N];
bool gmark[N];
vector <int> v[N];
vector <int> bad;
void clear()
{
	for (int i = 0 ;i < bad.size(); ++i) mark[bad[i]] = false, d[bad[i]] = -1;
}
void dfs (int s, int head, int dd)
{
	par[s] = head;
	mark[s] = true;
	gmark[s] = true;
	bad.insert(s);
	d[s] = dd;
	for (int i = 0 ; i < v[s].size(); ++i)
		if (!mark[v[s][i]]) dfs(v[s][i], head, dd+1);
}
int parent (int x)
{
	if(x == par[x])
		return x;
	par[x] = parent (par[x]);
	return par[x];
}
int main()
{
	scanf ("%d%d%d" , &n , &m, &q);
	int x,y;
	for (int i = 0 ;i < m; ++i)
	{
		scanf ("%d%d", &x, &y);
		x--, y--;
		v[x].push_back (y);
		v[y].push_back (x);
	}
	int help, id;
	for (int i = 0 ;i < n; ++i)
		if (!gmark[i])
		{
			help = i;
			clear();
			dfs (i, i, 0);
			help = -1;
			for (int j = 0 ;j < N; ++j)
				if ((d[j] != -1) and (help < d[j]))
				{
					help = d[j];
					id = j;
				}
			clear();
			dfs (id, i, 0);
			help  = -1;
			for (int j = 0 ;j < N; ++j)
				help = max (help, d[j]);
			ans[i] = help;
		}
	for (int i = 0;i < q; ++i)
	{
		scanf ("%d%d", &help, &x);
		x --;
		if (help == 2)
		{

			scanf ("%d", &y);y--;
			parent (x);
			parent (y);
			if (par[x] == par[y]) continue;
			ans[par[x]] = max (max (ans[par[y]], ans[par[x]]),(((ans[par[y]]+1)/2) + ((ans[par[x]]+1)/2))+1); //cout << ans[par[x]] << endl;
			par[par[y]] = par[x];
			continue;
		}
		parent(x);
		printf ("%d\n", ans[par[x]]);
	}

}


#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
int par[N];
int ans[N];
bool mark[2][N];
int n,m,q;
int d[N];
bool gmark[N];
vector <int> v[N];
vector <int> bad;
int maa, idd;
void dfs (int s, int head, int dd, int flag)
{
//	cout << endl << " s : " << s << "  head : " << head << "  dd : " << dd << " flag : " << flag << endl;
	if (maa < dd)
	{
		maa = dd;
		idd = s;
	}
	par[s] = head;
	mark[flag][s] = true;
	gmark[s] = true;
	bad.push_back(s);
	d[s] = dd;
	for (int i = 0 ; i < v[s].size(); ++i)
		if (!mark[flag][v[s][i]]) dfs(v[s][i], head, dd+1, flag);
}
int parent (int x)
{
	if(x == par[x])
		return x;
	par[x] = parent (par[x]);
	return par[x];
}
int main()
{
	scanf ("%d%d%d" , &n , &m, &q);
	int x,y;
	for (int i = 0 ;i < m; ++i)
	{
		scanf ("%d%d", &x, &y);
		x--, y--;
		v[x].push_back (y);
		v[y].push_back (x);
	}
	int help, id;
	for (int i = 0 ;i < n; ++i)
		if (!gmark[i])
		{
	//		help = i;
	//		clear();
			maa = -1;
			dfs (i, i, 0,0);
	//		help = -1;
	/*		for (int j = 0 ;j < N; ++j)
				if ((d[j] != -1) and (help < d[j]))
				{
					help = d[j];
					id = j;
				}

			clear();
			*/
			maa = -1;
			dfs (idd, i, 0,1);
		//	help  = -1;
		//	for (int j = 0 ;j < N; ++j)
		//		help = max (help, d[j]);
			ans[i] = maa;
		//	cout << "\nans[ " << i << "] : " << ans[i] << endl;
		}
	for (int i = 0;i < q; ++i)
	{
		scanf ("%d%d", &help, &x);
		x --;
		if (help == 2)
		{

			scanf ("%d", &y);y--;
			parent (x);
			parent (y);
			if (par[x] == par[y]) continue;
			ans[par[x]] = max (max (ans[par[y]], ans[par[x]]),(((ans[par[y]]+1)/2) + ((ans[par[x]]+1)/2))+1); //cout << ans[par[x]] << endl;
			par[par[y]] = par[x];
			continue;
		}
		parent(x);
		printf ("%d\n", ans[par[x]]);
	}

}


#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
vector <int> v[N];
int ans[N];
int par[N];
int n,m,q;
int d[N];
int mini;
bool mark[N], mark1[N];
void clear()
{
	for (int i = 0 ;i < N; ++i) d[i] = -1, mark1[i] = false;
	mini = 1e9;
}
void dfs (int s, int x = 0)
{
	d[s] = x;
	mark1[s] = true;
	mini = min (mini, s);
	for (int i = 0 ;i < v[s].size(); ++i)
		if (!mark1[i])
			dfs (v[s][i], x+1);
}
int find_par (int x)
{
	if (par[x] == x) return x;
	par[x] = find_par (par[x]);
	return par[x];
}
int main()
{
	cin >> n >> m >> q;
	int help1, help2;
	for (int i = 0 ;i < n; ++i) par[i] = i;
	for (int  i = 0 ;i < m; ++i)
	{
		cin >> help1 >> help2;
		v[help1].push_back (help2);
		v[help2].push_back (help1);
	}
	for (int i= 0 ; i < n; ++i)
		if(!mark[i])
		{
			clear();
			dfs (i);
			help1 = -1;

			for (int i =0; i < n; ++i)
				if (help1 < d[i])
				{
					help1 = d[i];
					help2 = i;
				}
			dfs (help2);
			for (int i = 0 ;i < n; ++i)
				if (d[i] != -1)
					par[i] = mini;
			help1 = -1;
			for (int i= 0 ;i < n; ++i) help1 = max (help1, d[i]);
			ans[mini] = help1;
		}
	int help3;
	int x1,x2;
	for (int i = 0 ;i < q; ++i)
	{
		cin >> help3 >> help1 ;
		if (help3 == 1)
		{
			cout << ans[find_par(help1)] << endl;
			continue;
		}
		cin >> help2;
		x1 = find_par (help1), x2 = find_par (help2);
		ans[x2] = (ans[x2]+1)/2 + (ans[x1]+1)/2; ans[x2] ++;
		par[x1] = x2;

	}

}


#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
int par[N];
int ans[N];
bool mark[N];
int n,m,q;
int d[N];
bool gmark[N];
vector <int> v[N];
vector <int> bad;
void clear()
{
	for (int i = 0 ;i < bad.size(); ++i) mark[bad[i]] = false, d[bad[i]] = -1;
}
void dfs (int s, int head, int dd)
{
	par[s] = head;
	mark[s] = true;
	gmark[s] = true;
	bad.push_back(s);
	d[s] = dd;
	for (int i = 0 ; i < v[s].size(); ++i)
		if (!mark[v[s][i]]) dfs(v[s][i], head, dd+1);
}
int parent (int x)
{
	if(x == par[x])
		return x;
	par[x] = parent (par[x]);
	return par[x];
}
int main()
{
	scanf ("%d%d%d" , &n , &m, &q);
	int x,y;
	for (int i = 0 ;i < m; ++i)
	{
		scanf ("%d%d", &x, &y);
		x--, y--;
		v[x].push_back (y);
		v[y].push_back (x);
	}
	int help, id;
	for (int i = 0 ;i < n; ++i)
		if (!gmark[i])
		{
			help = i;
			clear();
			dfs (i, i, 0);
			help = -1;
			for (int j = 0 ;j < N; ++j)
				if ((d[j] != -1) and (help < d[j]))
				{
					help = d[j];
					id = j;
				}
			clear();
			dfs (id, i, 0);
			help  = -1;
			for (int j = 0 ;j < N; ++j)
				help = max (help, d[j]);
			ans[i] = help;
		}
	for (int i = 0;i < q; ++i)
	{
		scanf ("%d%d", &help, &x);
		x --;
		if (help == 2)
		{

			scanf ("%d", &y);y--;
			parent (x);
			parent (y);
			if (par[x] == par[y]) continue;
			ans[par[x]] = max (max (ans[par[y]], ans[par[x]]),(((ans[par[y]]+1)/2) + ((ans[par[x]]+1)/2))+1); //cout << ans[par[x]] << endl;
			par[par[y]] = par[x];
			continue;
		}
		parent(x);
		printf ("%d\n", ans[par[x]]);
	}

}


#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
int par[N];
int ans[N];
bool mark[N];
int n,m,q;
int d[N];
bool gmark[N];
vector <int> v[N];
void clear()
{
	for (int i =0 ; i < N; ++i) d[i] = -1, mark[i] = false;
}
void dfs (int s, int head, int dd)
{
	par[s] = head;
	mark[s] = true;
	gmark[s] = true;
	d[s] = dd;
	for (int i = 0 ; i < v[s].size(); ++i)
		if (!mark[v[s][i]]) dfs(v[s][i], head, dd+1);
}
int parent (int x)
{
	if(x == par[x])
		return x;
	par[x] = parent (par[x]);
	return par[x];
}
int main()
{
	cin >> n >> m >> q;
	int x,y;
	for (int i = 0 ;i < m; ++i)
	{
		cin >> x >> y;
		x--, y--;
		v[x].push_back (y);
		v[y].push_back (x);
	}
	int help, id;
	for (int i = 0 ;i < n; ++i)
		if (!gmark[i])
		{
	//		cout << endl << " I : "<< i <<  "    max d[i] : " ;
			help = i;
			clear();
			dfs (i, i, 0);
			help = -1;
			for (int j = 0 ;j < N; ++j)
				if ((d[j] != -1) and (help < d[j]))
				{
					help = d[j];
					id = j;
				}
		//	cout << help << "  id : " << id << endl;
			clear();
			dfs (id, i, 0);
			help  = -1;
			for (int j = 0 ;j < N; ++j)
				help = max (help, d[j]);
			ans[i] = help;
		//	cout << endl << " I : " << i << "  ans[i] : " << ans[i] << endl;
		}
	for (int i = 0;i < q; ++i)
	{
		cin >> help >> x ;
		x --;
		if (help == 2)
		{

			cin >> y;y--;
			parent (x);
			parent (y);
	//		cout << endl << "  x : " << x << " y : "<< y << "  par[x] : " << par[x] << "   par[y] : "<< par[y] << "old ans[par[y]] : " << ans[par[y]] << "   old ans[par[x]] : " << ans[par[x]] << "  new : " ;
			ans[par[x]] = max (max (ans[par[y]], ans[par[x]]),(((ans[par[y]]+1)/2) + ((ans[par[x]]+1)/2))+1); //cout << ans[par[x]] << endl;
			par[par[y]] = par[x];
			continue;
		}
		parent(x);
	//	cout << endl << " X : " << x << " parent[x] : " << par[x] << "  ans : " ;
		cout << ans[par[x]] << endl;
	}

}


#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
int par[N];
int ans[N];
bool mark[N];
int n,m,q;
int d[N];
bool gmark[N];
vector <int> v[N];
void clear()
{
	for (int i =0 ; i < N; ++i) d[i] = -1, mark[i] = false;
}
void dfs (int s, int head, int dd)
{
	par[s] = head;
	mark[s] = true;
	gmark[s] = true;
	d[s] = dd;
	for (int i = 0 ; i < v[s].size(); ++i)
		if (!mark[v[s][i]]) dfs(v[s][i], head, dd+1);
}
int parent (int x)
{
	if(x == par[x])
		return x;
	par[x] = parent (par[x]);
	return par[x];
}
int main()
{
	cin >> n >> m >> q;
	int x,y;
	for (int i = 0 ;i < m; ++i)
	{
		scanf ("%d%d", &x, &y);
		x--, y--;
		v[x].push_back (y);
		v[y].push_back (x);
	}
	int help, id;
	for (int i = 0 ;i < n; ++i)
		if (!gmark[i])
		{
			help = i;
			clear();
			dfs (i, i, 0);
			help = -1;
			for (int j = 0 ;j < N; ++j)
				if ((d[j] != -1) and (help < d[j]))
				{
					help = d[j];
					id = j;
				}
			clear();
			dfs (id, i, 0);
			help  = -1;
			for (int j = 0 ;j < N; ++j)
				help = max (help, d[j]);
			ans[i] = help;
		}
	for (int i = 0;i < q; ++i)
	{
		scanf ("%d%d", &hlep, &x);
		x --;
		if (help == 2)
		{

			scanf ("%d", &y);y--;
			parent (x);
			parent (y);
			if (par[x] == par[y]) continue;
			ans[par[x]] = max (max (ans[par[y]], ans[par[x]]),(((ans[par[y]]+1)/2) + ((ans[par[x]]+1)/2))+1); //cout << ans[par[x]] << endl;
			par[par[y]] = par[x];
			continue;
		}
		parent(x);
		printf ("%d", ans[par[x]]);
	}

}


#include<iostream>
#include<vector>
using namespace std;
static int set[300001],diameter[300001];
static vector <int>A [300001];

template <typename T> pair<int, int > bfs(int source,  T visited[])
{
//	int node=source,dist=0;
	pair<int,int> node = make_pair(source,0);
//	cout<<"visiting "<<source <<endl;
	int k=0;
	for(int i=0;i<A[source].size();i++)
	{
		if(visited[A[source][i]]!=visited[source])
		{
			visited[A[source][i]]= visited[source];
			pair<int,int> node2= bfs(A[source][i],visited);

			if(node.second<=node2.second)
			{
				node=node2;
			}
			k++;
		}

	}
	if(k!=0)	node.second++;
//	cout <<"from" << source  << " " <<node.first <<"  " <<node.second<<endl;
	return node;
}
//
//pair<int, int > bfs(int source,  bool visited[])
//{
////	int node=source,dist=0;
//	pair<int,int> node = make_pair(source,1);
//	cout<<"visiting "<<source;
//	for(int i=0;i<A[source].size();i++)
//	{
//		if(visited[A[source][i]]!=visited[source])
//		{
//			visited[A[source][i]]= visited[source];
//			pair<int,int> node2= bfs(A[source][i],visited);
//
//			if(node.second>node2.second)
//			{
//				node=node2;
//			}
//			node.second++;
//		}
//
//	}
//	cout <<"  "<<node.second<<endl;
//	return node;
//}
int root(int const arr[], int i)
{
	while(arr[i]!=i)
	{
		i=arr[i];
	}
	return i;
}

int main()
{
	int n,m,q;
//	cin >>n >>m >>q;
	scanf("%d %d %d",&n,&m,&q);
	int x,y,z;

	for(int i=0;i<m;i++)
	{
//		cin >>x >>y;
		scanf("%d %d",&x,&y);
//		x--,y--;
		A[x].push_back(y);
		A[y].push_back(x);
	}

//	cout<<"edges done\n";
//	cout<<"abt to fill\n";
//	fill(set,set+n+1,0);
//	fill(diameter,diameter+n+1,1);
//	cout<<"filled\n";
	for(int i=1;i<=n;i++)
	{
		if(set[i]==0)
		{
//			cout<<"bfsing " <<i<<endl;
			set[i]=i;
			int node=bfs(i,set).first;
//			cout <<"------" <<node <<endl;
			bool visited[n+1];
			fill(visited,visited+n+1,0);
			visited[node]=1;
			diameter[i]=bfs(node,visited).second;
//			cout<<diameter[1]<<endl;
		}
	}
//	cout<<"abt to make points\n";

//	for(int i=1;i<=n;i++)
//	{
////		cout<<i <<" --  " <<root(set,i) <<"  "<<diameter[root(set,i)]<<endl;
//	}
//	int q;
//	cin >>q;
	while(q--)
	{
//		cin >>x;
		scanf("%d",&x);
		if(x==1)
		{
			cin >>y;
//			y--;
//			cout<<diameter[root(set,y)]<<endl;
			printf("%d\n",diameter[root(set,y)]);
		}
		else{
//			cin >>y>>z;
			scanf("%d %d",&y,&z);
//			y--,z--;

			y=root(set,y);
			z=root(set,z);
			if(y!=z){
				if(diameter[y]>diameter[z])
				{
					set[z]=y;
					diameter[y]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
				else{
					set[y]=z;
					diameter[z]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
			}
		}
	}
}


#include<iostream>
#include<vector>
using namespace std;
static int set[300001],diameter[300001];
static vector <int>A [300001];

template <typename T> pair<int, int > bfs(int source,  T visited[])
{
//	int node=source,dist=0;
	pair<int,int> node = make_pair(source,0);
//	cout<<"visiting "<<source <<endl;
	int k=0;
	for(int i=0;i<A[source].size();i++)
	{
		if(visited[A[source][i]]!=visited[source])
		{
			visited[A[source][i]]= visited[source];
			pair<int,int> node2= bfs(A[source][i],visited);

			if(node.second<=node2.second)
			{
				node=node2;
			}
			k++;
		}

	}
	if(k!=0)	node.second++;
//	cout <<"from" << source  << " " <<node.first <<"  " <<node.second<<endl;
	return node;
}
//
//pair<int, int > bfs(int source,  bool visited[])
//{
////	int node=source,dist=0;
//	pair<int,int> node = make_pair(source,1);
//	cout<<"visiting "<<source;
//	for(int i=0;i<A[source].size();i++)
//	{
//		if(visited[A[source][i]]!=visited[source])
//		{
//			visited[A[source][i]]= visited[source];
//			pair<int,int> node2= bfs(A[source][i],visited);
//
//			if(node.second>node2.second)
//			{
//				node=node2;
//			}
//			node.second++;
//		}
//
//	}
//	cout <<"  "<<node.second<<endl;
//	return node;
//}
int root(int const arr[], int i)
{
	while(arr[i]!=i)
	{
		i=arr[i];
	}
	return i;
}

int main()
{
	int n,m,q;
//	cin >>n >>m >>q;
	scanf("%d %d %d",&n,&m,&q);
	int x,y,z;

	for(int i=0;i<m;i++)
	{
//		cin >>x >>y;
		scanf("%d %d",&x,&y);
//		x--,y--;
		A[x].push_back(y);
		A[y].push_back(x);
	}

//	cout<<"edges done\n";
//	cout<<"abt to fill\n";
//	fill(set,set+n+1,0);
//	fill(diameter,diameter+n+1,1);
//	cout<<"filled\n";
	bool visited[n+1];
	for(int i=1;i<=n;i++)
	{
		if(set[i]==0)
		{
//			cout<<"bfsing " <<i<<endl;
			set[i]=i;
			int node=bfs(i,set).first;
//			cout <<"------" <<node <<endl;

			fill(visited,visited+n+1,0);
			visited[node]=1;
			diameter[i]=bfs(node,visited).second;
//			cout<<diameter[1]<<endl;
		}
	}
//	cout<<"abt to make points\n";

//	for(int i=1;i<=n;i++)
//	{
////		cout<<i <<" --  " <<root(set,i) <<"  "<<diameter[root(set,i)]<<endl;
//	}
//	int q;
//	cin >>q;
	while(q--)
	{
//		cin >>x;
		scanf("%d",&x);
		if(x==1)
		{
			cin >>y;
//			y--;
//			cout<<diameter[root(set,y)]<<endl;
			printf("%d\n",diameter[root(set,y)]);
		}
		else{
//			cin >>y>>z;
			scanf("%d %d",&y,&z);
//			y--,z--;

			y=root(set,y);
			z=root(set,z);
			if(y!=z){
				if(diameter[y]>diameter[z])
				{
					set[z]=y;
					diameter[y]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
				else{
					set[y]=z;
					diameter[z]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
			}
		}
	}
}


#include<iostream>
#include<vector>
using namespace std;
static int set[300001],diameter[300001];
static vector <int>A [300001];

template <typename T> pair<int, int > bfs(int source,  T visited[])
{
//	int node=source,dist=0;
	pair<int,int> node = make_pair(source,0);
//	cout<<"visiting "<<source <<endl;
	int k=0;
	for(int i=0;i<A[source].size();i++)
	{
		if(visited[A[source][i]]!=visited[source])
		{
			visited[A[source][i]]= visited[source];
			pair<int,int> node2= bfs(A[source][i],visited);

			if(node.second<=node2.second)
			{
				node=node2;
			}
			k++;
		}

	}
	if(k!=0)	node.second++;
//	cout <<"from" << source  << " " <<node.first <<"  " <<node.second<<endl;
	return node;
}

int bfs(int source,  int old)
{
int node=0,node2;

//	cout<<"visiting "<<source <<endl;
	int k=0;
	for(int i=0;i<A[source].size();i++)
	{
		if(A[source][i]!=old&&A[source][i]!=source)
		{
			int c =bfs(A[source][i],source);
			if(node < c )
			{
//				cout <<node << "node < c - " <<c<<endl ;
				node =c;
			}
//			cout << " value of node " <<node <<" recieved from " <<A[source][i] <<"  "<<c  <<endl;
			k++;
		}

	}
	if(k!=0)	node++;
//	cout <<"from" << source <<"  " <<node<<endl;
	return node;
}

int root( int i)
{
	while(set[i]!=i)
	{
		i=set[i];
	}
	return i;
}

int main()
{
	int n,m,q;
//	cin >>n >>m >>q;
	scanf("%d %d %d",&n,&m,&q);
	int x,y,z;

	for(int i=0;i<m;i++)
	{
//		cin >>x >>y;
		scanf("%d %d",&x,&y);
//		x--,y--;
		A[x].push_back(y);
		A[y].push_back(x);
	}

//	cout<<"edges done\n";
//	cout<<"abt to fill\n";
//	fill(set,set+n+1,0);
//	fill(diameter,diameter+n+1,1);
//	cout<<"filled\n";
	bool visited[n+1];
	for(int i=1;i<=n;i++)
	{
		if(set[i]==0)
		{
//			cout<<"bfsing " <<i<<endl;
			set[i]=i;
			int node=bfs(i,set).first;
//			cout <<"------" <<node <<endl;

//			fill(visited,visited+n+1,0);
//			visited[node]=1;
			diameter[i]=bfs(node,-1);
//			cout<<i <<"  "<<diameter[i]<<endl;
		}
	}
//	cout<<"abt to make points\n";

//	for(int i=1;i<=n;i++)
//	{
////		cout<<i <<" --  " <<root(set,i) <<"  "<<diameter[root(set,i)]<<endl;
//	}
//	int q;
//	cin >>q;
	while(q--)
	{
//		cin >>x;
		scanf("%d",&x);
		if(x==1)
		{
			cin >>y;
//			y--;
//			cout<<diameter[root(set,y)]<<endl;
			printf("%d\n",diameter[root(y)]);
		}
		else{
//			cin >>y>>z;
			scanf("%d %d",&y,&z);
//			y--,z--;

			y=root(y);
			z=root(z);
			if(y!=z){
				if(diameter[y]>diameter[z])
				{
					set[z]=y;
					diameter[y]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
				else{
					set[y]=z;
					diameter[z]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
			}
		}
	}
}


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

pair<int, int > bfs(int source, vector<int> A[], int visited[])
{
//	int node=source,dist=0;
	pair<int,int> node = make_pair(source,0);
	for(int i=0;i<A[source].size();i++)
	{
		if(visited[i]!=visited[source])
		{
			visited[A[source][i]]= visited[source];
			pair<int,int> node2= bfs(A[source][i],A,visited);

			if(node.second>node2.second)
			{
				node=node2;
			}
		}

	}
	return node;
}

pair<int, int > bfs(int source, vector<int> A[],  bool visited[])
{
//	int node=source,dist=0;
	pair<int,int> node = make_pair(source,0);
	for(int i=0;i<A[source].size();i++)
	{
		if(visited[i]!=visited[source])
		{
			visited[A[source][i]]= visited[source];
			pair<int,int> node2= bfs(A[source][i],A,visited);

			if(node.second>node2.second)
			{
				node=node2;
			}
		}

	}
	return node;
}

int normalize(int y, vector<int> point)
{
	if(y!=point[y] )
	{
		point[y]=normalize(point[y],point);
	}
	return point[y];
}
int main()
{
	int n,m,q;
	cin >>n >>m >>q;
	vector<int> A[n+1];

	int x,y,z;

	for(int i=0;i<m;i++)
	{
		cin >>x >>y;
//		x--,y--;
		A[x].push_back(y);
		A[y].push_back(x);
	}

	int set[n+1],diameter[n+1],k=1;
	fill(set,set+n+1,0);
	for(int i=1;i<=n;i++)
	{
		if(set[i]==0)
		{
			set[i]=k;
			int node=bfs(i,A,set).first;
			bool visited[n+1];
			fill(visited,visited+n+1,0);
			visited[i]=1;
			diameter[i]=bfs(node, A,  visited).second;
			k++;
		}
	}
	vector<int> point(k+1);

	for(int i=0;i<=k;i++)
	{
		point[i]=i;
	}

//	int q;
//	cin >>q;
	while(q--)
	{
		cin >>x;
		if(x==1)
		{
			cin >>y;
//			y--;
			cout<<diameter[point[set[y]]]<<endl;
		}
		else{
			cin >>y>>z;
//			y--,z--;

			y=set[y];
//			cout<<y <<" ";
			z=set[z];
//			cout<<z <<" ";
//			cout <<diameter[point[y]] <<"  "<<diameter[point[z]] <<endl;

			if(y!=z){

//			y=point[y];
//			z=point[z];
			diameter[point[y]]= max(diameter[point[y]],max(diameter[point[z]] , ((diameter[point[z]]+1)/2)+ ((diameter[point[y]]+1)/2) +1 ));
//			cout<<diameter[point[y]] << endl;
			int a = point[z];
			for(int i=0;i<=point.size();i++)
			{
				if(point[i]==a)
				{
					point[i]=point[y];
				}
			}

			}
		}
	}
}


#include<iostream>
#include<vector>
using namespace std;
static int set[300001],diameter[300001];
static vector <int>A [300001];

template <typename T> pair<int, int > bfs(int source,  T visited[])
{
//	int node=source,dist=0;
	pair<int,int> node = make_pair(source,0);
//	cout<<"visiting "<<source <<endl;
	int k=0;
	for(int i=0;i<A[source].size();i++)
	{
		if(visited[A[source][i]]!=visited[source])
		{
			visited[A[source][i]]= visited[source];
			pair<int,int> node2= bfs(A[source][i],visited);

			if(node.second<=node2.second)
			{
				node=node2;
			}
			k++;
		}

	}
	if(k!=0)	node.second++;
//	cout <<"from" << source  << " " <<node.first <<"  " <<node.second<<endl;
	return node;
}

int node=0,node2;

int bfs(int source,  int old)
{
	node=0;

//	cout<<"visiting "<<source <<endl;
	int k=0;
	for(int i=0;i<A[source].size();i++)
	{
		if(A[source][i]!=old&&A[source][i]!=source)
		{

			node2= bfs(A[source][i],source);

			if(node<=node2)
			{
				node=node2;
			}
			k++;
		}

	}
	if(k!=0)	node++;
//	cout <<"from" << source  << " " <<node.first <<"  " <<node.second<<endl;
	return node;
}

int root( int i)
{
	while(set[i]!=i)
	{
		i=set[i];
	}
	return i;
}

int main()
{
	int n,m,q;
//	cin >>n >>m >>q;
	scanf("%d %d %d",&n,&m,&q);
	int x,y,z;

	for(int i=0;i<m;i++)
	{
//		cin >>x >>y;
		scanf("%d %d",&x,&y);
//		x--,y--;
		A[x].push_back(y);
		A[y].push_back(x);
	}

//	cout<<"edges done\n";
//	cout<<"abt to fill\n";
//	fill(set,set+n+1,0);
//	fill(diameter,diameter+n+1,1);
//	cout<<"filled\n";
	bool visited[n+1];
	for(int i=1;i<=n;i++)
	{
		if(set[i]==0)
		{
//			cout<<"bfsing " <<i<<endl;
			set[i]=i;
			int node=bfs(i,set).first;
//			cout <<"------" <<node <<endl;

//			fill(visited,visited+n+1,0);
//			visited[node]=1;
			diameter[i]=bfs(node,-1);
//			cout<<i <<"  "<<diameter[i]<<endl;
		}
	}
//	cout<<"abt to make points\n";

//	for(int i=1;i<=n;i++)
//	{
////		cout<<i <<" --  " <<root(set,i) <<"  "<<diameter[root(set,i)]<<endl;
//	}
//	int q;
//	cin >>q;
	while(q--)
	{
//		cin >>x;
		scanf("%d",&x);
		if(x==1)
		{
			cin >>y;
//			y--;
//			cout<<diameter[root(set,y)]<<endl;
			printf("%d\n",diameter[root(y)]);
		}
		else{
//			cin >>y>>z;
			scanf("%d %d",&y,&z);
//			y--,z--;

			y=root(y);
			z=root(z);
			if(y!=z){
				if(diameter[y]>diameter[z])
				{
					set[z]=y;
					diameter[y]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
				else{
					set[y]=z;
					diameter[z]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
			}
		}
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
vector <int> A [300000];
template <typename T> pair<int, int > bfs(int source,  T visited[])
{
//	int node=source,dist=0;
	pair<int,int> node = make_pair(source,0);
//	cout<<"visiting "<<source <<endl;
	int k=0;
	for(int i=0;i<A[source].size();i++)
	{
		if(visited[A[source][i]]!=visited[source])
		{
			visited[A[source][i]]= visited[source];
			pair<int,int> node2= bfs(A[source][i],visited);

			if(node.second<=node2.second)
			{
				node=node2;
			}
			k++;
		}

	}
	if(k!=0)	node.second++;
//	cout <<"from" << source  << " " <<node.first <<"  " <<node.second<<endl;
	return node;
}
//
//pair<int, int > bfs(int source,  bool visited[])
//{
////	int node=source,dist=0;
//	pair<int,int> node = make_pair(source,1);
//	cout<<"visiting "<<source;
//	for(int i=0;i<A[source].size();i++)
//	{
//		if(visited[A[source][i]]!=visited[source])
//		{
//			visited[A[source][i]]= visited[source];
//			pair<int,int> node2= bfs(A[source][i],visited);
//
//			if(node.second>node2.second)
//			{
//				node=node2;
//			}
//			node.second++;
//		}
//
//	}
//	cout <<"  "<<node.second<<endl;
//	return node;
//}
int root(int const arr[], int i)
{
	while(arr[i]!=i)
	{
		i=arr[i];
	}
	return i;
}

int main()
{
	int n,m,q;
	cin >>n >>m >>q;

	int x,y,z;

	for(int i=0;i<m;i++)
	{
		cin >>x >>y;
//		x--,y--;
		A[x].push_back(y);
		A[y].push_back(x);
	}

//	cout<<"edges done\n";
	int set[n+1],diameter[n+1],k=1;
//	cout<<"abt to fill\n";
	fill(set,set+n+1,0);
	fill(diameter,diameter+n+1,1);
//	cout<<"filled\n";
	for(int i=1;i<=n;i++)
	{
		if(set[i]==0)
		{
//			cout<<"bfsing " <<i<<endl;
			set[i]=i;
			int node=bfs(i,set).first;
//			cout <<"------" <<node <<endl;
			bool visited[n+1];
			fill(visited,visited+n+1,0);
			visited[node]=1;
			diameter[i]=bfs(node,visited).second;
//			cout<<diameter[1]<<endl;
			k++;
		}
	}
//	cout<<"abt to make points\n";

	for(int i=1;i<=n;i++)
	{
//		cout<<i <<" --  " <<root(set,i) <<"  "<<diameter[root(set,i)]<<endl;
	}
//	int q;
//	cin >>q;
	while(q--)
	{
		cin >>x;
		if(x==1)
		{
			cin >>y;
//			y--;
			cout<<diameter[root(set,y)]<<endl;
		}
		else{
			cin >>y>>z;
//			y--,z--;

			y=root(set,y);
			z=root(set,z);
			if(y!=z){
				if(diameter[y]>diameter[z])
				{
					set[z]=y;
					diameter[y]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
				else{
					set[y]=z;
					diameter[z]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
			}
		}
	}
}


#include<iostream>
#include<vector>
using namespace std;
static int set[300001],diameter[300001];
static vector <int>A [300001];

template <typename T> pair<int, int > bfs(int source,  T visited[])
{
//	int node=source,dist=0;
	pair<int,int> node = make_pair(source,0);
//	cout<<"visiting "<<source <<endl;
	int k=0;
	for(int i=0;i<A[source].size();i++)
	{
		if(visited[A[source][i]]!=visited[source])
		{
			visited[A[source][i]]= visited[source];
			pair<int,int> node2= bfs(A[source][i],visited);

			if(node.second<=node2.second)
			{
				node=node2;
			}
			k++;
		}

	}
	if(k!=0)	node.second++;
//	cout <<"from" << source  << " " <<node.first <<"  " <<node.second<<endl;
	return node;
}
//
//pair<int, int > bfs(int source,  bool visited[])
//{
////	int node=source,dist=0;
//	pair<int,int> node = make_pair(source,1);
//	cout<<"visiting "<<source;
//	for(int i=0;i<A[source].size();i++)
//	{
//		if(visited[A[source][i]]!=visited[source])
//		{
//			visited[A[source][i]]= visited[source];
//			pair<int,int> node2= bfs(A[source][i],visited);
//
//			if(node.second>node2.second)
//			{
//				node=node2;
//			}
//			node.second++;
//		}
//
//	}
//	cout <<"  "<<node.second<<endl;
//	return node;
//}
int root(int const arr[], int i)
{
	while(arr[i]!=i)
	{
		i=arr[i];
	}
	return i;
}

int main()
{
	int n,m,q;
//	cin >>n >>m >>q;
	scanf("%d %d %d",&n,&m,&q);
	int x,y,z;

	for(int i=0;i<m;i++)
	{
//		cin >>x >>y;
		scanf("%d %d",&x,&y);
//		x--,y--;
		A[x].push_back(y);
		A[y].push_back(x);
	}

//	cout<<"edges done\n";
//	cout<<"abt to fill\n";
//	fill(set,set+n+1,0);
	fill(diameter,diameter+n+1,1);
//	cout<<"filled\n";
	for(int i=1;i<=n;i++)
	{
		if(set[i]==0)
		{
//			cout<<"bfsing " <<i<<endl;
			set[i]=i;
			int node=bfs(i,set).first;
//			cout <<"------" <<node <<endl;
			bool visited[n+1];
			fill(visited,visited+n+1,0);
			visited[node]=1;
			diameter[i]=bfs(node,visited).second;
//			cout<<diameter[1]<<endl;
		}
	}
//	cout<<"abt to make points\n";

//	for(int i=1;i<=n;i++)
//	{
////		cout<<i <<" --  " <<root(set,i) <<"  "<<diameter[root(set,i)]<<endl;
//	}
//	int q;
//	cin >>q;
	while(q--)
	{
//		cin >>x;
		scanf("%d",&x);
		if(x==1)
		{
			cin >>y;
//			y--;
//			cout<<diameter[root(set,y)]<<endl;
			printf("%d\n",diameter[root(set,y)]);
		}
		else{
//			cin >>y>>z;
			scanf("%d %d",&y,&z);
//			y--,z--;

			y=root(set,y);
			z=root(set,z);
			if(y!=z){
				if(diameter[y]>diameter[z])
				{
					set[z]=y;
					diameter[y]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
				else{
					set[y]=z;
					diameter[z]=max(diameter[y],max(diameter[z], (diameter[z]+1)/2 + (diameter[y]+1)/2 + 1));
				}
			}
		}
	}
}


//bayemirov
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back
#define f first
#define s second

const int N = 4e5;
const int INF = 1e9;

vector<int> adj[N], comp[N];
int id[N], d[N], diam[N], cnt = 1, p[N], sz[N];
bool was[N];

void dfs(int v) {
	was[v] = 1;
	id[v] = cnt;
	comp[cnt].pb(v);
	for (auto to : adj[v])
		if (!was[to])
			dfs(to);
}

int furthest(int num) {
	pair<int, int> res = {-1, 0};
	for (auto v : comp[num])
		if (d[v] > res.f)
			res = {d[v], v};
	return res.s;
}

int findDiameter(int s, int order, int num) {
	for (auto v : comp[num]) d[v] = INF;
	queue<int> q;
	q.push(s);
	d[s] = 0;
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (auto to : adj[v]) {
			if (d[to] > d[v] + 1) {
				d[to] = d[v] + 1;
				q.push(to);
			}
		}
	}
	return (order == 1 ? findDiameter(furthest(num), 2, num) : d[furthest(num)]);
}

int get_root(int v) {
	return (p[v] == v ? v : p[v] = get_root(p[v]));
}

void unite(int u, int v) {
	u = get_root(u);
	v = get_root(v);
	if (u == v) return;
	if (sz[u] < sz[v]) swap(u, v);
	p[v] = u;
	sz[u] += sz[v];
	diam[u] = max(max(diam[u], diam[v]), (diam[u] + 1) / 2 + (diam[v] + 1) / 2 + 1);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, m, q;
	cin >> n >> m >> q;
	while (m--) {
		int u, v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for (int i = 1; i <= n; i++) {
		if (!was[i]) {
			dfs(i);
			diam[cnt] = findDiameter(i, 1, cnt);
			cnt++;
		}
	}
	for (int i = 1; i < cnt; i++) {
		sz[i] = 1;
		p[i] = i;
	}
	while (q--) {
		int t, u;
		cin >> t >> u;
		if (t == 1)
			cout << diam[get_root(id[u])] << "\n";
	  	else {
	  		int v;
	  		cin >> v;
	  		unite(id[u], id[v]);
	  	}
	}
	return 0;
}


//bayemirov
#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;

#define pb push_back
#define f first
#define s second

const int N = 4e5;
const int INF = 1e9;

vector<int> adj[N], comp[N];
int id[N], d[N], diam[N], cnt = 1, p[N], sz[N];
bool was[N];

void dfs(int v) {
	was[v] = 1;
	id[v] = cnt;
	comp[cnt].pb(v);
	for (auto to : adj[v])
		if (!was[to])
			dfs(to);
}

int furthest(int num) {
	pair<int, int> res = {-1, 0};
	for (auto v : comp[num])
		if (d[v] > res.f)
			res = {d[v], v};
	return res.s;
}

int findDiameter(int s, int order, int num) {
	queue<int> q;
	q.push(s);
	d[s] = 0;
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (auto to : adj[v]) {
			if (d[to] > d[v] + 1) {
				d[to] = d[v] + 1;
				q.push(to);
			}
		}
	}
	return (order == 1 ? findDiameter(furthest(num), 2, num) : d[furthest(num)]);
}

int get_root(int v) {
	return (p[v] == v ? v : p[v] = get_root(p[v]));
}

void unite(int u, int v) {
	u = get_root(u);
	v = get_root(v);
	if (sz[u] < sz[v]) swap(u, v);
	p[v] = u;
	sz[u] += sz[v];
	diam[u] = max(max(diam[u], diam[v]), (diam[u] + 1) / 2 + (diam[v] + 1) / 2 + 1);
}

int main() {
	fill(d, d + N, INF);
	int n, m, q;
	cin >> n >> m >> q;
	while (m--) {
		int u, v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for (int i = 1; i <= n; i++) {
		if (!was[i]) {
			dfs(i);
			diam[cnt] = findDiameter(i, 1, cnt);
			cnt++;
		}
	}
	for (int i = 1; i <= cnt; i++) {
		sz[i] = 1;
		p[i] = i;
	}
	while (q--) {
		int t, u;
		cin >> t >> u;
		if (t == 1)
			cout << diam[get_root(id[u])] << "\n";
	  	else {
	  		int v;
	  		cin >> v;
	  		unite(id[u], id[v]);
	  	}
	}
	return 0;
}


//bayemirov
#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;

#define pb push_back
#define f first
#define s second

const int N = 4e5;
const int INF = 1e9;

vector<int> adj[N], comp[N];
int id[N], d[N], diam[N], cnt = 1, p[N], sz[N];
bool was[N];

void dfs(int v) {
	was[v] = 1;
	id[v] = cnt;
	comp[cnt].pb(v);
	for (auto to : adj[v])
		if (!was[to])
			dfs(to);
}

int furthest(int num) {
	pair<int, int> res = {0, 0};
	for (auto v : comp[num])
		if (d[v] > res.f)
			res = {d[v], v};
	return res.s;
};

int findDiameter(int s, int order, int num) {
	queue<int> q;
	q.push(s);
	d[s] = 0;
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (auto to : adj[v]) {
			if (d[to] > d[v] + 1) {
				d[to] = d[v] + 1;
				q.push(to);
			}
		}
	}
	return (order == 1 ? findDiameter(furthest(num), 2, num) : d[furthest(num)]);
}

int get_root(int v) {
	return (p[v] == v ? v : p[v] = get_root(p[v]));
}

void unite(int u, int v) {
	u = get_root(u);
	v = get_root(v);
	if (sz[u] < sz[v]) swap(u, v);
	p[v] = u;
	sz[u] += sz[v];
	diam[u] = max(max(diam[u], diam[v]), (diam[u] + 1) / 2 + (diam[v] + 1) / 2 + 1);
}

int main() {
	fill(d, d + N, INF);
	int n, m, q;
	cin >> n >> m >> q;
	while (m--) {
		int u, v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for (int i = 1; i <= n; i++) {
		if (!was[i]) {
			dfs(i);
			diam[cnt] = findDiameter(i, 1, cnt);
			cnt++;
		}
	}
	for (int i = 1; i <= cnt; i++) {
		sz[i] = 1;
		p[i] = i;
	}
	while (q--) {
		int t, u;
		cin >> t >> u;
		if (t == 1)
			cout << diam[get_root(id[u])] << "\n";
	  	else {
	  		int v;
	  		cin >> v;
	  		unite(id[u], id[v]);
	  	}
	}
	return 0;
}


//bayemirov
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back
#define f first
#define s second

const int N = 4e5;
const int INF = 1e9;

vector<int> adj[N], comp[N];
int id[N], d[N], diam[N], cnt = 1, p[N], sz[N];
bool was[N];

void dfs(int v) {
	was[v] = 1;
	id[v] = cnt;
	comp[cnt].pb(v);
	for (auto to : adj[v])
		if (!was[to])
			dfs(to);
}

int furthest(int num) {
	pair<int, int> res = {-1, 0};
	for (auto v : comp[num])
		if (d[v] > res.f)
			res = {d[v], v};
	return res.s;
}

int findDiameter(int s, int order, int num) {
	for (auto v : comp[num]) d[v] = INF;
	queue<int> q;
	q.push(s);
	d[s] = 0;
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (auto to : adj[v]) {
			if (d[to] > d[v] + 1) {
				d[to] = d[v] + 1;
				q.push(to);
			}
		}
	}
	return (order == 1 ? findDiameter(furthest(num), 2, num) : d[furthest(num)]);
}

int get_root(int v) {
	return (p[v] == v ? v : p[v] = get_root(p[v]));
}

void unite(int u, int v) {
	u = get_root(u);
	v = get_root(v);
	if (u == v) return;
	if (sz[u] < sz[v]) swap(u, v);
	p[v] = u;
	sz[u] += sz[v];
	diam[u] = max(max(diam[u], diam[v]), (diam[u] + 1) / 2 + (diam[v] + 1) / 2 + 1);
}

int main() {
	ios_base::sync_with_stdio(0);
	//cin.tie(0); cout.tie(0);
	int n, m, q;
	cin >> n >> m >> q;
	while (m--) {
		int u, v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for (int i = 1; i <= n; i++) {
		if (!was[i]) {
			dfs(i);
			diam[cnt] = findDiameter(i, 1, cnt);
			cnt++;
		}
	}
	for (int i = 1; i < cnt; i++) {
		sz[i] = 1;
		p[i] = i;
	}
	while (q--) {
		int t, u;
		cin >> t >> u;
		if (t == 1)
			cout << diam[get_root(id[u])] << "\n";
	  	else {
	  		int v;
	  		cin >> v;
	  		unite(id[u], id[v]);
	  	}
	}
	return 0;
}


//bayemirov
#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;

#define pb push_back
#define f first
#define s second

const int N = 4e5;
const int INF = 1e9;

vector<int> adj[N], comp[N];
int id[N], d[N], diam[N], cnt = 1, p[N], sz[N];
bool was[N];

void dfs(int v) {
	was[v] = 1;
	id[v] = cnt;
	comp[cnt].pb(v);
	for (auto to : adj[v])
		if (!was[to])
			dfs(to);
}

int furthest(int num) {
	pair<int, int> res = {-1, 0};
	for (auto v : comp[num])
		if (d[v] > res.f)
			res = {d[v], v};
	return res.s;
}

int findDiameter(int s, int order, int num) {
	for (auto v : comp[num]) d[v] = INF;
	queue<int> q;
	q.push(s);
	d[s] = 0;
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (auto to : adj[v]) {
			if (d[to] > d[v] + 1) {
				d[to] = d[v] + 1;
				q.push(to);
			}
		}
	}
	return (order == 1 ? findDiameter(furthest(num), 2, num) : d[furthest(num)]);
}

int get_root(int v) {
	return (p[v] == v ? v : p[v] = get_root(p[v]));
}

void unite(int u, int v) {
	u = get_root(u);
	v = get_root(v);
	if (u == v) return;
	if (sz[u] < sz[v]) swap(u, v);
	p[v] = u;
	sz[u] += sz[v];
	diam[u] = max(max(diam[u], diam[v]), (diam[u] + 1) / 2 + (diam[v] + 1) / 2 + 1);
}

int main() {
	int n, m, q;
	cin >> n >> m >> q;
	while (m--) {
		int u, v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for (int i = 1; i <= n; i++) {
		if (!was[i]) {
			dfs(i);
			diam[cnt] = findDiameter(i, 1, cnt);
			cnt++;
		}
	}
	for (int i = 1; i < cnt; i++) {
		sz[i] = 1;
		p[i] = i;
	}
	while (q--) {
		int t, u;
		cin >> t >> u;
		if (t == 1)
			cout << diam[get_root(id[u])] << "\n";
	  	else {
	  		int v;
	  		cin >> v;
	  		unite(id[u], id[v]);
	  	}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

int n,m,T,minL;
vector<char> MP[1012345];
int maxdep[2][1012345];

void Find()
{
    minL=max(n,m);
    for(int x=1;x<=n;x++)
    for(int y=1,las;y<=m+1;y++)
    {
        if(MP[x][y]=='X'&&MP[x][y-1]=='.')las=y;
        if(MP[x][y]=='.'&&MP[x][y-1]=='X')minL=min(minL,y-las);
    }
    for(int y=1;y<=m;y++)
    for(int x=1,las;x<=n+1;x++)
    {
        if(MP[x][y]=='X'&&MP[x-1][y]=='.')las=x;
        if(MP[x][y]=='.'&&MP[x-1][y]=='X')minL=min(minL,x-las);
    }
    T=(minL-1)>>1;
    minL=T<<1|1;
}

int mini(int a,int b,int c)
{
    if(a<=b&&a<=c)return a;
    else if(b<=a&&b<=c)return b;
    return c;
}

void Time_Back()
{
    for(int x=1,k=x&1;x<=n;x++,k=x&1)
    for(int y=1;y<=m;y++)
    {
        if(MP[x][y]=='X')maxdep[k][y]=mini(maxdep[k^1][y],maxdep[k][y-1],maxdep[k^1][y-1])+1;
        else maxdep[k][y]=0;
        //printf("%d %d %d\n",x,y,maxdep[k][y]);
        if(maxdep[k][y]>=minL)MP[x-T][y-T]='T';
    }
}

int main()
{
    //freopen("In.txt","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    scanf("%d%d\n",&n,&m);
    for(int i=0;i<=m+1;i++)MP[0].push_back('.');
    for(int x=1;x<=n;x++)
    {
        MP[x].push_back('.');
        for(int y=1;y<=m;y++)MP[x].push_back(getchar());
        MP[x].push_back('.');
        getchar();
    }
    for(int i=0;i<=m+1;i++)MP[n+1].push_back('.');
    Find();
    Time_Back();
    cout<<T<<endl;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            putchar(MP[i][j]=='T'?'X':'.');
        printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N=1e6+5;

int n,m,cnt[N];
string MP[N];
vector<int>DP[N];

int mini(int a,int b,int c)
{
    if(a<=b&&a<=c)return a;
    if(b<=a&&b<=c)return b;
    return c;
}

bool OK(int t)
{
    fill(cnt,cnt+m,0);
    int k=t<<1|1;
    for(int i=n;i;i--)
    {
        for(int j=0;j<m;j++)if(DP[i][j]>=k)cnt[j]++;
        int sm=0;
        for(int j=m-1;j>=0;j--)
        {
            sm+=cnt[j];
            if(j+k<m)sm-=cnt[j+k];
            if(!sm&&MP[i][j]=='X')return 0;
        }
    }
    return 1;
}

int main()
{
    //freopen("In.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        cin>>MP[i];
        DP[i].resize(m+1);
    }
    DP[0].resize(m+1);
    for(int i=1;i<=n;i++)
    for(int j=0;j<m;j++)
        if(MP[i][j]=='X')DP[i][j]=mini(i==0?0:DP[i-1][j],j==0?0:DP[i][j-1],(i==0||j==0)?0:DP[i-1][j-1])+1;
        else DP[i][j]=0;
    int L=0,R=(min(n,m)-1)>>1;
    while(L<R)
    {
        int mid=(L+R+1)>>1;
        if(OK(mid))L=mid;
        else R=mid-1;
    }
    int T=L,minL=T<<1|1;
    for(int i=1;i<=n;i++)
    for(int j=0;j<m;j++)
        if(DP[i][j]>=minL)MP[i-T][j-T]='T';
    printf("%d\n",T);
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<m;j++)
            if(MP[i][j]=='T')putchar('X');
            else putchar('.');
        putchar('\n');
    }
}


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

const int N = 2e6;

string s[N];
vector<int> sum[N],vis[N];
int n,m;
vector<pair<int,pair<int,int> > > v;

void update(int t,int i,int j){
    vis[i-t][j-t]++; vis[i+t+1][j+t+1]++;
    vis[i+t+1][j-t]--; vis[i-t][j+t+1]--;
}

int check(int t)
{
    for (int i = 1;i<=n;i++)
        fill(vis[i].begin(),vis[i].end(),0);
    for (auto p: v) if (p.first>=t)
        update(p.first,p.second.first,p.second.second);
    for (int j = 1;j<=m;j++){
        for (int i = 1;i<=n;i++)
            vis[i][j] += vis[i-1][j];
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++)
            vis[i][j] += vis[i][j-1];
    }
    for (auto p: v) if (p.first<t)
        if (!vis[p.second.first][p.second.second])
            return 0;
    return 1;
}

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

    cin>>n>>m;
    for (int i = 1;i<=n;i++){
        string t; cin>>t;
        s[i] = " "+t;
    }
    for (int i = 0;i<n+5;i++){
        sum[i].resize(m+5);
        vis[i].resize(m+5);
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(s[i][j]=='X');
        }
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++)if (s[i][j]=='X'){
            int l = 0, r = min(min(i-1,j-1),min(n-i,m-j));
            int t = 0;
            while (l<=r){
                int mid = l+r>>1;
                int x = sum[i+mid][j+mid]-sum[i+mid][j-mid-1]-sum[i-mid-1][j+mid]+sum[i-mid-1][j-mid-1];
                if (x==(2*mid+1)*(2*mid+1)) t = mid,l = mid+1;
                else r = mid-1;
            }
            v.emplace_back(make_pair(t,make_pair(i,j)));
        }
    }
    sort(v.begin(), v.end());
    int l = 0,r = min((n-1)/2,(m-1)/2);
    int t = 0;
    while (l<=r){
        int mid = l+r>>1;
        if (check(mid)) t = mid, l = mid+1;
        else r = mid-1;
    }
    cout<<t<<'\n';
    for (int i = 1;i<=n;i++){
        s[i].resize(m);
        fill(s[i].begin(), s[i].end(),'.');
    }
    for (auto p: v)if (p.first>=t)
        s[p.second.first][p.second.second-1] = 'X';
    for (int i = 1;i<=n;i++)
        cout<<s[i]<<"\n";
    return 0;
}


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

const int N = 2e6;

string s[N];
vector<int> sum[N];
int n,m;
vector<pair<int,pair<int,int> > > v;
vector<pair<int,int>> vec[N];

int query(int t,int i,int j){
    if (i+t>n || i-t<1 || j+t>m || j-t<1) return 1421;
    return sum[i+t][j+t] - sum[i-t-1][j+t]-sum[i+t][j-t-1]+sum[i-t-1][j-t-1];
}
void update(int t,int i,int j){

}

int check(int t)
{
    vector<vector<int> > vis(n+2,vector<int>(m+2,0));
    for (int i =1;i<=n;i++){
        for (int j= 1;j<=m;j++)if (s[i][j]=='X'){
            if (query(t,i,j)==0){
                vis[i-t][j-t]++; vis[i+t+1][j+t+1]++;
                vis[i+t+1][j-t]--; vis[i-t][j+t+1]--;
            }
        }
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            vis[i][j] += vis[i-1][j] + vis[i][j-1] - vis[i-1][j-1];
            if (s[i][j]=='X' && !vis[i][j])
                return 0;
        }
    }
    return 1;
}

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

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

    int l = 0,r = min((n-1)/2,(m-1)/2);
    int t = 0;
    while (l<=r){
        int mid = l+r>>1;
        if (check(mid)) t = mid, l = mid+1;
        else r = mid-1;
    }
    cout<<t<<endl;
    for (int i = 1;i<=n;i++){
        s[i].resize(m);
        fill(s[i].begin(), s[i].end(),'.');
    }
    for (int i = 1;i<=n;i++)
        for (int j = 1;j<=m;j++)
            if (query(t,i,j)==0)
                s[i][j-1]='X';
    for (int i = 1;i<=n;i++)
        cout<<s[i]<<endl;
    return 0;
}


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

const int N = 2e6;

string s[N];
vector<int> sum[N],vis[N];
int n,m;
vector<pair<int,pair<int,int> > > v;
vector<pair<int,int>> vec[N];

void update(int t,int i,int j){
    vis[i-t][j-t]++; vis[i+t+1][j+t+1]++;
    vis[i+t+1][j-t]--; vis[i-t][j+t+1]--;
}

int check(int t)
{
    for (int i = 1;i<=n;i++)
        fill(vis[i].begin(),vis[i].end(),0);
    for (auto p: v) if (p.first>=t)
        update(p.first,p.second.first,p.second.second);
    for (int j = 1;j<=m;j++){
        for (int i = 1;i<=n;i++)
            vis[i][j] += vis[i-1][j];
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++)
            vis[i][j] += vis[i][j-1];
    }
    for (auto p: v) if (p.first<t)
        if (!vis[p.second.first][p.second.second])
            return 0;
    return 1;
}

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

    cin>>n>>m;
    for (int i = 1;i<=n;i++){
        string t; cin>>t;
        s[i] = " "+t;
    }
    for (int i = 0;i<n+2;i++){
        sum[i].resize(m+2);
        vis[i].resize(m+2);
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(s[i][j]=='X');
        }
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++)if (s[i][j]=='X'){
            int l = 0, r = min(min(i-1,j-1),min(n-i,m-j));
            int t = 0;
            while (l<=r){
                int mid = l+r>>1;
                int x = sum[i+mid][j+mid]-sum[i+mid][j-mid-1]-sum[i-mid-1][j+mid]+sum[i-mid-1][j-mid-1];
                if (x==(2*mid+1)*(2*mid+1)) t = mid,l = mid+1;
                else r = mid-1;
            }
            vec[t].emplace_back(i,j);
            //v.emplace_back(make_pair(t,make_pair(i,j)));
        }
    }
    //sort(v.begin(), v.end());
    for (int i = min(n,m);i>=0;i--)
        for (auto x: vec[i]) v.emplace_back(i,x);
    int l = 0,r = min((n-1)/2,(m-1)/2);
    int t = 0;
    while (l<=r){
        int mid = l+r>>1;
        if (check(mid)) t = mid, l = mid+1;
        else r = mid-1;
    }
    cout<<t<<endl;
    for (int i = 1;i<=n;i++){
        s[i].resize(m);
        fill(s[i].begin(), s[i].end(),'.');
    }
    for (auto p: v)if (p.first>=t)
        s[p.second.first][p.second.second-1] = 'X';
    for (int i = 1;i<=n;i++)
        cout<<s[i]<<endl;
    return 0;
}


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

const int N = 2e6;

string s[N];
vector<int> sum[N];
int n,m;
vector<pair<int,pair<int,int> > > v;
vector<pair<int,int>> vec[N];

int query(int t,int i,int j){
    if (i+t>n || i-t<1 || j+t>m || j-t<1) return 1421;
    return sum[i+t][j+t] - sum[i-t-1][j+t]-sum[i+t][j-t-1]+sum[i-t-1][j-t-1];
}
void update(int t,int i,int j){

}

int check(int t)
{
    vector<vector<int> > vis(n+2,vector<int>(m+2,0));
    for (int i =1;i<=n;i++){
        for (int j= 1;j<=m;j++)if (s[i][j]=='X'){
            if (query(t,i,j)==0){
                vis[i-t][j-t]++; vis[i+t+1][j+t+1]++;
                vis[i+t+1][j-t]--; vis[i-t][j+t+1]--;
            }
        }
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            vis[i][j] += vis[i-1][j] + vis[i][j-1] - vis[i-1][j-1];
            if (s[i][j]=='X' && !vis[i][j])
                return 0;
        }
    }
    return 1;
}

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

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

    int l = 0,r = min((n-1)/2,(m-1)/2);
    int t = 0;
    while (l<=r){
        int mid = l+r>>1;
        if (check(mid)) t = mid, l = mid+1;
        else r = mid-1;
    }
    cout<<t<<"\n";
    for (int i = 1;i<=n;i++){
        s[i].resize(m);
        fill(s[i].begin(), s[i].end(),'.');
    }
    for (int i = 1;i<=n;i++)
        for (int j = 1;j<=m;j++)
            if (query(t,i,j)==0)
                s[i][j-1]='X';
    for (int i = 1;i<=n;i++)
        cout<<s[i]<<"\n";
    return 0;
}


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

const int N = 2e6;

string s[N];
vector<int> sum[N],vis[N];
int n,m;
vector<pair<int,pair<int,int> > > v;
vector<pair<int,int>> vec[N];

int query(int t,int i,int j){
    if (i+t>n || i-t<1 || j+t>m || j-t<1) return 1421;
    return sum[i+t][j+t] - sum[i-t-1][j+t]-sum[i+t][j-t-1]+sum[i-t-1][j-t-1];
}
void update(int t,int i,int j){
    vis[i-t][j-t]++; vis[i+t+1][j+t+1]++;
    vis[i+t+1][j-t]--; vis[i-t][j+t+1]--;
}

int check(int t)
{
    for (int i = 1;i<=n;i++)
        fill(vis[i].begin(),vis[i].end(),0);
    for (int i =1;i<=n;i++){
        for (int j= 1;j<=m;j++)if (s[i][j]=='X'){
            if (query(t,i,j)==0) update(t,i,j);
        }
    }
    for (int j = 1;j<=m;j++){
        for (int i = 1;i<=n;i++)
            vis[i][j] += vis[i-1][j];
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++)
            vis[i][j] += vis[i][j-1];
    }
    for (int i = 1;i<=n;i++)
        for (int j = 1;j<=m;j++) if (s[i][j]=='X' && !vis[i][j])
            return 0;
    return 1;
}

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

    cin>>n>>m;
    for (int i = 1;i<=n;i++){
        string t; cin>>t;
        s[i] = " "+t;
    }
    for (int i = 0;i<n+2;i++){
        sum[i].resize(m+2);
        vis[i].resize(m+2);
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(s[i][j]=='.');
        }
    }

    int l = 0,r = min((n-1)/2,(m-1)/2);
    int t = 0;
    while (l<=r){
        int mid = l+r>>1;
        if (check(mid)) t = mid, l = mid+1;
        else r = mid-1;
    }
    cout<<t<<endl;
    for (int i = 1;i<=n;i++){
        s[i].resize(m);
        fill(s[i].begin(), s[i].end(),'.');
    }
    for (int i = 1;i<=n;i++)
        for (int j = 1;j<=m;j++)
            if (query(t,i,j)==0)
                s[i][j-1]='X';
    for (int i = 1;i<=n;i++)
        cout<<s[i]<<endl;
    return 0;
}


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

const int N = 2e6;

string s[N];
vector<int> sum[N],vis[N];
int n,m;
vector<pair<int,pair<int,int> > > v;

void update(int t,int i,int j){
    vis[i-t][j-t]++; vis[i+t+1][j+t+1]++;
    vis[i+t+1][j-t]--; vis[i-t][j+t+1]--;
}

int check(int t)
{
    for (int i = 1;i<=n;i++)
        fill(vis[i].begin(),vis[i].end(),0);
    for (auto p: v) if (p.first>=t)
        update(p.first,p.second.first,p.second.second);
    for (int j = 1;j<=m;j++){
        for (int i = 1;i<=n;i++)
            vis[i][j] += vis[i-1][j];
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++)
            vis[i][j] += vis[i][j-1];
    }
    for (auto p: v) if (p.first<t)
        if (!vis[p.second.first][p.second.second])
            return 0;
    return 1;
}

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

    cin>>n>>m;
    for (int i = 1;i<=n;i++){
        string t; cin>>t;
        s[i] = " "+t;
    }
    for (int i = 0;i<n+5;i++){
        sum[i].resize(m+5);
        vis[i].resize(m+5);
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(s[i][j]=='X');
        }
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++)if (s[i][j]=='X'){
            int l = 0, r = min(min(i-1,j-1),min(n-i,m-j));
            int t = 0;
            while (l<=r){
                int mid = l+r>>1;
                int x = sum[i+mid][j+mid]-sum[i+mid][j-mid-1]-sum[i-mid-1][j+mid]+sum[i-mid-1][j-mid-1];
                if (x==(2*mid+1)*(2*mid+1)) t = mid,l = mid+1;
                else r = mid-1;
            }
            v.emplace_back(make_pair(t,make_pair(i,j)));
        }
    }
    sort(v.begin(), v.end());
    int l = 0,r = min((n-1)/2,(m-1)/2);
    int t = 0;
    while (l<=r){
        int mid = l+r>>1;
        if (check(mid)) t = mid, l = mid+1;
        else r = mid-1;
    }
    cout<<t<<endl;
    for (int i = 1;i<=n;i++){
        s[i].resize(m);
        fill(s[i].begin(), s[i].end(),'.');
    }
    for (auto p: v)if (p.first>=t)
        s[p.second.first][p.second.second-1] = 'X';
    for (int i = 1;i<=n;i++)
        cout<<s[i]<<endl;
    return 0;
}


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

const int N = 2e6;

string s[N];
vector<int> sum[N],vis[N];
int n,m;
vector<pair<int,pair<int,int> > > v;

void update(int t,int i,int j){
    vis[i-t][j-t]++; vis[i+t+1][j+t+1]++;
    vis[i+t+1][j-t]--; vis[i-t][j+t+1]--;
}

int check(int t)
{
    for (int i = 1;i<=n;i++)
        fill(vis[i].begin(),vis[i].end(),0);
    for (auto p: v) if (p.first>=t)
        update(p.first,p.second.first,p.second.second);
    for (int j = 1;j<=m;j++){
        for (int i = 1;i<=n;i++)
            vis[i][j] += vis[i-1][j];
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++)
            vis[i][j] += vis[i][j-1];
    }
    for (auto p: v) if (p.first<t)
        if (!vis[p.second.first][p.second.second])
            return 0;
    return 1;
}

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

    cin>>n>>m;
    for (int i = 1;i<=n;i++){
        string t; cin>>t;
        s[i] = " "+t;
    }
    for (int i = 0;i<n+2;i++){
        sum[i].resize(m+2);
        vis[i].resize(m+2);
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(s[i][j]=='X');
        }
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++)if (s[i][j]=='X'){
            int l = 0, r = min(min(i-1,j-1),min(n-i,m-j));
            int t = 0;
            while (l<=r){
                int mid = l+r>>1;
                int x = sum[i+mid][j+mid]-sum[i+mid][j-mid-1]-sum[i-mid-1][j+mid]+sum[i-mid-1][j-mid-1];
                if (x==(2*mid+1)*(2*mid+1)) t = mid,l = mid+1;
                else r = mid-1;
            }
            v.emplace_back(make_pair(t,make_pair(i,j)));
        }
    }
    sort(v.begin(), v.end());
    int l = 0,r = min((n-1)/2,(m-1)/2);
    int t = 0;
    while (l<=r){
        int mid = l+r>>1;
        if (check(mid)) t = mid, l = mid+1;
        else r = mid-1;
    }
    cout<<t<<endl;
    for (int i = 1;i<=n;i++){
        s[i].resize(m);
        fill(s[i].begin(), s[i].end(),'.');
    }
    for (auto p: v)if (p.first>=t)
        s[p.second.first][p.second.second-1] = 'X';
    for (int i = 1;i<=n;i++)
        cout<<s[i]<<endl;
    return 0;
}


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

const int N = 2e6;

string s[N];
vector<int> sum[N],vis[N];
int n,m;
vector<pair<int,pair<int,int> > > v;
vector<pair<int,int>> vec[N];

int query(int t,int i,int j){
    if (i+t>n || i-t<1 || j+t>m || j-t<1) return 1421;
    return sum[i+t][j+t] - sum[i-t-1][j+t]-sum[i+t][j-t-1]+sum[i-t-1][j-t-1];
}
void update(int t,int i,int j){
    vis[i-t][j-t]++; vis[i+t+1][j+t+1]++;
    vis[i+t+1][j-t]--; vis[i-t][j+t+1]--;
}

int check(int t)
{
    for (int i = 1;i<=n;i++)
        fill(vis[i].begin(),vis[i].end(),0);
    for (int i =1;i<=n;i++){
        for (int j= 1;j<=m;j++)if (s[i][j]=='X'){
            if (query(t,i,j)==0) update(t,i,j);
        }
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            vis[i][j] += vis[i-1][j] + vis[i][j-1] - vis[i-1][j-1];
            if (s[i][j]=='X' && !vis[i][j])
                return 0;
        }
    }
    return 1;
}

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

    cin>>n>>m;
    for (int i = 1;i<=n;i++){
        string t; cin>>t;
        s[i] = " "+t;
    }
    for (int i = 0;i<n+2;i++){
        sum[i].resize(m+2);
        vis[i].resize(m+2);
    }
    for (int i = 1;i<=n;i++){
        for (int j = 1;j<=m;j++){
            sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(s[i][j]=='.');
        }
    }

    int l = 0,r = min((n-1)/2,(m-1)/2);
    int t = 0;
    while (l<=r){
        int mid = l+r>>1;
        if (check(mid)) t = mid, l = mid+1;
        else r = mid-1;
    }
    cout<<t<<endl;
    for (int i = 1;i<=n;i++){
        s[i].resize(m);
        fill(s[i].begin(), s[i].end(),'.');
    }
    for (int i = 1;i<=n;i++)
        for (int j = 1;j<=m;j++)
            if (query(t,i,j)==0)
                s[i][j-1]='X';
    for (int i = 1;i<=n;i++)
        cout<<s[i]<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m;
vector<vector<int>> a, pre;

bool check(int l, bool pr){
	vector<vector<int>> b(n+1,vector<int>(m+1,0));
	for(int i=l+1;i+l<=n;i++){
		for(int j=l+1;j+l<=m;j++){
			int box=pre[i+l][j+l]-pre[i-l-1][j+l]-pre[i+l][j-l-1]+pre[i-l-1][j-l-1];
			if(box==1ll*(2*l+1)*(2*l+1)){
				b[i][j]=1;
			}
		}
	}

	auto pren=b;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			pren[i][j]+=pren[i-1][j]+pren[i][j-1]-pren[i-1][j-1];
		}
	}

	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			int rx=min(n,i+l), ry=min(m,j+l);
			int lx=max(1,i-l), ly=max(1,j-l);

			int box=pren[rx][ry]-pren[lx-1][ry]-pren[rx][ly-1]+pren[lx-1][ly-1];
			if(a[i][j] && (!box)){
				return 0;
			}
			if((!a[i][j]) && (box)){
				return 0;
			}
		}
	}

	if(pr){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(b[i][j]){
					cout << 'X';
				}
				else{
					cout << '.';
				}
			}
			cout << endl;
		}
	}
	return 1;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	a.resize(n+1,vector<int>(m+1,0));

	for(int i=1;i<=n;i++){
		string s; cin >> s;
		for(int j=1;j<=m;j++){
			a[i][j]=(s[j-1]=='X');
		}
	}
	pre=a;

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

	int l=0,r=1e6+5;
	while(l+1<r){
		int mid=(l+r)>>1;
		if(check(mid,0)){
			l=mid;
		}
		else{
			r=mid;
		}
	}
	cout << l << endl;
	check(l,1);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m;
vector<vector<int>> a, pre;

bool check(int l, bool pr){
	vector<vector<int>> b(n+1,vector<int>(m+1,0));
	for(int i=l+1;i+l<=n;i++){
		for(int j=l+1;j+l<=m;j++){
			int box=pre[i+l][j+l]-pre[i-l-1][j+l]-pre[i+l][j-l-1]+pre[i-l-1][j-l-1];
			if(box==1ll*(2*l+1)*(2*l+1)){
				b[i][j]=1;
			}
		}
	}

	auto pren=b;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			pren[i][j]+=pren[i-1][j]+pren[i][j-1]-pren[i-1][j-1];
		}
	}

	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			int rx=min(n,i+l), ry=min(m,j+l);
			int lx=max(1,i-l), ly=max(1,j-l);

			int box=pren[rx][ry]-pren[lx-1][ry]-pren[rx][ly-1]+pren[lx-1][ly-1];
			if(a[i][j] && (!box)){
				return 0;
			}
			if((!a[i][j]) && (box)){
				return 0;
			}
		}
	}

	if(pr){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(b[i][j]){
					putchar('X');
				}
				else{
					putchar('.');
				}
			}
			putchar('\n');
		}
	}
	return 1;
}

int main(){
	scanf("%d%d",&n,&m);
	a.resize(n+1,vector<int>(m+1,0));

	for(int i=1;i<=n;i++){
		string s;
		scanf("%s",s.c_str());
		for(int j=1;j<=m;j++){
			a[i][j]=(s[j-1]=='X');
		}
	}
	pre=a;

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

	int l=0,r=max(n/2+5,m/2+5);
	while(l+1<r){
		int mid=(l+r)>>1;
		if(check(mid,0)){
			l=mid;
		}
		else{
			r=mid;
		}
	}
	printf("%d\n",l);
	check(l,1);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m;
vector<vector<int>> a, pre;

bool check(int l, bool pr){
	vector<vector<int>> b(n+1,vector<int>(m+1,0));
	for(int i=l+1;i+l<=n;i++){
		for(int j=l+1;j+l<=m;j++){
			int box=pre[i+l][j+l]-pre[i-l-1][j+l]-pre[i+l][j-l-1]+pre[i-l-1][j-l-1];
			if(box==1ll*(2*l+1)*(2*l+1)){
				b[i][j]=1;
			}
		}
	}

	auto pren=b;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			pren[i][j]+=pren[i-1][j]+pren[i][j-1]-pren[i-1][j-1];
		}
	}

	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			int rx=min(n,i+l), ry=min(m,j+l);
			int lx=max(1,i-l), ly=max(1,j-l);

			int box=pren[rx][ry]-pren[lx-1][ry]-pren[rx][ly-1]+pren[lx-1][ly-1];
			if(a[i][j] && (!box)){
				return 0;
			}
			if((!a[i][j]) && (box)){
				return 0;
			}
		}
	}

	if(pr){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(b[i][j]){
					putchar('X');
				}
				else{
					putchar('.');
				}
			}
			putchar('\n');
		}
	}
	return 1;
}

char s[1000005];

int main(){
	scanf("%d%d",&n,&m);
	a.resize(n+1,vector<int>(m+1,0));

	for(int i=1;i<=n;i++){
		scanf("%s",s);
		for(int j=1;j<=m;j++){
			a[i][j]=(s[j-1]=='X');
		}
	}
	pre=a;

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

	int l=0,r=max(n/2+5,m/2+5);
	while(l+1<r){
		int mid=(l+r)>>1;
		if(check(mid,0)){
			l=mid;
		}
		else{
			r=mid;
		}
	}
	printf("%d\n",l);
	check(l,1);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m;
vector<vector<int>> a, pre;

bool check(int l, bool pr){
	vector<vector<int>> b(n+1,vector<int>(m+1,0));
	for(int i=l+1;i+l<=n;i++){
		for(int j=l+1;j+l<=m;j++){
			int box=pre[i+l][j+l]-pre[i-l-1][j+l]-pre[i+l][j-l-1]+pre[i-l-1][j-l-1];
			if(box==1ll*(2*l+1)*(2*l+1)){
				b[i][j]=1;
			}
		}
	}

	auto pren=b;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			pren[i][j]+=pren[i-1][j]+pren[i][j-1]-pren[i-1][j-1];
		}
	}

	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			int rx=min(n,i+l), ry=min(m,j+l);
			int lx=max(1,i-l), ly=max(1,j-l);

			int box=pren[rx][ry]-pren[lx-1][ry]-pren[rx][ly-1]+pren[lx-1][ly-1];
			if(a[i][j] && (!box)){
				return 0;
			}
			if((!a[i][j]) && (box)){
				return 0;
			}
		}
	}

	if(pr){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(b[i][j]){
					putchar('X');
				}
				else{
					putchar('.');
				}
			}
			putchar('\n');
		}
	}
	return 1;
}

char s[1000005];

int main(){
	scanf("%d%d",&n,&m);
	a.resize(n+1,vector<int>(m+1,0));

	for(int i=1;i<=n;i++){
		scanf("%s",s);
		for(int j=1;j<=m;j++){
			a[i][j]=(s[j-1]=='X');
		}
	}
	pre=a;

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

	int l=0,r=min(n/2+5,m/2+5);
	while(l+1<r){
		int mid=(l+r)>>1;
		if(check(mid,0)){
			l=mid;
		}
		else{
			r=mid;
		}
	}
	printf("%d\n",l);
	check(l,1);
	return 0;
}


bool debug=false;
#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;
#define F first
#define S second
const int N=4e3+10;
const int INF=1e9+10;
bool ma[N],ans[N],went[N];
int n,m,dis[N],dx[8]={1,1,1,0,0,-1,-1,-1},dy[8]={1,0,-1,1,-1,1,0,-1};
pair<int,int> id[N];
int min(int a,int b){return a>b?b:a;}
int pos(int a,int b){
	return a*(m+2)+b;
}
bool can(int x,int y){
	if(x<0||x>n+1)return false;
	if(y<0||y>m+1)return false;
	return true;
}
int ABS(int a){return a>=0?a:-a;}
bool ok(int x){
	if(debug)cout<<"ok?"<<x<<endl;
	int now=0,temp,X,Y,nxt;
	queue<int> q;
	bool have=false;
	memset(went,false,sizeof(went));
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++){
		temp=pos(i,j);
		if(!ma[temp]){
			dis[temp]=x;
			went[temp]=true;
			q.push(temp);
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++)if(can(X+dx[i],Y+dy[i])){
			temp=pos(X+dx[i],Y+dy[i]);
			if(!ma[temp]){
				if(dis[nxt]>=x-1)continue;
				else {
					if(debug)cout<<"here temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
					if(debug)cout<<"here nxt="<<X<<" "<<Y<<endl;
					if(debug)cout<<"dis[temp]="<<dis[temp]<<endl;
					if(debug)cout<<"dis[nxt]="<<dis[nxt]<<endl;
					return false;
				}
			}
			else if(!went[temp]){
				dis[temp]=max(-1,dis[nxt]-1);
				went[temp]=true;
				q.push(temp);
			}
			else if(ABS(dis[temp]-dis[nxt])>1){
				if(debug)cout<<"temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
				return false;
			}
		}
	}
	if(debug){
		for(int i=1;i<=n;i++,cout<<endl)for(int j=1;j<=m;j++)cout<<dis[pos(i,j)];
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(dis[temp]<0)ans[temp]=true;
		else ans[temp]=false;
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(ans[temp]){
			dis[temp]=0;
			went[temp]=false;
			q.push(temp);
		}else{
			dis[temp]=x+1;
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++){
			temp=pos(X+dx[i],Y+dy[i]);
			if(went[temp]){
				dis[temp]=dis[nxt]+1;
				q.push(temp);
				went[temp]=false;
			}
		}
	}
	if(debug){
		cout<<"back::\n";
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++)cout<<dis[pos(i,j)]<<" ";
			cout<<'\n';
		}
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if((dis[temp]<=x)!=ma[temp])return false;
	}
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int l,r,mid,now;
	string s;
	cin>>n>>m;
	memset(ma,false,sizeof(ma));
	now=m+2;
	for(int i=1;i<=n;i++){
		cin>>s;
		now++;
		for(int j=0;j<m;j++)ma[now++]=(s[j]=='X');
		now++;
	}
	now=0;
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)id[now++]={i,j};
	l=0;
	r=(min(n,m)+1)>>1;
	while(r-l>1){
		mid=(l+r)>>1;
		if(ok(mid))l=mid;
		else r=mid;
	}
	ok(l);
	cout<<l<<'\n';
	now=m+2;
	for(int i=1;i<=n;i++,cout<<'\n'){
		now++;
		for(int j=0;j<m;j++)if(ans[now++])cout<<"X";
		else cout<<".";
		now++;
	}
}


bool debug=false;
#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;
#define F first
#define S second
const int N=4e6+10;
const int INF=1e9+10;
bool ma[N],ans[N],went[N];
int n,m,dis[N],dx[8]={1,1,1,0,0,-1,-1,-1},dy[8]={1,0,-1,1,-1,1,0,-1};
pair<int,int> id[N];
int min(int a,int b){return a>b?b:a;}
int pos(int a,int b){
	return a*(m+2)+b;
}
bool can(int x,int y){
	if(x<0||x>n+1)return false;
	if(y<0||y>m+1)return false;
	return true;
}
int ABS(int a){return a>=0?a:-a;}
bool ok(int x){
	if(debug)cout<<"ok?"<<x<<endl;
	int now=0,temp,X,Y,nxt;
	queue<int> q;
	bool have=false;
	memset(went,false,sizeof(went));
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++){
		temp=pos(i,j);
		if(!ma[temp]){
			dis[temp]=x;
			went[temp]=true;
			q.push(temp);
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++)if(can(X+dx[i],Y+dy[i])){
			temp=pos(X+dx[i],Y+dy[i]);
			if(!ma[temp]){
				if(dis[nxt]>=x-1)continue;
				else {
					if(debug)cout<<"here temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
					if(debug)cout<<"here nxt="<<X<<" "<<Y<<endl;
					if(debug)cout<<"dis[temp]="<<dis[temp]<<endl;
					if(debug)cout<<"dis[nxt]="<<dis[nxt]<<endl;
					return false;
				}
			}
			else if(!went[temp]){
				dis[temp]=max(-1,dis[nxt]-1);
				went[temp]=true;
				q.push(temp);
			}
			else if(ABS(dis[temp]-dis[nxt])>1){
				if(debug)cout<<"temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
				return false;
			}
		}
	}
	if(debug){
		for(int i=1;i<=n;i++,cout<<endl)for(int j=1;j<=m;j++)cout<<dis[pos(i,j)];
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(dis[temp]<0)ans[temp]=true;
		else ans[temp]=false;
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(ans[temp]){
			dis[temp]=0;
			went[temp]=false;
			q.push(temp);
		}else{
			dis[temp]=x+1;
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++){
			temp=pos(X+dx[i],Y+dy[i]);
			if(went[temp]){
				dis[temp]=dis[nxt]+1;
				q.push(temp);
				went[temp]=false;
			}
		}
	}
	if(debug){
		cout<<"back::\n";
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++)cout<<dis[pos(i,j)]<<" ";
			cout<<'\n';
		}
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if((dis[temp]<=x)!=ma[temp])return false;
	}
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int l,r,mid,now;
	string s;
	cin>>n>>m;
	memset(ma,false,sizeof(ma));
	now=m+2;
	for(int i=1;i<=n;i++){
		cin>>s;
		now++;
		for(int j=0;j<m;j++)ma[now++]=(s[j]=='X');
		now++;
	}
	now=0;
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)id[now++]={i,j};
	l=0;
	r=(min(n,m)+1)>>1;
	while(r-l>1){
		mid=(l+r)>>1;
		if(ok(mid))l=mid;
		else r=mid;
	}
	ok(l);
	cout<<l<<'\n';
	now=m+2;
	for(int i=1;i<=n;i++,cout<<'\n'){
		now++;
		for(int j=0;j<m;j++)if(ans[now++])cout<<"X";
		else cout<<".";
		now++;
	}
}


bool debug=false;
#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;
#define F first
#define S second
const int N=4e6+10;
const int INF=1e9+10;
bool ma[N],ans[N],went[N];
int n,m,dis[N],dx[8]={1,1,1,0,0,-1,-1,-1},dy[8]={1,0,-1,1,-1,1,0,-1};
pair<int,int> id[N];
int min(int a,int b){return a>b?b:a;}
int pos(int a,int b){
	return a*(m+2)+b;
}
bool can(int x,int y){
	if(x<0||x>n+1)return false;
	if(y<0||y>m+1)return false;
	return true;
}
int ABS(int a){return a>=0?a:-a;}
bool ok(int x){
	if(debug)cout<<"ok?"<<x<<endl;
	int now=0,temp,X,Y,nxt;
	queue<int> q;
	bool have=false;
	memset(went,false,sizeof(went));
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++){
		temp=pos(i,j);
		if(!ma[temp]){
			dis[temp]=x;
			went[temp]=true;
			q.push(temp);
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++)if(can(X+dx[i],Y+dy[i])){
			temp=pos(X+dx[i],Y+dy[i]);
			if(!ma[temp]){
				if(dis[nxt]>=x-1)continue;
				else {
					if(debug)cout<<"here temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
					if(debug)cout<<"here nxt="<<X<<" "<<Y<<endl;
					if(debug)cout<<"dis[temp]="<<dis[temp]<<endl;
					if(debug)cout<<"dis[nxt]="<<dis[nxt]<<endl;
					return false;
				}
			}
			else if(!went[temp]){
				dis[temp]=max(-1,dis[nxt]-1);
				went[temp]=true;
				q.push(temp);
			}
		}
	}
	if(debug){
		for(int i=1;i<=n;i++,cout<<endl)for(int j=1;j<=m;j++)cout<<dis[pos(i,j)];
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(dis[temp]<0)ans[temp]=true;
		else ans[temp]=false;
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(ans[temp]){
			dis[temp]=0;
			went[temp]=false;
			q.push(temp);
		}else{
			dis[temp]=x+1;
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++){
			temp=pos(X+dx[i],Y+dy[i]);
			if(went[temp]){
				dis[temp]=dis[nxt]+1;
				q.push(temp);
				went[temp]=false;
			}
		}
	}
	if(debug){
		cout<<"back::\n";
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++)cout<<dis[pos(i,j)]<<" ";
			cout<<'\n';
		}
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if((dis[temp]<=x)!=ma[temp])return false;
	}
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int l,r,mid,now;
	string s;
	cin>>n>>m;
	memset(ma,false,sizeof(ma));
	now=m+2;
	for(int i=1;i<=n;i++){
		cin>>s;
		now++;
		for(int j=0;j<m;j++)ma[now++]=(s[j]=='X');
		now++;
	}
	now=0;
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)id[now++]={i,j};
	l=0;
	r=(min(n,m)+1);
	while(r-l>1){
		mid=(l+r)>>1;
		if(ok(mid))l=mid;
		else r=mid;
	}
	ok(l);
	cout<<l<<'\n';
	now=m+2;
	for(int i=1;i<=n;i++,cout<<'\n'){
		now++;
		for(int j=0;j<m;j++)if(ans[now++])cout<<"X";
		else cout<<".";
		now++;
	}
}


bool debug=false;
#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;
#define F first
#define S second
const int N=4e6+10;
const int INF=1e9+10;
bool ma[N],ans[N],went[N];
int n,m,dis[N],dx[8]={1,1,1,0,0,-1,-1,-1},dy[8]={1,0,-1,1,-1,1,0,-1};
pair<int,int> id[N];
int min(int a,int b){return a>b?b:a;}
int pos(int a,int b){
	return a*(m+2)+b;
}
bool can(int x,int y){
	if(x<0||x>n+1)return false;
	if(y<0||y>m+1)return false;
	return true;
}
int ABS(int a){return a>=0?a:-a;}
bool ok(int x){
	if(debug)cout<<"ok?"<<x<<endl;
	int now=0,temp,X,Y,nxt;
	queue<int> q;
	bool have=false;
	memset(went,false,sizeof(went));
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++){
		temp=pos(i,j);
		if(!ma[temp]){
			dis[temp]=x;
			went[temp]=true;
			q.push(temp);
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++)if(can(X+dx[i],Y+dy[i])){
			temp=pos(X+dx[i],Y+dy[i]);
			if(!ma[temp]){
				if(dis[nxt]>=x-1)continue;
				else {
					if(debug)cout<<"here temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
					if(debug)cout<<"here nxt="<<X<<" "<<Y<<endl;
					if(debug)cout<<"dis[temp]="<<dis[temp]<<endl;
					if(debug)cout<<"dis[nxt]="<<dis[nxt]<<endl;
					return false;
				}
			}
			else if(!went[temp]){
				dis[temp]=max(-1,dis[nxt]-1);
				went[temp]=true;
				q.push(temp);
			}
		}
	}
	if(debug){
		for(int i=1;i<=n;i++,cout<<endl)for(int j=1;j<=m;j++)cout<<dis[pos(i,j)];
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(dis[temp]<0)ans[temp]=true;
		else ans[temp]=false;
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(ans[temp]){
			dis[temp]=0;
			went[temp]=false;
			q.push(temp);
		}else{
			dis[temp]=x+1;
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++){
			temp=pos(X+dx[i],Y+dy[i]);
			if(went[temp]){
				dis[temp]=dis[nxt]+1;
				q.push(temp);
				went[temp]=false;
			}
		}
	}
	if(debug){
		cout<<"back::\n";
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++)cout<<dis[pos(i,j)]<<" ";
			cout<<'\n';
		}
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if((dis[temp]<=x)!=ma[temp])return false;
	}
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int l,r,mid,now;
	string s;
	cin>>n>>m;
	memset(ma,false,sizeof(ma));
	now=m+2;
	for(int i=1;i<=n;i++){
		cin>>s;
		now++;
		for(int j=0;j<m;j++)ma[now++]=(s[j]=='X');
		now++;
	}
	now=0;
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)id[now++]={i,j};
	l=0;
	r=(min(n,m)+1)>>1;
	while(r-l>1){
		mid=(l+r)>>1;
		if(ok(mid))l=mid;
		else r=mid;
	}
	ok(l);
	cout<<l<<'\n';
	now=m+2;
	for(int i=1;i<=n;i++,cout<<'\n'){
		now++;
		for(int j=0;j<m;j++)if(ans[now++])cout<<"X";
		else cout<<".";
		now++;
	}
}


bool debug=false;
#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;
#define F first
#define S second
const int N=4e6+10;
const int INF=1e9+10;
bool ma[N],ans[N],went[N];
int n,m,dis[N],dx[8]={1,1,1,0,0,-1,-1,-1},dy[8]={1,0,-1,1,-1,1,0,-1};
pair<int,int> id[N];
int min(int a,int b){return a>b?b:a;}
int pos(int a,int b){
	return a*(m+2)+b;
}
bool can(int x,int y){
	if(x<0||x>n+1)return false;
	if(y<0||y>m+1)return false;
	return true;
}
int ABS(int a){return a>=0?a:-a;}
bool ok(int x){
	if(debug)cout<<"ok?"<<x<<endl;
	int now=0,temp,X,Y,nxt;
	queue<int> q;
	bool have=false;
	memset(went,false,sizeof(went));
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++){
		temp=pos(i,j);
		if(!ma[temp]){
			dis[temp]=x;
			went[temp]=true;
			q.push(temp);
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++)if(can(X+dx[i],Y+dy[i])){
			temp=pos(X+dx[i],Y+dy[i]);
			if(!ma[temp]){
				if(dis[nxt]>=x-1)continue;
				else {
					if(debug)cout<<"here temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
					if(debug)cout<<"here nxt="<<X<<" "<<Y<<endl;
					if(debug)cout<<"dis[temp]="<<dis[temp]<<endl;
					if(debug)cout<<"dis[nxt]="<<dis[nxt]<<endl;
					return false;
				}
			}
			else if(!went[temp]){
				dis[temp]=max(-1,dis[nxt]-1);
				went[temp]=true;
				q.push(temp);
			}
			else dis[temp]=max(dis[temp],dis[nxt]-1);
		}
	}
	if(debug){
		for(int i=1;i<=n;i++,cout<<endl)for(int j=1;j<=m;j++)cout<<dis[pos(i,j)];
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(dis[temp]<0)ans[temp]=true;
		else ans[temp]=false;
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(ans[temp]){
			dis[temp]=0;
			went[temp]=false;
			q.push(temp);
		}else{
			dis[temp]=x+1;
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++)if(can(X+dx[i],Y+dy[i])){
			temp=pos(X+dx[i],Y+dy[i]);
			if(went[temp]){
				dis[temp]=dis[nxt]+1;
				q.push(temp);
				went[temp]=false;
			}
		}
	}
	if(debug){
		cout<<"back::\n";
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++)cout<<dis[pos(i,j)]<<" ";
			cout<<'\n';
		}
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if((dis[temp]<=x)!=ma[temp])return false;
	}
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int l,r,mid,now;
	string s;
	cin>>n>>m;
	memset(ma,false,sizeof(ma));
	now=m+2;
	for(int i=1;i<=n;i++){
		cin>>s;
		now++;
		for(int j=0;j<m;j++)ma[now++]=(s[j]=='X');
		now++;
	}
	now=0;
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)id[now++]={i,j};
	l=0;
	r=(min(n,m)+1);
	while(r-l>1){
		mid=(l+r)>>1;
		if(ok(mid))l=mid;
		else r=mid;
	}
	ok(l);
	cout<<l<<'\n';
	now=m+2;
	for(int i=1;i<=n;i++,cout<<'\n'){
		now++;
		for(int j=0;j<m;j++)if(ans[now++])cout<<"X";
		else cout<<".";
		now++;
	}
}


bool debug=false;
#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;
#define F first
#define S second
const int N=4e6+10;
bool ma[N],ans[N],went[N];
int n,m,dis[N],dx[8]={1,1,1,0,0,-1,-1,-1},dy[8]={1,0,-1,1,-1,1,0,-1};
pair<int,int> id[N];
int min(int a,int b){return a>b?b:a;}
int pos(int a,int b){
	return a*(m+2)+b;
}
bool can(int x,int y){
	if(x<0||x>n+1)return false;
	if(y<0||y>m+1)return false;
	return true;
}
int ABS(int a){return a>=0?a:-a;}
bool ok(int x){
	int now=0,temp,X,Y,nxt;
	queue<int> q;
	bool have=false;
	memset(went,false,sizeof(went));
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++){
		temp=pos(i,j);
		if(!ma[temp]){
			dis[temp]=x;
			went[temp]=true;
			q.push(temp);
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++)if(can(X+dx[i],Y+dy[i])){
			temp=pos(X+dx[i],Y+dy[i]);
			if(!ma[temp]){
				if(dis[nxt]>=x-1)continue;
				else {
					if(debug)cout<<"here temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
					if(debug)cout<<"here nxt="<<X<<" "<<Y<<endl;
					if(debug)cout<<"dis[temp]="<<dis[temp]<<endl;
					if(debug)cout<<"dis[nxt]="<<dis[nxt]<<endl;
					return false;
				}
			}
			else if(!went[temp]){
				dis[temp]=max(-1,dis[nxt]-1);
				went[temp]=true;
				q.push(temp);
			}
			else if(ABS(dis[temp]-dis[nxt])>1){
				if(debug)cout<<"temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
				return false;
			}
		}
	}
	if(debug){
		for(int i=1;i<=n;i++,cout<<endl)for(int j=1;j<=m;j++)cout<<dis[pos(i,j)];
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(dis[temp]<0)have=ans[temp]=true;
		else ans[temp]=false;
	}
	if(!have)return false;
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(ma[temp]&&dis[temp]>=0){
			have=false;
			for(int k=0;k<8;k++){
				if(dis[pos(i+dx[k],j+dy[k])]<=dis[temp]){
					have=true;
					break;
				}
			}
			if(!have){
				if(debug)cout<<"dont have "<<i<<" "<<j<<endl;
				return false;
			}
		}
	}
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int l,r,mid,now;
	string s;
	cin>>n>>m;
	memset(ma,false,sizeof(ma));
	now=m+2;
	for(int i=1;i<=n;i++){
		cin>>s;
		now++;
		for(int j=0;j<m;j++)ma[now++]=(s[j]=='X');
		now++;
	}
	now=0;
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)id[now++]={i,j};
	l=0;
	r=(min(n,m)+1)>>1;
	while(r-l>1){
		mid=(l+r)>>1;
		if(ok(mid))l=mid;
		else r=mid;
	}
	ok(l);
	cout<<l<<'\n';
	now=m+2;
	for(int i=1;i<=n;i++,cout<<'\n'){
		now++;
		for(int j=0;j<m;j++)if(ans[now++])cout<<"X";
		else cout<<".";
		now++;
	}
}


bool debug=false;
#include <iostream>
#include <queue>
#include <string.h>
#include <utility>
using namespace std;
#define F first
#define S second
const int N=4e6+10;
const int INF=1e9+10;
bool ma[N],ans[N],went[N];
int n,m,dis[N],dx[8]={1,1,1,0,0,-1,-1,-1},dy[8]={1,0,-1,1,-1,1,0,-1};
pair<int,int> id[N];
int min(int a,int b){return a>b?b:a;}
int pos(int a,int b){
	return a*(m+2)+b;
}
bool can(int x,int y){
	if(x<0||x>n+1)return false;
	if(y<0||y>m+1)return false;
	return true;
}
int ABS(int a){return a>=0?a:-a;}
bool ok(int x){
	if(debug)cout<<"ok?"<<x<<endl;
	int now=0,temp,X,Y,nxt;
	queue<int> q;
	bool have=false;
	memset(went,false,sizeof(went));
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++){
		temp=pos(i,j);
		if(!ma[temp]){
			dis[temp]=x;
			went[temp]=true;
			q.push(temp);
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++)if(can(X+dx[i],Y+dy[i])){
			temp=pos(X+dx[i],Y+dy[i]);
			if(!ma[temp]){
				if(dis[nxt]>=x-1)continue;
				else {
					if(debug)cout<<"here temp="<<X+dx[i]<<" "<<Y+dy[i]<<endl;
					if(debug)cout<<"here nxt="<<X<<" "<<Y<<endl;
					if(debug)cout<<"dis[temp]="<<dis[temp]<<endl;
					if(debug)cout<<"dis[nxt]="<<dis[nxt]<<endl;
					return false;
				}
			}
			else if(!went[temp]){
				dis[temp]=max(-1,dis[nxt]-1);
				went[temp]=true;
				q.push(temp);
			}
			else dis[temp]=max(dis[temp],dis[nxt]-1);
		}
	}
	if(debug){
		for(int i=1;i<=n;i++,cout<<endl)for(int j=1;j<=m;j++)cout<<dis[pos(i,j)];
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(dis[temp]<0)ans[temp]=true;
		else ans[temp]=false;
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if(ans[temp]){
			dis[temp]=0;
			went[temp]=false;
			q.push(temp);
		}else{
			dis[temp]=x+1;
		}
	}
	while(!q.empty()){
		nxt=q.front();
		q.pop();
		X=id[nxt].F;
		Y=id[nxt].S;
		for(int i=0;i<8;i++){
			temp=pos(X+dx[i],Y+dy[i]);
			if(went[temp]){
				dis[temp]=dis[nxt]+1;
				q.push(temp);
				went[temp]=false;
			}
		}
	}
	if(debug){
		cout<<"back::\n";
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++)cout<<dis[pos(i,j)]<<" ";
			cout<<'\n';
		}
		cout<<'\n';
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
		temp=pos(i,j);
		if((dis[temp]<=x)!=ma[temp])return false;
	}
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int l,r,mid,now;
	string s;
	cin>>n>>m;
	memset(ma,false,sizeof(ma));
	now=m+2;
	for(int i=1;i<=n;i++){
		cin>>s;
		now++;
		for(int j=0;j<m;j++)ma[now++]=(s[j]=='X');
		now++;
	}
	now=0;
	for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)id[now++]={i,j};
	l=0;
	r=(min(n,m)+1);
	while(r-l>1){
		mid=(l+r)>>1;
		if(ok(mid))l=mid;
		else r=mid;
	}
	ok(l);
	cout<<l<<'\n';
	now=m+2;
	for(int i=1;i<=n;i++,cout<<'\n'){
		now++;
		for(int j=0;j<m;j++)if(ans[now++])cout<<"X";
		else cout<<".";
		now++;
	}
}


//In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt" , "r+" , stdin) ; freopen("output.txt" , "w+" , stdout);
#define pb push_back
#define pll pair<ll, ll>
#define F first
#define S second

ll n, m;

set<ll> st[maxn];
string s[maxn];

int main(){
	fast_io;

	cin >> n >> m;

	ll dp[n + 10][m + 10];
	bool f[n + 10][m + 10], c[n + 10][m + 10], d[n + 10][m + 10];

	for(ll i = 0; i < n; i++){
		cin >> s[i];
		for(ll j = 0; j < m; j++){
			f[i + 1][j + 1] = (s[i][j] == 'X');
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) + f[i][j];
		}
	}

	ll l = 0, r = min(n + 1, m + 1) / 2, x, y;

	while(r - l > 1){
		x = (l + r) / 2;
		set<ll>::iterator itr;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m + 1; j++){
				st[i].insert(j);
				c[i][j] = 0;
				d[i][j] = 0;
			}
		}
		for(ll i = x + 1; i <= n - x; i++){
			for(ll j = x + 1; j <= m - x; j++){
				if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
					d[i][j] = 1;
					for(ll k = i - x; k <= i + x; k++){
						itr = st[k].lower_bound(j - x);
						while(*itr <= j + x){
							c[k][*itr] = 1;
							y = *itr;
							st[k].erase(itr);
							itr = st[k].lower_bound(y);
						}
					}
				}
			}
		}
		bool o = 1;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m; j++){
				if(c[i][j] != f[i][j]) o = 0;
			}
		}
		if(o) l = x;
		else r = x;
	}

	cout << l << endl;

	x = l;
	set<ll>::iterator itr;
	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m + 1; j++){
			st[i].insert(j);
			c[i][j] = 0;
			d[i][j] = 0;
		}
	}
	for(ll i = x + 1; i <= n - x; i++){
		for(ll j = x + 1; j <= m - x; j++){
			if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
				d[i][j] = 1;
				for(ll k = i - x; k <= i + x; k++){
					itr = st[k].lower_bound(j - x);
					while(*itr <= j + x){
						c[k][*itr] = 1;
						y = *itr;
						st[k].erase(itr);
						itr = st[k].lower_bound(y);
					}
				}
			}
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			cout << (d[i][j] ? 'X' : '.');
		}
		cout << endl;
	}

	return 0;
}


//In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef int ll;

const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e9;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt" , "r+" , stdin) ; freopen("output.txt" , "w+" , stdout);
#define pb push_back
#define pll pair<ll, ll>
#define F first
#define S second

ll n, m;

set<ll> st[maxn];

set<ll>::iterator itr;

int main(){
	fast_io;

	cin >> n >> m;

	char s[n + 10][m + 10];
	ll dp[n + 10][m + 10];
	bool f[n + 10][m + 10], c[n + 10][m + 10], d[n + 10][m + 10];

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			cin >> s[i][j];
			f[i][j] = (s[i][j] == 'X');
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) + f[i][j];
		}
	}

	ll l = 0, r = min(n + 1, m + 1) / 2, x, y;

	while(r - l > 1){
		x = (l + r) / 2;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m + 1; j++){
				st[i].insert(j);
				c[i][j] = d[i][j] = 0;
			}
		}
		for(ll i = x + 1; i <= n - x; i++){
			for(ll j = x + 1; j <= m - x; j++){
				if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
					d[i][j] = 1;
					for(ll k = i - x; k <= i + x; k++){
						itr = st[k].lower_bound(j - x);
						while(*itr <= j + x){
							c[k][*itr] = 1;
							y = *itr;
							st[k].erase(itr);
							itr = st[k].lower_bound(y);
						}
					}
				}
			}
		}
		bool o = 1;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m; j++){
				if(c[i][j] != f[i][j]) o = 0;
			}
		}
		if(o) l = x;
		else r = x;
	}

	cout << l << '\n';;

	x = l;
	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m + 1; j++){
			st[i].insert(j);
			c[i][j] = d[i][j] = 0;
		}
	}
	for(ll i = x + 1; i <= n - x; i++){
		for(ll j = x + 1; j <= m - x; j++){
			if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
				d[i][j] = 1;
				for(ll k = i - x; k <= i + x; k++){
					itr = st[k].lower_bound(j - x);
					while(*itr <= j + x){
						c[k][*itr] = 1;
						y = *itr;
						st[k].erase(itr);
						itr = st[k].lower_bound(y);
					}
				}
			}
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			cout << (d[i][j] ? 'X' : '.');
		}
		cout << '\n';
	}

	return 0;
}


//In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef int ll;

const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt" , "r+" , stdin) ; freopen("output.txt" , "w+" , stdout);
#define pb push_back
#define pll pair<ll, ll>
#define F first
#define S second

ll n, m;

set<ll> st[maxn];
string s[maxn];

int main(){
	fast_io;

	cin >> n >> m;

	ll dp[n + 10][m + 10];
	bool f[n + 10][m + 10], c[n + 10][m + 10], d[n + 10][m + 10];

	for(ll i = 0; i < n; i++){
		cin >> s[i];
		for(ll j = 0; j < m; j++){
			f[i + 1][j + 1] = (s[i][j] == 'X');
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) + f[i][j];
		}
	}

	ll l = 0, r = min(n + 1, m + 1) / 2, x, y;

	while(r - l > 1){
		x = (l + r) / 2;
		set<ll>::iterator itr;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m + 1; j++){
				st[i].insert(j);
				c[i][j] = 0;
				d[i][j] = 0;
			}
		}
		for(ll i = x + 1; i <= n - x; i++){
			for(ll j = x + 1; j <= m - x; j++){
				if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
					d[i][j] = 1;
					for(ll k = i - x; k <= i + x; k++){
						itr = st[k].lower_bound(j - x);
						while(*itr <= j + x){
							c[k][*itr] = 1;
							y = *itr;
							st[k].erase(itr);
							itr = st[k].lower_bound(y);
						}
					}
				}
			}
		}
		bool o = 1;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m; j++){
				if(c[i][j] != f[i][j]) o = 0;
			}
		}
		if(o) l = x;
		else r = x;
	}

	cout << l << endl;

	x = l;
	set<ll>::iterator itr;
	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m + 1; j++){
			st[i].insert(j);
			c[i][j] = 0;
			d[i][j] = 0;
		}
	}
	for(ll i = x + 1; i <= n - x; i++){
		for(ll j = x + 1; j <= m - x; j++){
			if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
				d[i][j] = 1;
				for(ll k = i - x; k <= i + x; k++){
					itr = st[k].lower_bound(j - x);
					while(*itr <= j + x){
						c[k][*itr] = 1;
						y = *itr;
						st[k].erase(itr);
						itr = st[k].lower_bound(y);
					}
				}
			}
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			cout << (d[i][j] ? 'X' : '.');
		}
		cout << endl;
	}

	return 0;
}


//In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef int ll;

const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e9;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt" , "r+" , stdin) ; freopen("output.txt" , "w+" , stdout);
#define pb push_back
#define pll pair<ll, ll>
#define F first
#define S second

ll n, m;

set<ll> st[maxn];

set<ll>::iterator itr;

int main(){
	//fast_io;

	scanf("%i %i", &n, &m);
	printf("%i %i \n", n, m);

	char s[n + 10][m + 10];
	ll dp[n + 10][m + 10];
	bool f[n + 10][m + 10], c[n + 10][m + 10], d[n + 10][m + 10];

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			scanf(" %c", &s[i][j]);
			f[i][j] = (s[i][j] == 'X');
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) + f[i][j];
		}
	}

	ll l = 0, r = min(n + 1, m + 1) / 2, x, y;

	while(r - l > 1){
		x = (l + r) / 2;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m + 1; j++){
				st[i].insert(j);
				c[i][j] = d[i][j] = 0;
			}
		}
		for(ll i = x + 1; i <= n - x; i++){
			for(ll j = x + 1; j <= m - x; j++){
				if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
					d[i][j] = 1;
					for(ll k = i - x; k <= i + x; k++){
						itr = st[k].lower_bound(j - x);
						while(*itr <= j + x){
							c[k][*itr] = 1;
							y = *itr;
							st[k].erase(itr);
							itr = st[k].lower_bound(y);
						}
					}
				}
			}
		}
		bool o = 1;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m; j++){
				if(c[i][j] != f[i][j]) o = 0;
			}
		}
		if(o) l = x;
		else r = x;
	}

	printf("%i \n", l);

	x = l;
	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m + 1; j++){
			st[i].insert(j);
			c[i][j] = d[i][j] = 0;
		}
	}
	for(ll i = x + 1; i <= n - x; i++){
		for(ll j = x + 1; j <= m - x; j++){
			if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
				d[i][j] = 1;
				for(ll k = i - x; k <= i + x; k++){
					itr = st[k].lower_bound(j - x);
					while(*itr <= j + x){
						c[k][*itr] = 1;
						y = *itr;
						st[k].erase(itr);
						itr = st[k].lower_bound(y);
					}
				}
			}
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			printf("%c", (d[i][j] ? 'X' : '.'));
		}
		printf("\n");
	}

	return 0;
}


//In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef int ll;

const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e9;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt" , "r+" , stdin) ; freopen("output.txt" , "w+" , stdout);
#define pb push_back
#define pll pair<ll, ll>
#define F first
#define S second

ll n, m;

set<ll> st[maxn];

set<ll>::iterator itr;

int main(){
	//fast_io;

	scanf("%i %i", &n, &m);

	char s[n + 10][m + 10];
	ll dp[n + 10][m + 10];
	bool f[n + 10][m + 10], c[n + 10][m + 10], d[n + 10][m + 10];

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			scanf(" %c", &s[i][j]);
			f[i][j] = (s[i][j] == 'X');
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) + f[i][j];
		}
	}

	ll l = 0, r = min(n + 1, m + 1) / 2, x, y;

	while(r - l > 1){
		x = (l + r) / 2;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m + 1; j++){
				st[i].insert(j);
				c[i][j] = d[i][j] = 0;
			}
		}
		for(ll i = x + 1; i <= n - x; i++){
			for(ll j = x + 1; j <= m - x; j++){
				if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
					d[i][j] = 1;
					for(ll k = i - x; k <= i + x; k++){
						itr = st[k].lower_bound(j - x);
						while(*itr <= j + x){
							c[k][*itr] = 1;
							y = *itr;
							st[k].erase(itr);
							itr = st[k].lower_bound(y);
						}
					}
				}
			}
		}
		bool o = 1;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m; j++){
				if(c[i][j] != f[i][j]) o = 0;
			}
		}
		if(o) l = x;
		else r = x;
	}

	printf("%i \n", l);

	x = l;
	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m + 1; j++){
			st[i].insert(j);
			c[i][j] = d[i][j] = 0;
		}
	}
	for(ll i = x + 1; i <= n - x; i++){
		for(ll j = x + 1; j <= m - x; j++){
			if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
				d[i][j] = 1;
				for(ll k = i - x; k <= i + x; k++){
					itr = st[k].lower_bound(j - x);
					while(*itr <= j + x){
						c[k][*itr] = 1;
						y = *itr;
						st[k].erase(itr);
						itr = st[k].lower_bound(y);
					}
				}
			}
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			printf("%c", (d[i][j] ? 'X' : '.'));
		}
		printf("\n");
	}

	return 0;
}


//In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef int ll;

const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e9;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt" , "r+" , stdin) ; freopen("output.txt" , "w+" , stdout);
#define pb push_back
#define pll pair<ll, ll>
#define F first
#define S second

ll n, m;

set<ll> st[maxn];
string s[maxn];

set<ll>::iterator itr;

int main(){
	fast_io;

	cin >> n >> m;

	ll dp[n + 10][m + 10];
	bool f[n + 10][m + 10], c[n + 10][m + 10], d[n + 10][m + 10];

	for(ll i = 0; i < n; i++){
		cin >> s[i];
		for(ll j = 0; j < m; j++){
			f[i + 1][j + 1] = (s[i][j] == 'X');
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) + f[i][j];
		}
	}

	ll l = 0, r = min(n + 1, m + 1) / 2, x, y;

	while(r - l > 1){
		x = (l + r) / 2;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m + 1; j++){
				st[i].insert(j);
				c[i][j] = d[i][j] = 0;
			}
		}
		for(ll i = x + 1; i <= n - x; i++){
			for(ll j = x + 1; j <= m - x; j++){
				if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
					d[i][j] = 1;
					for(ll k = i - x; k <= i + x; k++){
						itr = st[k].lower_bound(j - x);
						while(*itr <= j + x){
							c[k][*itr] = 1;
							y = *itr;
							st[k].erase(itr);
							itr = st[k].lower_bound(y);
						}
					}
				}
			}
		}
		bool o = 1;
		for(ll i = 1; i <= n; i++){
			for(ll j = 1; j <= m; j++){
				if(c[i][j] != f[i][j]) o = 0;
			}
		}
		if(o) l = x;
		else r = x;
	}

	cout << l << '\n';

	x = l;
	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m + 1; j++){
			st[i].insert(j);
			c[i][j] = d[i][j] = 0;
		}
	}
	for(ll i = x + 1; i <= n - x; i++){
		for(ll j = x + 1; j <= m - x; j++){
			if(dp[i + x][j + x] + dp[i - x - 1][j - x - 1] - dp[i + x][j - x - 1] - dp[i - x - 1][j + x] == (2 * x + 1) * (2 * x + 1)){
				d[i][j] = 1;
				for(ll k = i - x; k <= i + x; k++){
					itr = st[k].lower_bound(j - x);
					while(*itr <= j + x){
						c[k][*itr] = 1;
						y = *itr;
						st[k].erase(itr);
						itr = st[k].lower_bound(y);
					}
				}
			}
		}
	}

	for(ll i = 1; i <= n; i++){
		for(ll j = 1; j <= m; j++){
			cout << (d[i][j] ? 'X' : '.');
		}
		cout << '\n';
	}

	return 0;
}


#include <set>
#include <map>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1000010;

int n, m;
char str[N];
string board[N];

int main() {

	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++) {
		scanf("%s", str);
		board[i] = str;
	}

	vector < vector <int> > s(n + 1, vector <int> (m + 1, 0));

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

	auto check = [&](int mid) {
		vector < vector <int> > t(n + 1, vector <int> (m + 1, 0));
		long long r = 2 * mid + 1;
		for (int i = 0; i <= n - r; i++) {
			for (int j = 0; j <= m - r; j++) {
				if (s[i + r][j + r] - s[i + r][j] - s[i][j + r] + s[i][j] == r * r) {
					t[i][j]++;
					t[i + r][j]--;
					t[i][j + r]--;
					t[i + r][j + r]++;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (i != 0) {
					t[i][j] += t[i - 1][j];
				}
				if (j != 0) {
					t[i][j] += t[i][j - 1];
				}
				if (i != 0 && j != 0) {
					t[i][j] += t[i - 1][j - 1];
				}
				if (board[i][j] == 'X' && t[i][j] == 0) {
					return false;
				}
			}
		}
		return true;
	};

	int low = 0;
	int high = max(n, m);

	while (low <= high) {
		int mid = (low + high) / 2;
		if (check(mid)) {
			low = mid + 1;
		} else {
			high = mid - 1;
		}
	}

	long long r = 2 * high + 1;

	vector <string> ans(n, string(m, '.'));

	for (int i = 0; i <= n - r; i++) {
		for (int j = 0; j <= m; j++) {
			if (s[i + r][j + r] - s[i + r][j] - s[i][j + r] + s[i][j] == r * r) {
				ans[i + high][j + high] = 'X';
			}
		}
	}

	printf("%d\n", high);

	for (int i = 0; i < n; i++) {
		printf("%s\n", ans[i].c_str());
	}

	return 0;

}


#include <set>
#include <map>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1000010;

int n, m;
char str[N];
string board[N];

int main() {

	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++) {
		scanf("%s", str);
		board[i] = str;
	}

	vector < vector <int> > s(n + 1, vector <int> (m + 1, 0));

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

	auto check = [&](int mid) {
		vector < vector <int> > t(n + 1, vector <int> (m + 1, 0));
		int r = 2 * mid + 1;
		for (int i = 0; i <= n - r; i++) {
			for (int j = 0; j <= m - r; j++) {
				if (s[i + r][j + r] - s[i + r][j] - s[i][j + r] + s[i][j] == r * r) {
					t[i][j]++;
					t[i + r][j]--;
					t[i][j + r]--;
					t[i + r][j + r]++;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (i != 0) {
					t[i][j] += t[i - 1][j];
				}
				if (j != 0) {
					t[i][j] += t[i][j - 1];
				}
				if (i != 0 && j != 0) {
					t[i][j] += t[i - 1][j - 1];
				}
				if (board[i][j] == 'X' && t[i][j] == 0) {
					return false;
				}
			}
		}
		return true;
	};

	int low = 0;
	int high = max(n, m);

	while (low <= high) {
		int mid = (low + high) / 2;
		if (check(mid)) {
			low = mid + 1;
		} else {
			high = mid - 1;
		}
	}

	int r = 2 * high + 1;

	vector <string> ans(n, string(m, '.'));

	for (int i = 0; i <= n - r; i++) {
		for (int j = 0; j <= m; j++) {
			if (s[i + r][j + r] - s[i + r][j] - s[i][j + r] + s[i][j] == r * r) {
				ans[i + high][j + high] = 'X';
			}
		}
	}

	printf("%d\n", high);

	for (int i = 0; i < n; i++) {
		printf("%s\n", ans[i].c_str());
	}

	return 0;

}