#include <bits/stdc++.h>
#define mp make_pair
#define ff first
#define se second
#define pb push_back
#define nn 2010
#define pii pair<int,int>
#define mt make_tuple
#define ll long long int
#define pdd pair<long double,long double>
#define db double
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define inf 10000000000010ll
#define logn 20
#define mod 1000000007
#define mt make_tuple
#define nullp mp(-1,-1)

using namespace std;

short int s[nn][nn];
bool v[nn][nn];

pii check(int i,int j)
{
	pii ret(-1,-1);
	int a0=s[i][j]+s[i-1][j]+s[i-1][j-1]+s[i][j-1];
	int a1=s[i][j]+s[i-1][j]+s[i-1][j+1]+s[i][j+1];
	int a2=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	int a3=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	if(a0==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i-1][j])
			return mp(i-1,j);
		if(s[i-1][j-1]==1)
			return mp(i-1,j-1);
		return mp(i,j-1);
	}
	if(a1==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i-1][j])
			return mp(i-1,j);
		if(s[i-1][j+1]==1)
			return mp(i-1,j+1);
		return mp(i,j+1);
	}
	if(a2==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i+1][j])
			return mp(i+1,j);
		if(s[i+1][j+1]==1)
			return mp(i+1,j+1);
		return mp(i,j+1);
	}
	if(a3==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i+1][j])
			return mp(i+1,j);
		if(s[i+1][j-1]==1)
			return mp(i+1,j-1);
		return mp(i,j-1);
	}
	return ret;
}

void solve(int i,int j)
{
	if(!s[i][j])
		return;
	queue<pii>q;
	pii a=check(i,j);
	if(a==nullp)
		return;
	q.push(a);
	v[a.ff][a.se]=1;
	while(!q.empty())
	{
		pii t=q.front();
		a=check(t.ff,t.se);
		q.pop();
		while(a!=nullp)
		{
			s[a.ff][a.se]=0;
			if(!v[a.ff][a.se])
				q.push(a);
			v[a.ff][a.se]=1;
			a=check(t.ff,t.se);
		}
	}
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int n,m;
    cin>>n>>m;
    char ch;
    memset(s,1,sizeof s);
    list<pii>li;
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    	{
    		cin>>ch,s[i][j]=(ch=='*')?1:0;
    		if(s[i][j])
    			li.pb(mp(i,j));
    	}
    }
    for(list<pii>::iterator u=li.begin();u!=li.end();u++)
    {
    	int i=u->ff,j=u->se;
    	if(!v[i][j])
    		solve(i,j);
    }
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		cout<<((s[i][j])?'*':'.');
    	cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define ff first
#define se second
#define pb push_back
#define nn 2010
#define pii pair<int,int>
#define mt make_tuple
#define ll long long int
#define pdd pair<long double,long double>
#define db double
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define inf 10000000000010ll
#define logn 20
#define mod 1000000007
#define mt make_tuple
#define nullp mp(-1,-1)

using namespace std;

int s[nn][nn];
bool v[nn][nn];

bool check(int i,int j)
{
	int a0=s[i][j]+s[i-1][j]+s[i-1][j-1]+s[i][j-1];
	int a1=s[i][j]+s[i-1][j]+s[i-1][j+1]+s[i][j+1];
	int a2=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	int a3=s[i][j]+s[i+1][j]+s[i+1][j-1]+s[i][j-1];
	if(a0==1 || a1==1 || a2==1 || a3==1)
		return true;
	return false;
}

/*void solve(int i,int j)
{
	queue<pii>q;
	pii a=check(i,j);
	if(a==nullp)
		return;
	q.push(a);
	v[a.ff][a.se]=1;
	while(!q.empty())
	{
		pii t=q.front();
		a=check(t.ff,t.se);
		q.pop();
		while(a!=nullp)
		{
			s[a.ff][a.se]=0;
			if(!v[a.ff][a.se])
				q.push(a);
			v[a.ff][a.se]=1;
			a=check(t.ff,t.se);
		}
	}
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int n,m;
    cin>>n>>m;
    char ch;
    memset(s,1,sizeof s);
    list<pii>li;
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    	{
    		cin>>ch,s[i][j]=(ch=='*')?1:0;
    		if(s[i][j])
    			li.pb(mp(i,j));
    	}
    }
    /*for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		if(s[i][j] && !v[i][j])
    			solve(i,j);
    }*/
    int prev=0,cur=0;
    while(true)
    {
    	list<pii>::iterator v;
    	for(list<pii>::iterator u=li.begin();u!=li.end();)
    	{
    		v=u;
    		v++;
    		int i=u->ff,j=u->se;
    		if(check(i,j))
    				s[i][j]=0,cur++,li.erase(u);
    		u=v;
    	}
    	//cout<<1;
    	if(cur==prev || li.empty())
    		break;
    	prev=cur;
    }
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		cout<<((s[i][j])?'*':'.');
    	cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define ff first
#define se second
#define pb push_back
#define nn 2010
#define pii pair<int,int>
#define mt make_tuple
#define ll long long int
#define pdd pair<long double,long double>
#define db double
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define inf 10000000000010ll
#define logn 20
#define mod 1000000007
#define mt make_tuple
#define nullp mp(-1,-1)

using namespace std;

int s[nn][nn];
bool v[nn][nn];

bool check(int i,int j)
{
	int a0=s[i][j]+s[i-1][j]+s[i-1][j-1]+s[i][j-1];
	int a1=s[i][j]+s[i-1][j]+s[i-1][j+1]+s[i][j+1];
	int a2=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	int a3=s[i][j]+s[i+1][j]+s[i+1][j-1]+s[i][j-1];
	if(a0==1 || a1==1 || a2==1 || a3==1)
		return true;
	return false;
}

/*void solve(int i,int j)
{
	queue<pii>q;
	pii a=check(i,j);
	if(a==nullp)
		return;
	q.push(a);
	v[a.ff][a.se]=1;
	while(!q.empty())
	{
		pii t=q.front();
		a=check(t.ff,t.se);
		q.pop();
		while(a!=nullp)
		{
			s[a.ff][a.se]=0;
			if(!v[a.ff][a.se])
				q.push(a);
			v[a.ff][a.se]=1;
			a=check(t.ff,t.se);
		}
	}
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int n,m;
    cin>>n>>m;
    char ch;
    memset(s,1,sizeof s);
    for(int i=1;i<=n;i++)
    	for(int j=1;j<=m;j++)
    		cin>>ch,s[i][j]=(ch=='*')?1:0;
    /*for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		if(s[i][j] && !v[i][j])
    			solve(i,j);
    }*/
    int prev=0,cur=0;
    for(int rep=1;rep<=4;rep++)
    {
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=1;j<=m;j++)
    		{
    			if(!s[i][j])
    				continue;
    			if(check(i,j))
    				s[i][j]=0,cur++;
    		}
    	}
    	//cout<<1;
    	if(cur==prev)
    		break;
    	prev=cur;
    }
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		cout<<((s[i][j])?'*':'.');
    	cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define ff first
#define se second
#define pb push_back
#define nn 2010
#define pii pair<int,int>
#define mt make_tuple
#define ll long long int
#define pdd pair<long double,long double>
#define db double
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define inf 10000000000010ll
#define logn 20
#define mod 1000000007
#define mt make_tuple
#define nullp mp(-1,-1)

using namespace std;

int s[nn][nn];
bool v[nn][nn];

bool check(int i,int j)
{
	int a0=s[i][j]+s[i-1][j]+s[i-1][j-1]+s[i][j-1];
	int a1=s[i][j]+s[i-1][j]+s[i-1][j+1]+s[i][j+1];
	int a2=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	int a3=s[i][j]+s[i+1][j]+s[i+1][j-1]+s[i][j-1];
	if(a0==1 || a1==1 || a2==1 || a3==1)
		return true;
	return false;
}

/*void solve(int i,int j)
{
	queue<pii>q;
	pii a=check(i,j);
	if(a==nullp)
		return;
	q.push(a);
	v[a.ff][a.se]=1;
	while(!q.empty())
	{
		pii t=q.front();
		a=check(t.ff,t.se);
		q.pop();
		while(a!=nullp)
		{
			s[a.ff][a.se]=0;
			if(!v[a.ff][a.se])
				q.push(a);
			v[a.ff][a.se]=1;
			a=check(t.ff,t.se);
		}
	}
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int n,m;
    cin>>n>>m;
    char ch;
    memset(s,1,sizeof s);
    list<pii>li;
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    	{
    		cin>>ch,s[i][j]=(ch=='*')?1:0;
    		if(s[i][j])
    			li.pb(mp(i,j));
    	}
    }
    /*for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		if(s[i][j] && !v[i][j])
    			solve(i,j);
    }*/
    int prev=0,cur=0;
    while(true)
    {
    	for(list<pii>::iterator u=li.begin();u!=li.end();u++)
    	{
    		int i=u->ff,j=u->se;
    		if(check(i,j))
    				s[i][j]=0,cur++,li.erase(u);
    	}
    	//cout<<1;
    	if(cur==prev)
    		break;
    	prev=cur;
    }
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		cout<<((s[i][j])?'*':'.');
    	cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define ff first
#define se second
#define pb push_back
#define nn 2010
#define pii pair<int,int>
#define mt make_tuple
#define ll long long int
#define pdd pair<long double,long double>
#define db double
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define inf 10000000000010ll
#define logn 20
#define mod 1000000007
#define mt make_tuple
#define nullp mp(-1,-1)

using namespace std;

int s[nn][nn];
bool v[nn][nn];

bool check(int i,int j)
{
	int a0=s[i][j]+s[i-1][j]+s[i-1][j-1]+s[i][j-1];
	int a1=s[i][j]+s[i-1][j]+s[i-1][j+1]+s[i][j+1];
	int a2=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	int a3=s[i][j]+s[i+1][j]+s[i+1][j-1]+s[i][j-1];
	if(a0==1 || a1==1 || a2==1 || a3==1)
		return true;
	return false;
}

/*void solve(int i,int j)
{
	queue<pii>q;
	pii a=check(i,j);
	if(a==nullp)
		return;
	q.push(a);
	v[a.ff][a.se]=1;
	while(!q.empty())
	{
		pii t=q.front();
		a=check(t.ff,t.se);
		q.pop();
		while(a!=nullp)
		{
			s[a.ff][a.se]=0;
			if(!v[a.ff][a.se])
				q.push(a);
			v[a.ff][a.se]=1;
			a=check(t.ff,t.se);
		}
	}
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int n,m;
    cin>>n>>m;
    char ch;
    memset(s,1,sizeof s);
    for(int i=1;i<=n;i++)
    	for(int j=1;j<=m;j++)
    		cin>>ch,s[i][j]=(ch=='*')?1:0;
    /*for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		if(s[i][j] && !v[i][j])
    			solve(i,j);
    }*/
    int prev=0,cur=0;
    while(true)
    {
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=1;j<=m;j++)
    		{
    			if(!s[i][j])
    				continue;
    			if(check(i,j))
    				s[i][j]=0,cur++;
    		}
    	}
    	//cout<<1;
    	if(cur==prev)
    		break;
    	prev=cur;
    }
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		cout<<((s[i][j])?'*':'.');
    	cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define ff first
#define se second
#define pb push_back
#define nn 2010
#define pii pair<int,int>
#define mt make_tuple
#define ll long long int
#define pdd pair<long double,long double>
#define db double
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define inf 10000000000010ll
#define logn 20
#define mod 1000000007
#define mt make_tuple
#define nullp mp(-1,-1)

using namespace std;

int s[nn][nn];
bool v[nn][nn];

pii check(int i,int j)
{
	pii ret(-1,-1);
	int a0=s[i][j]+s[i-1][j]+s[i-1][j-1]+s[i][j-1];
	int a1=s[i][j]+s[i-1][j]+s[i-1][j+1]+s[i][j+1];
	int a2=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	int a3=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	if(a0==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i-1][j])
			return mp(i-1,j);
		if(s[i-1][j-1]==1)
			return mp(i-1,j-1);
		return mp(i,j-1);
	}
	if(a1==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i-1][j])
			return mp(i-1,j);
		if(s[i-1][j+1]==1)
			return mp(i-1,j+1);
		return mp(i,j+1);
	}
	if(a2==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i+1][j])
			return mp(i+1,j);
		if(s[i+1][j+1]==1)
			return mp(i+1,j+1);
		return mp(i,j+1);
	}
	if(a3==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i+1][j])
			return mp(i+1,j);
		if(s[i+1][j-1]==1)
			return mp(i+1,j-1);
		return mp(i,j-1);
	}
	return ret;
}

void solve(int i,int j)
{
	queue<pii>q;
	pii a=check(i,j);
	if(a==nullp)
		return;
	q.push(a);
	while(!q.empty())
	{
		pii t=q.front();
		a=check(t.ff,t.se);
		q.pop();
		while(a!=nullp)
		{
			s[a.ff][a.se]=0;
			q.push(a);
			a=check(t.ff,t.se);
		}
	}
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int n,m;
    cin>>n>>m;
    char ch;
    memset(s,1,sizeof s);
    for(int i=1;i<=n;i++)
    	for(int j=1;j<=m;j++)
    		cin>>ch,s[i][j]=(ch=='*')?1:0;
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		if(s[i][j])
    			solve(i,j);
    }
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		cout<<((s[i][j])?'*':'.');
    	cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define ff first
#define se second
#define pb push_back
#define nn 2010
#define pii pair<int,int>
#define mt make_tuple
#define ll long long int
#define pdd pair<long double,long double>
#define db double
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define inf 10000000000010ll
#define logn 20
#define mod 1000000007
#define mt make_tuple
#define nullp mp(-1,-1)

using namespace std;

int s[nn][nn];
bool v[nn][nn];

pii check(int i,int j)
{
	pii ret(-1,-1);
	int a0=s[i][j]+s[i-1][j]+s[i-1][j-1]+s[i][j-1];
	int a1=s[i][j]+s[i-1][j]+s[i-1][j+1]+s[i][j+1];
	int a2=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	int a3=s[i][j]+s[i+1][j]+s[i+1][j+1]+s[i][j+1];
	if(a0==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i-1][j])
			return mp(i-1,j);
		if(s[i-1][j-1]==1)
			return mp(i-1,j-1);
		return mp(i,j-1);
	}
	if(a1==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i-1][j])
			return mp(i-1,j);
		if(s[i-1][j+1]==1)
			return mp(i-1,j+1);
		return mp(i,j+1);
	}
	if(a2==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i+1][j])
			return mp(i+1,j);
		if(s[i+1][j+1]==1)
			return mp(i+1,j+1);
		return mp(i,j+1);
	}
	if(a3==1)
	{
		if(s[i][j]==1)
			return mp(i,j);
		if(s[i+1][j])
			return mp(i+1,j);
		if(s[i+1][j-1]==1)
			return mp(i+1,j-1);
		return mp(i,j-1);
	}
	return ret;
}

void solve(int i,int j)
{
	queue<pii>q;
	pii a=check(i,j);
	if(a==nullp)
		return;
	q.push(a);
	v[a.ff][a.se]=1;
	while(!q.empty())
	{
		pii t=q.front();
		a=check(t.ff,t.se);
		q.pop();
		while(a!=nullp)
		{
			s[a.ff][a.se]=0;
			if(!v[a.ff][a.se])
				q.push(a);
			v[a.ff][a.se]=1;
			a=check(t.ff,t.se);
		}
	}
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int n,m;
    cin>>n>>m;
    char ch;
    memset(s,1,sizeof s);
    for(int i=1;i<=n;i++)
    	for(int j=1;j<=m;j++)
    		cin>>ch,s[i][j]=(ch=='*')?1:0;
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		if(s[i][j] && !v[i][j])
    			solve(i,j);
    }
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=m;j++)
    		cout<<((s[i][j])?'*':'.');
    	cout<<'\n';
    }
    return 0;
}


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

const int N = 2000;

int n, m;
string s[N];

int check(int i, int j) {
	if (i < 0 || n <= i+1 || j < 0 || m <= j+1) {
		return -1;
	}
	int cnt = 0;
	for (int ii = i; ii <= i+1; ii++) {
		for (int jj = j; jj <= j+1; jj++) {
			cnt += s[ii][jj] == '*';
		}
	}
	if (cnt != 1) {
		return -1;
	}
	for (int ii = i; ii <= i+1; ii++) {
		for (int jj = j; jj <= j+1; jj++) {
			if (s[ii][jj] == '*') {
				return ii*m + jj;
			}
		}
	}
	assert(false);
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++){
		cin >> s[i];
	}

	queue<int> q;
	vector<bool> was(n*m);
	auto maybe_push = [&q, &was](int i, int j) {
		int k = check(i, j);
		if (k != -1 && !was[k]) {
			q.push(k);
			was[k] = true;
		}
	};
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			maybe_push(i, j);
		}
	}

	while (!q.empty()) {
		int k = q.front();
		q.pop();

		int i = k/m;
		int j = k%m;

		s[i][j] = '.';
		maybe_push(i, j);
		maybe_push(i-1, j);
		maybe_push(i, j-1);
		maybe_push(i-1, j-1);
	}

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

	return 0;
}


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

string grid[2005];
int n, m;
int vis[2005][2005];

bool okay(int x, int y) {
    if (x < 0 or x > n-2) return 0;
    if (y < 0 or y > m-2) return 0;
    int c = 0;
    c += (grid[x][y] == '*');
    c += (grid[x][y+1] == '*');
    c += (grid[x+1][y] == '*');
    c += (grid[x+1][y+1] == '*');
    return c == 1;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 0; i < n; i++) {
        cin >> grid[i];
    }
    queue <pair<int,int>> que;
    for(int i = 0; i < n-1; i++) {
        for(int j = 0; j < m-1; j++) {
            if (okay(i, j)) {
                vis[i][j] = 1;
                que.push({i, j});
            }
        }
    }
    while(que.size()) {
        pair<int, int> P = que.front();
        que.pop();
        int x = P.first, y = P.second;
        grid[x][y] = '.';
        grid[x+1][y] = '.';
        grid[x][y+1] = '.';
        grid[x+1][y+1] = '.';
        for(int k = -2; k <= 2; k++) {
            for(int l = -2; l <= 2; l++) {
                if (okay(x-k, y-l)) {
                    if (vis[x-k][y-l] == 0) {
                        vis[x-k][y-l] = 1;
                        que.push({x-k, y-l});
                    }
                }
            }
        }
    }

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


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

int n,m;
string s[2005];

pair<int,int> f(int x,int y,int xx,int yy){
    pair<int,int> res=make_pair(-1,-1);
    if (x<=0 || xx>n || y<=0 || yy>m) return res;
    int kol=0;
    for (int i=x;i<=xx;++i)
        for (int j=y;j<=yy;++j)
            if (s[i][j-1]=='*') ++kol;
    if (kol!=1) return res;
    for (int i=x;i<=xx;++i)
        for (int j=y;j<=yy;++j)
            if (s[i][j-1]=='*') { s[i][j-1]='.'; return make_pair(i,j);}
}
void chek(int x,int y){
    pair<int,int> t;
    t=f(x-1,y-1,x,y);
    if (t.first!=-1) chek(t.first,t.second);
    t=f(x,y-1,x+1,y);
    if (t.first!=-1) chek(t.first,t.second);
    t=f(x,y,x+1,y+1);
    if (t.first!=-1) chek(t.first,t.second);
    t=f(x-1,y,x,y+1);
    if (t.first!=-1) chek(t.first,t.second);
}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for (int i=1;i<=n;++i)
        cin>>s[i];
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
            chek(i,j);
    for (int i=1;i<=n;++i)
        cout<<s[i]<<'\n';
}


#include <iostream>
#include <algorithm>
using namespace std;
int a[200005];
int main(){
	int t,n;
	cin >> t;
	while(t--){
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> a[i];
		}
		int k =0;
		for(int i=n-1;i>0;i--){
			if(a[i]!=a[i-1]){
				cout << 1 << endl;
				break;
			}
		}
		cout << n << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int a[200005];
int main(){
	int t,n;
	cin >> t;
	while(t--){
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> a[i];
		}
		sort(a,a+n);
		int k =0;
		for(int i=n-1;i>0;i--){
			if(a[i]==a[i-1]){
				k++;
			}else{
				break;
			}
		}
		k++;
		cout << k << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long a[200005];
int main(){
	int t,n;
	cin >> t;
	while(t--){
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> a[i];
		}
		int k =0;
		for(int i=n-1;i>0;i--){
			if(a[i]!=a[i-1]){
				cout << 1 << endl;
				break;
			}
		}
		cout << n << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int a[200005];
int main(){
	int t,n;
	cin >> t;
	while(t--){
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> a[i];
		}
		sort(a,a+n);
		int k =0;
		for(int i=n-1;i>0;i--){
			if(a[i]!=a[i-1]){
				k++;
				break;
			}
		}
		k++;
		cout << k << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int t,n;
	cin >> t;
	while(t--){
		cin >> n;
		long now =0;
		long pre =-1;
		int k =0;
		for(int i=0;i<n;i++){
			cin >> now;
			if(pre != -1){
				if(now != pre){
					k =1;
				}
			}else{
				pre = now;
			}
		}
		if(k)
			cout << 1 << endl;
		else
			cout << n << endl;
	}

	return 0;
}


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

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

    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;

        int arr[n], mx = -1;
        for(int i = 0; i < n; ++i)
        {
            cin >> arr[i];
            mx = max(mx, arr[i]);
        }

        int ans = n;
        for(int i = 0; i < n; ++i)
            if(arr[i] != mx)
            {
                ans = 1;
                break;
            }

        cout << ans << "\n";
    }

    return 0;
}


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

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

    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;

        int arr[n], mx = -1;
        for(int i = 0; i < n; ++i)
        {
            cin >> arr[i];
            mx = max(mx, arr[i]);
        }

        int ans = min(2, n);
        for(int i = 0; i < n; ++i)
            if(arr[i] != mx)
            {
                ans = 1;
                break;
            }

        cout << ans << "\n";
    }

    return 0;
}


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

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

    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;

        stack<int> s;
        for(int i = 0; i < n; ++i)
        {
            int x;
            cin >> x;

            while(!s.empty() && s.top() != x)
            {
                x += s.top();
                s.pop();
            }

            s.push(x);
        }

        cout << s.size() << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[100100];
int next[100100];
//////////////////////////////////////////
void solve(){
	int n;
	cin>>n;
	int tmp=0;
	cin>>tmp;
	bool f = false;
	for(int i=2;i<=n;i++){
		int q;
		cin>>q;
		if(tmp!=q)f=1;
	}
	if(f) cout<<"1\n";
	else cout<<n<<"\n";
}

int main(){
	int T;
	cin>>T;
	while(T--){
		solve();
	}
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>

#define MAXN 200005

using namespace std;

int a[MAXN],n,t;

int main(void){

    scanf("%d",&t);
    while(t--){
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        int maxx=-0x7fffffff,minn=0x7fffffff;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            maxx=max(maxx,a[i]);
            minn=min(minn,a[i]);
        }
        if(maxx==minn)printf("%d\n",n);
        else puts("1");
    }

    return 0;
}


#include<bits/stdc++.h>
#define fint register int
#define h 5001
#define p 3432492
using namespace std;
int a[p];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		for(fint i=1;i<=n;i++)
		cin>>a[i];
		bool ff=0;
		for(fint i=1;i<n;i++)
		if(a[i]!=a[i+1])
		{
			cout<<1<<endl;
			ff=1;
			break;
		}
		if(!ff)
		cout<<n<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int MAXN = 200100;
char s[MAXN];
int n, m, a[MAXN];
int main() {
#ifdef TEST
	freopen("input.txt", "r", stdin);
#endif
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 0; i < n; ++i) {
			scanf("%d", &a[i]);
		}
		printf("%d\n", a[0] == a[n - 1] ? n : 1);
	}
	return 0;
}

/*
2
4
2 1 3 1
2
420 420
*/


#include <cstdio>
#include <iostream>
using namespace std;
int a[200050];
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n;
		cin >> n;
		for(int i=1;i<=n;i++)
		{
			cin >> a[i];
		}
		bool flag = 0;
		for(int i=2;i<=n;i++)
		{
			if(a[i]!=a[1])
			{
				flag = true;
				break;
			}
		}
		printf("%d\n",flag?1:n);
	}

	return 0;
}


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

void output(vector<long> v,int n)
{
    sort(v.begin(),v.end())
    if(v[0]==v[n-1])
        cout<<v.size()<<endl;
    else
        cout<<1<<endl;
}

int main()
{
    int t;
    cin>>t;
    long n;
    for(int i=1;i<=t;i++)
    {
        cin>>n;
        vector<long> v;
        for(int j=0;j<n;j++)
        {
            cin>>v[j];
        }
        output(v,n);
    }
}


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

void output(vector<long> v,int n)
{
    sort(v.begin(),v.end());
    if(v[0]==v[n-1])
        cout<<v.size()<<endl;
    else
        cout<<1<<endl;
}

int main()
{
    int t;
    cin>>t;
    long n;
    for(int i=1;i<=t;i++)
    {
        cin>>n;
        vector<long> v;
        for(int j=0;j<n;j++)
        {
            cin>>v[j];
        }
        output(v,n);
    }
}


#include<bits/stdc++.h>

void output(vector<long> v,int n)
{
    sort(v.begin(),v.end())
    if(v[0]==v[n])
        cout<<v.size()<<endl;
    else
        cout<<1<<endl;
}

int main()
{
    int t;
    cin>>t;
    long n;
    for(int i=1;i<=t;i++)
    {
        cin>>n;
        vector<long> v;
        for(int j=0;j<n;j++)
        {
            cin>>v[j];
        }
        output(v,n);
    }
}


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

void output(vector<long> v,int n)
{
    sort(v.begin(),v.end());
    if(v[0]==v[n-1])
        cout<<v.size()<<endl;
    else
        cout<<1<<endl;
}

int main()
{
    int t;
    cin>>t;
    long n;
    for(int i=1;i<=t;i++)
    {
        cin>>n;
        vector<long> v;
        for(int j=0;j<n;j++)
        {
            int num;
            cin>>num;
            v.push_back(num);
        }
        output(v,n);
    }
}


#include<iostream>
using namespace std;
int main(){
    int t; cin>>t;
    while(t--){
        int n; cin>>n;
        long long int s[n];
        for(int i=0;i<n;i++){
            cin>>s[i];
        }
        int rand= s[0];
        int one=false;
        for(int i=1;i<n;i++){
            if(s[i]!=rand){one =true; break;}
        }
        if(n==1||one) cout<<"1"<<endl;
        else cout<<n<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main(){
    int t; cin>>t;
    while(t--){
        int n; cin>>n;
        long long int s[n];
        for(int i=0;i<n;i++){
            cin>>s[i];
        }
        int rand= s[0];
        int one=false;
        for(int i=1;i<n;i++){
            if(s[i]!=rand){one =true; break;}
        }
        if(n==1||one) cout<<"1 "<<endl;
        else cout<<"n "<<endl;
    }
    return 0;
}


#include<iostream>
int main(){
    int t; cin>>t;
    while(t--){
        int n; cin>>n;
        long long int s[n];
        for(int i=0;i<n;i++){
            cin>>s[i];
        }
        int rand= s[0];
        int one=false;
        for(int i=1;i<n;i++){
            if(s[i]!=rand){one =true; break;}
        }
        if(n==1||one) cout<<"1 "<<endl;
        else cout<<"n "<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t; cin>>t;
    for(int i=0 ; i<t ; i++){
        int n; cin>>n;
        vector<int>vec(n);
        for(int j=0 ; j<n ; j++){
            cin>>vec[j];
        }
        x:
        if(vec.size()==1){
            cout<<1<<endl;
            continue;
        }else if(vec.size()==2){
            if(vec[0]==vec[1]){
                cout<<2<<endl;
            }else{
                cout<<1<<endl;
            }continue;
        }

        for(int j=1 ; j<vec.size()-1 ; j++){
            if(vec[j]==vec[j-1]){
                //cout<<"if 1"<<endl;
                if(vec[j]==vec[j+1])continue;
                vec[j]+=vec[j+1];
                vec.erase(vec.begin()+(j+1));
                j-=1;
            }
            if(vec[j-1]>vec[j+1] && vec[j]!=vec[j-1]){
                //cout<<"if 2"<<endl;
                vec[j]+=vec[j-1];
                vec.erase(vec.begin()+(j-1));
                j-=1;
            }else if(vec[j-1]<vec[j+1] && vec[j]!=vec[j+1]){
                //cout<<"else if"<<endl;
                vec[j]+=vec[j+1];
                vec.erase(vec.begin()+(j+1));
                j-=1;
            }
        }goto x;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t; cin>>t;
    for(int i=0 ; i<t ; i++){
        int n; cin>>n;
        vector<int>vec(n);
        for(int j=0 ; j<n ; j++){
            cin>>vec[j];
        }bool k=false;
        while(k==false){
            k=true;
            for(int j=0 ; j<vec.size()-1 ; j++){
                if(vec[j]!=vec[j+1]){
                    k=false;
                    vec[j]=vec[j]+vec[j+1];
                    vec[j+1]=vec[(vec.size()-1)];
                    vec.pop_back();
                }
            }
        }cout<<vec.size()<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t; cin>>t;
    for(int i=0 ; i<t ; i++){
        int n,k=0,kk; cin>>n;
        for(int j=0 ; j<n ; j++){
            int a; cin>>a;
            if(j==0){
                kk=a;
            }else{
                if(kk!=a){
                    k=1;
                }
            }
        }if(k==0){
            cout<<n<<endl;
        }else{
            cout<<1<<endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>t;
        ll n,a[200000];
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        if(a[0]!=a[n-1])
        {
            cout<<1<<endl;
        }
        else
        {
            cout<<n<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,a[200000];
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        if(a[0]!=a[n-1])
        {
            cout<<1<<endl;
        }
        else
        {
            cout<<n<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    int t;
    while(t--)
    {
        cin>>t;
        ll n,a[200000];
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        if(a[0]!=a[n-1])
        {
            cout<<1<<endl;
        }
        else
        {
            cout<<n<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> &input, int N)
{
    int temp;
    int M = 0;
    while (M++ != N)
    {
        for (auto it = input.begin(); it != input.end() - 1;)
        {
            if (*it != *(it + 1))
            {
                temp = *it + *(it + 1);
                input.erase(it);
                *(it) = temp;
            }
            else
                it++;
        }
        if (count(input.begin(), input.end(), input[0]) == input.size())
            break;
    }
    // for (auto it = input.begin(); it != input.end(); it++)
    //     cout << *it << " ";
    // cout << endl;
    return input.size();
}
int main()
{
    int T;
    cin >> T;
    int N;
    while (T--)
    {

        cin >> N;
        vector<int> input(N);
        for (int i = 0; i < N; ++i)
            cin >> input[i];

        cout << solution(input, N) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> &input, int N)
{
    int temp;
    int count = 0;
    while (count++ != N)
    {
        for (auto it = input.begin(); it != input.end() - 1;)
        {
            if (*it != *(it + 1))
            {
                temp = *it + *(it + 1);
                input.erase(it);
                *(it) = temp;
            }
            else
                it++;
        }
        if (count(input.begin(), input.end(), input[0]) == input.size())
            break;
    }
    // for (auto it = input.begin(); it != input.end(); it++)
    //     cout << *it << " ";
    // cout << endl;
    return input.size();
}
int main()
{
    int T;
    cin >> T;
    int N;
    while (T--)
    {

        cin >> N;
        vector<int> input(N);
        for (int i = 0; i < N; ++i)
            cin >> input[i];

        cout << solution(input, N) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> &ar, int N)
{
    vector<long long int> input;
    input.assign(ar.begin(), ar.end());
    long long temp;
    int M = 0;
    while (M++ != N)
    {
        for (auto it = input.begin(); it != input.end() - 1;)
        {
            if (*it != *(it + 1))
            {
                temp = *it + *(it + 1);
                input.erase(it);
                *(it) = temp;
                if (it != input.begin())
                    it--;
            }
            else
                it++;
        }
        if (count(input.begin(), input.end(), input[0]) == input.size())
            break;
    }
    return input.size();
}
int main()
{
    int T;
    cin >> T;
    int N;
    while (T--)
    {

        cin >> N;
        vector<int> input(N);
        for (int i = 0; i < N; ++i)
            cin >> input[i];

        cout << solution(input, N) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> &input, int N)
{
    int temp;
    while (true)
    {
        for (auto it = input.begin(); it != input.end() - 1;)
        {
            if (*it != *(it + 1))
            {
                temp = *it + *(it + 1);
                input.erase(it);
                *(it) = temp;
            }
            else
                it++;
        }
        if (count(input.begin(), input.end(), input[0]) == input.size())
            break;
    }
    // for (auto it = input.begin(); it != input.end(); it++)
    //     cout << *it << " ";
    // cout << endl;
    return input.size();
}
int main()
{
    int T;
    cin >> T;
    int N;
    while (T--)
    {

        cin >> N;
        vector<int> input(N);
        for (int i = 0; i < N; ++i)
            cin >> input[i];

        cout << solution(input, N) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> &ar, int N)
{
    vector<long long int> input;
    input.assign(ar.begin(), ar.end());
    long long temp;
    int M = 0;
    while (M++ != N)
    {
        for (auto it = input.begin(); it != input.end() - 1;)
        {
            if (*it != *(it + 1))
            {
                temp = *it + *(it + 1);
                input.erase(it);
                *(it) = temp;
            }
            else
                it++;
        }
        if (count(input.begin(), input.end(), input[0]) == input.size())
            break;
    }
    return input.size();
}
int main()
{
    int T;
    cin >> T;
    int N;
    while (T--)
    {

        cin >> N;
        vector<int> input(N);
        for (int i = 0; i < N; ++i)
            cin >> input[i];

        cout << solution(input, N) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
void debug(const vector<long long> &input)
{
    cout << " { ";
    for (auto it = input.begin(); it != input.end(); it++)
        cout << *it << " , ";
    cout << " } " << endl;
}
int solution(vector<int> &ar, int N)
{
    if (max_element(ar.begin(), ar.end()) == min_element(ar.begin(), ar.end()))
        return N;
    else
        return 1;
}
int main()
{
    int T;
    cin >> T;
    int N;
    while (T--)
    {

        cin >> N;
        vector<int> input(N);
        for (int i = 0; i < N; ++i)
            cin >> input[i];

        cout << solution(input, N) << endl;
    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int x;
    cin >> x;
    for(int r = 0;r < x;r++){
        int a,b;
        cin >> a;
        int tab[a],ile = 0;
        for(int i = 0;i < a;i++){
            cin >> tab[i];
        }
        int czy = tab[0];
        for(int i = 0;i < a;i++){
            if(czy == tab[i]){
                ile++;
            }
        }
        if(ile == a){
            cout << a;
        }else{
            cout << 1;
        }
    }
}


#include <iostream>

using namespace std;

int main()
{
    int x;
    cin >> x;
    int tab4[x];
    for(int r = 0;r < x;r++){
        int a,b;
        cin >> a;
        int tab[a],ile = 0;
        for(int i = 0;i < a;i++){
            cin >> tab[i];
        }
        int czy = tab[0];
        for(int i = 0;i < a;i++){
            if(czy == tab[i]){
                ile++;
            }
        }
        if(ile == a){
            tab4[r] = a;
        }else{
            tab4[r] = 1;
        }
    }
    for(int i= 0;i < x;i++){
        cout << tab4[i] << endl;
    }
}


#include <iostream>

using namespace std;

int main()
{
    int a,b;
    cin >> a;
    int tab[a],ile = 0;
    for(int i = 0;i < a;i++){
        cin >> tab[i];
    }
    int czy = tab[0];
    for(int i = 0;i < a;i++){
        if(czy == tab[i]){
            ile++;
        }
    }
    if(ile == a){
        cout << a;
    }else{
        cout << 1;
    }
}


#include <iostream>

using namespace std;

int main()
{
    int x;
    cin >> x;
    int tab4[x];
    for(int r = 0;r < x;r++){
        int a,b;
        cin >> a;
        int tab[a],ile = 0;
        for(int i = 0;i < a;i++){
            cin >> tab[i];
        }
        int czy = tab[0];
        for(int i = 0;i < a;i++){
            if(czy == tab[i]){
                ile++;
            }
        }
        if(ile == a){
            tab4[x] = a;
        }else{
            tab4[x] = 1;
        }
    }
    for(int i= 0;i < x;i++){
        cout << tab4[i] << endl;
    }
}


#include<bits/stdc++.h>
#include <math.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define ll long long int
#define mp make_pair
#define ss second
#define ff first
#define pb push_back
#define M 1000000007
#define PI 3.14159265358979323846

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<pair<ll,int>, null_type , less<pair<ll,int>>, rb_tree_tag , tree_order_statistics_node_update> Ordered_Set;
//order_of_key (k) : Number of items strictly smaller than k.
//find_by_order(k) : K-th element in a set (counting from zero).

int main()
{

  fast;
  int t;
  cin>>t;
  while(t--)
  {
      int n;
      cin>>n;
      int a[n];
      int c=0;
      stack<int> st;
      for(int i=0;i<n;i++)
      {
           cin>>a[i];
           if(i!=0 && a[i]==a[0])
           c++;
      }
      if(c==n-1)
      cout<<n<<endl;
      else
      {
          for(int i=0;i<n;i++)
          {
              if(st.empty())
              st.push(a[i]);
              else
              {
                  if(st.top()!=a[i])
                  {
                      int tt=st.top();
                      st.pop();
                      st.push(tt+a[i]);
                      while(st.size()>=2)
                      {
                          int tt=st.top();
                          st.pop();
                          int tt1=st.top();
                          if(tt!=tt1)
                          {
                              st.pop();
                              st.push(tt1+tt);
                          }
                          else
                          break;
                      }
                  }
                  else
                  st.push(a[i]);
              }
          }
          stack<int> st1;
          for(int i=n-1;i>=0;i--)
          {
              if(st1.empty())
              st1.push(a[i]);
              else
              {
                  if(st1.top()!=a[i])
                  {
                      int tt=st1.top();
                      st1.pop();
                      st1.push(tt+a[i]);
                      while(st1.size()>=2)
                      {
                          int tt=st1.top();
                          st1.pop();
                          int tt1=st1.top();
                          if(tt!=tt1)
                          {
                              st1.pop();
                              st1.push(tt1+tt);
                          }
                          else
                          break;
                      }
                  }
                  else
                  st1.push(a[i]);
              }
          }
          cout<<min(st.size(),st1.size())<<endl;
      }

  }
  return 0;

}


#include<bits/stdc++.h>
#include <math.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define ll long long int
#define mp make_pair
#define ss second
#define ff first
#define pb push_back
#define M 1000000007
#define PI 3.14159265358979323846

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<pair<ll,int>, null_type , less<pair<ll,int>>, rb_tree_tag , tree_order_statistics_node_update> Ordered_Set;
//order_of_key (k) : Number of items strictly smaller than k.
//find_by_order(k) : K-th element in a set (counting from zero).

int main()
{

  fast;
  int t;
  cin>>t;
  while(t--)
  {
      int n;
      cin>>n;
      ll a[n];
      int c=0;
      stack<ll> st;
      for(int i=0;i<n;i++)
      {
           cin>>a[i];
           if(i!=0 && a[i]==a[0])
           c++;
      }
     if(c==n-1)
     {
         cout<<n<<endl;
         continue;
     }
     cout<<1<<endl;

  }
  return 0;

}


#include<bits/stdc++.h>
#include <math.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define ll long long int
#define mp make_pair
#define ss second
#define ff first
#define pb push_back
#define M 1000000007
#define PI 3.14159265358979323846

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<pair<ll,int>, null_type , less<pair<ll,int>>, rb_tree_tag , tree_order_statistics_node_update> Ordered_Set;
//order_of_key (k) : Number of items strictly smaller than k.
//find_by_order(k) : K-th element in a set (counting from zero).

int main()
{

  fast;
  int t;
  cin>>t;
  while(t--)
  {
      int n;
      cin>>n;
      ll a[n];
      int c=0;
      stack<ll> st;
      for(int i=0;i<n;i++)
      {
           cin>>a[i];
           if(i!=0 && a[i]==a[0])
           c++;
      }
      if(c==n-1)
      cout<<n<<endl;
      else
      {
          for(int i=0;i<n;i++)
          {
              if(st.empty())
              st.push(a[i]);
              else
              {
                  if(st.top()!=a[i])
                  {
                      int tt=st.top();
                      st.pop();
                      st.push(tt+a[i]);
                      while(st.size()>=2)
                      {
                          int tt=st.top();
                          st.pop();
                          int tt1=st.top();
                          if(tt!=tt1)
                          {
                              st.pop();
                              st.push(tt1+tt);
                          }
                          else
                          break;
                      }
                  }
                  else
                  st.push(a[i]);
              }
          }
          stack<ll> st1;
          for(int i=n-1;i>=0;i--)
          {
              if(st1.empty())
              st1.push(a[i]);
              else
              {
                  if(st1.top()!=a[i])
                  {
                      int tt=st1.top();
                      st1.pop();
                      st1.push(tt+a[i]);
                      while(st1.size()>=2)
                      {
                          int tt=st1.top();
                          st1.pop();
                          int tt1=st1.top();
                          if(tt!=tt1)
                          {
                              st1.pop();
                              st1.push(tt1+tt);
                          }
                          else
                          break;
                      }
                  }
                  else
                  st1.push(a[i]);
              }
          }
          while(st1.size()>=2)
                      {
                          int tt=st1.top();
                          st1.pop();
                          int tt1=st1.top();
                          if(tt!=tt1)
                          {
                              st1.pop();
                              st1.push(tt1+tt);
                          }
                          else
                          break;
                      }
                       while(st.size()>=2)
                      {
                          int tt=st.top();
                          st.pop();
                          int tt1=st.top();
                          if(tt!=tt1)
                          {
                              st.pop();
                              st.push(tt1+tt);
                          }
                          else
                          break;
                      }
          cout<<min(st.size(),st1.size())<<endl;
      }

  }
  return 0;

}


#include<bits/stdc++.h>
#include <math.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define ll long long int
#define mp make_pair
#define ss second
#define ff first
#define pb push_back
#define M 1000000007
#define PI 3.14159265358979323846

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<pair<ll,int>, null_type , less<pair<ll,int>>, rb_tree_tag , tree_order_statistics_node_update> Ordered_Set;
//order_of_key (k) : Number of items strictly smaller than k.
//find_by_order(k) : K-th element in a set (counting from zero).

int main()
{

  fast;
  int t;
  cin>>t;
  while(t--)
  {
      int n;
      cin>>n;
      int a[n];
      int c=0;
      stack<int> st;
      for(int i=0;i<n;i++)
      {
           cin>>a[i];
           if(i!=0 && a[i]==a[0])
           c++;
      }
      if(c==n-1)
      cout<<n<<endl;
      else
      {
          for(int i=0;i<n;i++)
          {
              if(st.empty())
              st.push(a[i]);
              else
              {
                  if(st.top()!=a[i])
                  {
                      int tt=st.top();
                      st.pop();
                      st.push(tt+a[i]);
                      while(st.size()>=2)
                      {
                          int tt=st.top();
                          st.pop();
                          int tt1=st.top();
                          if(tt!=tt1)
                          {
                              st.pop();
                              st.push(tt1+tt);
                          }
                          else
                          break;
                      }
                  }
                  else
                  st.push(a[i]);
              }
          }
          stack<int> st1;
          for(int i=n-1;i>=0;i--)
          {
              if(st1.empty())
              st1.push(a[i]);
              else
              {
                  if(st1.top()!=a[i])
                  {
                      int tt=st1.top();
                      st1.pop();
                      st1.push(tt+a[i]);
                      while(st1.size()>=2)
                      {
                          int tt=st1.top();
                          st1.pop();
                          int tt1=st1.top();
                          if(tt!=tt1)
                          {
                              st1.pop();
                              st1.push(tt1+tt);
                          }
                          else
                          break;
                      }
                  }
                  else
                  st1.push(a[i]);
              }
          }
          while(st1.size()>=2)
                      {
                          int tt=st1.top();
                          st1.pop();
                          int tt1=st1.top();
                          if(tt!=tt1)
                          {
                              st1.pop();
                              st1.push(tt1+tt);
                          }
                          else
                          break;
                      }
                       while(st.size()>=2)
                      {
                          int tt=st.top();
                          st.pop();
                          int tt1=st.top();
                          if(tt!=tt1)
                          {
                              st.pop();
                              st.push(tt1+tt);
                          }
                          else
                          break;
                      }
          cout<<min(st.size(),st1.size())<<endl;
      }

  }
  return 0;

}


#include<bits/stdc++.h>
#include <math.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define ll long long int
#define mp make_pair
#define ss second
#define ff first
#define pb push_back
#define M 1000000007
#define PI 3.14159265358979323846

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<pair<ll,int>, null_type , less<pair<ll,int>>, rb_tree_tag , tree_order_statistics_node_update> Ordered_Set;
//order_of_key (k) : Number of items strictly smaller than k.
//find_by_order(k) : K-th element in a set (counting from zero).

int main()
{

  fast;
  int t;
  cin>>t;
  while(t--)
  {
      int n;
      cin>>n;
      int a[n];
      int c=0;
      stack<int> st;
      for(int i=0;i<n;i++)
      {
           cin>>a[i];
           if(i!=0 && a[i]==a[0])
           c++;
      }
      if(c==n-1)
      cout<<n<<endl;
      else
      {
          for(int i=0;i<n;i++)
          {
              if(st.empty())
              st.push(a[i]);
              else
              {
                  if(st.top()!=a[i])
                  {
                      int tt=st.top();
                      st.pop();
                      st.push(tt+a[i]);
                      while(st.size()>=2)
                      {
                          int tt=st.top();
                          st.pop();
                          int tt1=st.top();
                          if(tt!=tt1)
                          {
                              st.pop();
                              st.push(tt1+tt);
                          }
                          else
                          break;
                      }
                  }
                  else
                  st.push(a[i]);
              }
          }
          cout<<st.size()<<endl;
      }

  }
  return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[200000+8];
int main()
{
   int t,n,i,j,k;
   cin>>t;
   while(t--)
   {
       cin>>n;
       bool flag=0;
       for(i=0;i<n;i++)
       {
           cin>>a[i];
       }
       for(i=1;i<n;i++)
       {
           if(a[i]!=a[i-1])
           {
               flag=1;
           }
       }
       if(flag==1)
       {
           cout<<1<<endl;
       }
       else
       {
           cout<<n<<endl;
       }
   }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#define mid ((l + r) >> 1)
using namespace std;
//Ready-------------------------------------------------------------------
const int MAX = 0x7FFFFFFF;
const int INF = 0x3F3F3F3F;
void read(int &x) {
    int f = 1; x = 0; char c = getchar();
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    x *= f;
}
void Max(int &x, int y) {x = (x > y) ? x : y;}
void Min(int &x, int y) {x = (x < y) ? x : y;}
//------------------------------------------------------------------------
int main() {
    int T; read(T);
    while (T--) {
        int n, now = -1; read(n);
        bool fg = true;
        for (int i = 1; i <= n; i++) {
            int x; read(x);
            if (now == -1) now = x;
            else if (now != x) fg = false;
        }
        if (fg) printf("%d\n", n);
        else printf("1\n");
    }
    return 0;
}


#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)
#include<bits/stdc++.h>
using namespace std;
inline int input()
{
    register int x=0;register char a=getchar();
    while(a<'0'||a>'9')a=getchar();
    while(a>='0'&&a<='9')x=(x<<1)+(x<<3)+(a^48),a=getchar();
    return x;
}
int main(){
	int t=input();
	for(int i=1;i<=t;++i){
		int n=input();
		int x=input();
		int f=0;
		for(int j=2;j<=n;++j){
			int y=input();
			if(x!=y)f=1;
			x=y;
		}
		if(!f)cout << n << endl;
		else puts("1");
	}
}


#include <bits/stdc++.h>
using namespace std;
int a[100100];
int next[100100];

void solve(){
	int n;
	cin>>n;
	int tmp=0;
	cin>>tmp;
	bool f = false;
	for(int i=2;i<=n;i++){
		int q;
		cin>>q;
		if(tmp!=q)f=1;
	}
	if(f) cout<<"1\n";
	else cout<<n<<"\n";
}

int main(){
	int T;
	cin>>T;
	while(T--){
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
int T,n,a[200005],b[200005],ans;
int solve(int x,int t){
	if(t==n)
	return 1;
	int temp=0;
	for(int j=t+1;j<=n;j++){
		if(temp!=a[j])
		 temp+=a[j];
		else
		return 0;
		if(temp==x)
		return solve(x,j);
		else if(temp>x)
		return 0;
	}
}
int main(){
	int i;
	scanf("%d",&T);
	while(T--){
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		scanf("%d",&n);
		ans=n;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			b[i]=b[i-1]+a[i];
		}
		for(i=1;i<=n;i++){
			if(a[i]!=b[i-1]&&b[n]%b[i]==0&&solve(b[i],i))
			 ans=min(ans,b[n]/b[i]);
			else if(a[i]==b[i-1])
			break;
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int T,f,n,a[200005];
int main(){
	int i;
	scanf("%d",&T);
	while(T--){
		memset(a,0,sizeof(a));
		scanf("%d",&n);
		f=0;
		scanf("%d",&a[0]);
		for(i=1;i<n;i++){
			scanf("%d",&a[i]);
			if(a[i]!=a[i-1])
			 f=1;
		}
		if(f)
		printf("1\n");
		else
		printf("%d\n",n);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int T,f,n,a[200005];
int main(){
	int i;
	scanf("%d",&T);
	while(T--){
		memset(a,0,sizeof(a));
		scanf("%d",&n);
		f=0;
		scanf("%d",&a[0]);
		for(i=1;i<n;i++){
			scanf("%d",&a[i]);
			if(a[i]!=a[i-1])
			 f=1;
		}
		if(f)
		printf("1\n");
		else
		printf("%d",n);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int T,n,a[200005],b[200005],ans;
int solve(int x,int t){
	if(t==n)
	return 1;
	int temp=0;
	for(int j=t+1;j<=n;j++){
		if(temp!=a[j])
		 temp+=a[j];
		else
		return 0;
		if(temp==x)
		return solve(x,j);
		else if(temp>x)
		return 0;
	}
}
int main(){
	int i;
	scanf("%d",&T);
	while(T--){
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		scanf("%d",&n);
		ans=n;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			b[i]=b[i-1]+a[i];
		}
		for(i=1;i<=n;i++){
			if(a[i]!=b[i-1]&&b[n]%b[i]==0&&solve(b[i],i))
			 ans=min(ans,b[n]/b[i]);
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t;
    cin >> t;
    while (t-- > 0) {
        int n;
        cin >> n;
        vector<int> a(n + 5);
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        int answer = n;
        for (int i = 1; i <= n - 1; i++) {
            if (a[i] != a[i + 1] or a[i] == -1 or a[i + 1] == -1) {
                answer--;
                a[i] = -1;
                a[i + 1] = -1;
            }
        }
        cout << answer << '\n';
    }

}


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

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t;
    cin >> t;
    while (t-- > 0) {
        int n;
        cin >> n;
        vector<int> a(n + 5);
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        int answer = n;
        for (int i = 1; i <= n - 1; i++) {
            if (a[i] != a[i + 1]) {
                answer--;
            }
        }
        cout << answer << '\n';
    }

}


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

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t;
    cin >> t;
    while (t-- > 0) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        if (*max_element(a.begin(), a.end()) == *min_element(a.begin(), a.end())) {
            cout << n << '\n';
        } else {
            cout << 1 << '\n';
        }
    }

}


#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;

typedef long long lint;
typedef pair<int, int> pii;

int n, a[30][30];
map<int, vector<pii>> mp;
vector<pii> v;

void f(int x, int y, int sum) {
	if (x == n && y == n) { mp[sum] = v; return; }
	if (x > n || !x || !y || y > n) return;
	v.push_back({x+1, y}); f(x+1, y, sum+a[x+1][y]); v.pop_back();
	v.push_back({x, y+1}); f(x, y+1, sum+a[x][y+1]); v.pop_back();
}

int main()
{
	int q, i, j, k = 0;

	scanf("%d", &n);
	for (i = 2; i <= 2*n; ++i) {
		int t = 0;
		for (j = max(1, i-n); j <= n; ++j, ++t) a[i-j][j] = k*t;
		k++;
	}

	for (i = 1; i <= n; ++i) {
		for (j = 1; j <= n; ++j) printf("%d ", a[i][j]);
		puts(""); fflush(stdout);
	}

	v.push_back({1, 1});
	f(1, 1, 0);

	scanf("%d", &q);
	while (q--) {
		scanf("%d", &k); printf("%d\n", k);
		for (pii x: mp[k]) {
			printf("%d %d\n", x.X, x.Y);
			fflush(stdout);
		}
		puts("");
	}

	return 0;
}


#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;

typedef long long lint;
typedef pair<int, int> pii;

int main()
{
	int T;
	scanf("%d", &T);

	while (T--) {
		int n, m, i, j;
		scanf("%d", &n);
		int a[n+3];
		int cnt = 0;
		for (i = 0; i < n; ++i) {
			scanf("%d", &a[i]);
			if (i && a[i] != a[i-1]) cnt++;
		}
		printf("%d\n", n - cnt);

	}

	return 0;
}


#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;

typedef long long lint;
typedef pair<int, int> pii;

int main()
{
	int T;
	scanf("%d", &T);

	while (T--) {
		int n, m, i, j;
		scanf("%d", &n);
		int a[n+3];
		int cnt = 0;
		for (i = 0; i < n; ++i) {
			scanf("%d", &a[i]);
			if (i && a[i] == a[i-1]) cnt++;
		}
		if (n == 2 && cnt) puts("2");
		else puts("1");

	}

	return 0;
}


#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;

typedef long long lint;
typedef pair<int, int> pii;

int main()
{
	int T;
	scanf("%d", &T);

	while (T--) {
		int n, m, i, j;
		scanf("%d", &n);
		int a[n+3];
		int cnt = 0;
		for (i = 0; i < n; ++i) {
			scanf("%d", &a[i]);
			if (i && a[i] == a[i-1]) cnt++;
		}
		if (cnt == n-1) printf("%d\n", n);
		else puts("1");

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)cin>>a[i];
        set <int> s1;
        for(int i=0;i<n;i++)s1.insert(a[i]);
        if(s1.size()==0)
        cout<<n<<endl;
        else
        cout<<1<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)cin>>a[i];
        set <int> s1;
        for(int i=0;i<n;i++)s1.insert(a[i]);
        if(s1.size()==1)
        cout<<n<<endl;
        else
        cout<<1<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)cin>>a[i];
        set <int> s1;
        for(int i=0;i<n;i++)s.insert(a[i]);
        if(s1.size()==0)
        cout<<n<<endl;
        else
        cout<<1<<endl;
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define p push
#define ll long long
#define lld long long double
#define f first
#define s second
#define all(vec) begin(vec), end(vec)
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    freopen("maxcross.in", "r", stdin);
//    freopen("maxcross.out", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        set<int> st;
        for(int i = 0; i < n; i++){
            int x;
            cin >> x;
            st.insert(x);
        }
        if((int)st.size() == 1){
            cout << 2 << '\n';
        }else cout << 1 << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define p push
#define ll long long
#define lld long long double
#define f first
#define s second
#define all(vec) begin(vec), end(vec)
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    freopen("maxcross.in", "r", stdin);
//    freopen("maxcross.out", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        set<int> st;
        for(int i = 0; i < n; i++){
            int x;
            cin >> x;
            st.insert(x);
        }
        if((int)st.size() == 1){
            cout << n << '\n';
        }else cout << 1 << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define p push
#define ll long long
#define lld long long double
#define f first
#define s second
#define all(vec) begin(vec), end(vec)
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    freopen("maxcross.in", "r", stdin);
//    freopen("maxcross.out", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        set<int> st;
        for(int i = 0; i < n; i++){
            int x;
            cin >> x;
            st.insert(x);
        }
        if((int)st.size() == 1){
            cout << 1 << '\n';
        }else cout << 2 << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        long long int n,i,j,count=0,a[5000000];
        cin>>n;
        //long long int a[n];
        for(i=0; i<n; i++)
            cin>>a[i];
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(a[i]==a[j])
                    break;
            }
            if(i==j)
                count++;
        }
        if(count==1)
            cout<<n<<endl;
        else
            cout<<"1"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        int n,i,j,count=0;
        cin>>n;
        int a[n];
        for(i=0; i<n; i++)
            cin>>a[i];
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(a[i]==a[j])
                    break;
            }
            if(i==j)
                count++;
        }
        if(count==1)
            cout<<n<<endl;
        else
            cout<<"1"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        long long int n,i,j,flag=0;
        cin>>n;
        long long int a[n];
        for(i=0; i<n; i++)
            cin>>a[i];
        for(i=0; i<n-1; i++)
        {
            if(a[i]==a[i+1])
            {
                flag=1;
                break;
            }
            else
                flag=0;
        }
        if(flag==1)
            cout<<n<<endl;
        else
            cout<<"1"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        long long int n,i,count=0,flag=0;
        cin>>n;
        long long int a[n];
        for(i=0; i<n; i++)
            cin>>a[i];
        for(i=0; i<n-1; i++)
        {
            if(a[i]==a[i+1])
            {
                count++;
            }
        }
        if(count==n-1)
            cout<<n<<endl;
        else
            cout<<"1"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        long long int n,i,j,count=0;
        cin>>n;
        long long int a[n];
        for(i=0; i<n; i++)
            cin>>a[i];
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(a[i]==a[j])
                    break;
            }
            if(i==j)
                count++;
        }
        if(count==1)
            cout<<n<<endl;
        else
            cout<<"1"<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

template <class T, class U, class AssociativeOp = std::multiplies<>>
constexpr T power(T a, U n, T init = 1, AssociativeOp op = AssociativeOp{}) {
  static_assert(std::is_integral_v<U> and not std::is_same_v<U, bool>);
  assert(n >= 0);
  while (n) {
    if (n & 1) init = op(init, a);
    if (n >>= 1) a = op(a, a);
  }
  return init;
}

struct segment_tree_base {
  virtual int size() const = 0;

 protected:
  template <class F>
  void forward(int l, int r, F f) const {
    int h = h1(l += size() - 1, r += size());
    for (int s = 0; s < h; ++s)
      if (int i = l >> s; ~i & 1) f(i + 1);
    for (int s = h; s--;)
      if (int i = r >> s; i & 1) f(i - 1);
  }
  template <class F>
  void forward(int l, int r, F f) {
    const_cast<const segment_tree_base*>(this)->forward(l, r, f);
  }
  template <class F>
  void backward(int l, int r, F f) const {
    int h = h1(l += size() - 1, r += size());
    for (int s = 0; s < h; ++s)
      if (int i = r >> s; i & 1) f(i - 1);
    for (int s = h; s--;)
      if (int i = l >> s; ~i & 1) f(i + 1);
  }
  template <class F>
  void backward(int l, int r, F f) {
    const_cast<const segment_tree_base*>(this)->backward(l, r, f);
  }
  template <class F>
  void downward(int l, int r, F f) const {
    if (l == r or (l == 0 and r == size())) return;
    int h = h2(l += size(), r += size());
    for (int s = std::__lg(l); s > h; --s) f(l >> s);
    for (int s = h; s > __builtin_ctz(l); --s) f(l >> s);
    for (int s = h; s > __builtin_ctz(r); --s) f(r >> s);
  }
  template <class F>
  void downward(int l, int r, F f) {
    const_cast<const segment_tree_base*>(this)->downward(l, r, f);
  }
  template <class F>
  void upward(int l, int r, F f) const {
    if (l == r or (l == 0 and r == size())) return;
    int h = h2(l += size(), r += size());
    for (int s = __builtin_ctz(r); s++ < h;) f(r >> s);
    for (int s = __builtin_ctz(l); s++ < h;) f(l >> s);
    for (int s = h; s++ < std::__lg(l);) f(l >> s);
  }
  template <class F>
  void upward(int l, int r, F f) {
    const_cast<const segment_tree_base*>(this)->upward(l, r, f);
  }

 private:
  static int h1(int l, int r) {
    for (int h = 0;; ++h)
      if ((r >> h) - (l >> h) == 1) return h;
  }
  static int h2(int l, int r) {
    l <<= std::__lg(l) < std::__lg(r);
    return std::__lg(l ^ r);
  }
};

template <class T>
struct segment_tree : segment_tree_base {
  segment_tree() {}
  template <class Generator>
  segment_tree(int n, Generator gen) : tree(2 * n) {
    for (int i = 0; i < n; ++i) tree[n + i] = gen(i);
    for (int i = n; i-- > 1;) pull(i);
  }

  int size() const override { return std::size(tree) / 2; }
  const T& operator[](int i) const {
    assert(0 <= i), assert(i < size());
    return tree[size() + i];
  }
  T fold(int l, int r) const {
    assert(0 <= l), assert(l <= r), assert(r <= size());
    T res{};
    forward(l, r, [&](int i) { res = res * tree[i]; });
    return res;
  }
  T fold_all_rotated() const { return size() ? tree[1] : T{}; }
  template <class Function>
  void update(int i, Function func) {
    assert(0 <= i), assert(i < size());
    tree[size() + i] = func(tree[size() + i]);
    upward(i, i + 1, [&](int j) { pull(j); });
  }
  template <class Predicate>
  int forward_search(int l, int r, Predicate pred) const {
    assert(0 <= l), assert(l <= r), assert(r <= size());
    T a{};
    assert(pred(a));
    int res = r;
    forward(l, r, [&](int i) {
      if (res < r) return;
      if (T na = a * tree[i]; pred(na)) {
        a = na;
        return;
      }
      while (i < size())
        if (T na = a * tree[i *= 2]; pred(na)) a = na, ++i;
      res = i - size();
    });
    return res;
  }
  template <class Predicate>
  int backward_search(int l, int r, Predicate pred) const {
    assert(0 <= l), assert(l <= r), assert(r <= size());
    T a{};
    assert(pred(a));
    int res = l - 1;
    backward(l, r, [&](int i) {
      if (res >= l) return;
      if (T na = tree[i] * a; pred(na)) {
        a = na;
        return;
      }
      while (i < size())
        if (T na = tree[i = 2 * i + 1] * a; pred(na)) a = na, --i;
      res = i - size();
    });
    return res;
  }

 private:
  std::vector<T> tree;

  void pull(int i) { tree[i] = tree[2 * i] * tree[2 * i + 1]; }
};

#pragma region my_template

struct Rep {
  struct I {
    int i;
    void operator++() { ++i; }
    int operator*() const { return i; }
    bool operator!=(I o) const { return i < *o; }
  };
  const int l, r;
  Rep(int _l, int _r) : l(_l), r(_r) {}
  Rep(int n) : Rep(0, n) {}
  I begin() const { return {l}; }
  I end() const { return {r}; }
};
struct Per {
  struct I {
    int i;
    void operator++() { --i; }
    int operator*() const { return i; }
    bool operator!=(I o) const { return i > *o; }
  };
  const int l, r;
  Per(int _l, int _r) : l(_l), r(_r) {}
  Per(int n) : Per(0, n) {}
  I begin() const { return {r - 1}; }
  I end() const { return {l - 1}; }
};

template <class F>
struct Fix : private F {
  Fix(F f) : F(f) {}
  template <class... Args>
  decltype(auto) operator()(Args&&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <class T = int>
T scan() {
  T res;
  std::cin >> res;
  return res;
}

template <class T, class U = T>
bool chmin(T& a, U&& b) {
  return b < a ? a = std::forward<U>(b), true : false;
}
template <class T, class U = T>
bool chmax(T& a, U&& b) {
  return a < b ? a = std::forward<U>(b), true : false;
}

#ifndef LOCAL
#define DUMP(...) void(0)
template <int OnlineJudge, int Local>
constexpr int OjLocal = OnlineJudge;
#endif

using namespace std;

#define ALL(c) begin(c), end(c)

#pragma endregion

int w;

struct Node {
  array<int, 10> to{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  friend Node operator*(const Node& a, const Node& b) {
    Node c;
    for (int j : Rep(10))
      c.to[j] = 0 <= b.to[j] and b.to[j] < w ? a.to[b.to[j]] : b.to[j];
    return c;
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int h, q;
  h = scan(), w = scan(), q = scan();

  auto encode = [&](const string& s) -> int {
    assert(int(size(s)) == w);
    int res = 0;
    for (char c : s) {
      auto pos = string{"<^>"}.find(c);
      assert(pos != string::npos);
      res = res * 3 + pos;
    }
    return res;
  };

  vector<Node> tab(power(3, w));
  {
    string s;
    Fix([&](auto self) -> void {
      if (int(size(s)) == w) {
        auto&& e = tab[encode(s)].to;
        for (int j : Rep(w)) {
          int cur = j;
          bool ok = false;
          for (int _ = 20; _--;) {
            if (0 <= cur and cur < w) {
              if (s[cur] == '<') {
                --cur;
              } else if (s[cur] == '>') {
                ++cur;
              } else if (s[cur] == '^') {
                ok = true;
                break;
              } else
                assert(false);
            } else {
              ok = true;
              break;
            }
          }
          e[j] = ok ? cur : -2;
        }
        return;
      }
      for (char c : string{"<^>"}) {
        s += c;
        self();
        s.pop_back();
      }
    })();
  }

  vector<string> s(h);
  generate(ALL(s), scan<string>);
  segment_tree<Node> seg(h, [&](int i) { return tab[encode(s[i])]; });
  for (int r : Rep(1, h + 1))
    for (int l : Rep(r)) DUMP(l, r, seg.fold(l, r).to);
  while (q--) {
    if (scan<char>() == 'A') {
      int i = scan() - 1, j = scan() - 1;
      int l = seg.backward_search(0, i + 1, [&](const Node& e) {
        return 0 <= e.to[j] and e.to[j] < w;
      });
      // DUMP(i, j, w, l);
      // for (int l : Rep(i + 1)) {
      //   DUMP(l, seg.fold(l, i + 1).to[j]);
      // }
      // exit(0);
      if (l != -1 and seg.fold(l, i + 1).to[j] == -2) {
        cout << "-1 -1\n";
      } else if (l == -1) {
        cout << "0 " << seg.fold(0, i + 1).to[j] + 1 << '\n';
      } else {
        cout << l + 1 << ' ' << seg.fold(l, i + 1).to[j] + 1 << '\n';
      }
    } else {
      int i = scan() - 1, j = scan() - 1;
      s[i][j] = scan<char>();
      seg.update(i, [&](const Node&) { return tab[encode(s[i])]; });
    }
  }
}


#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#include <tuple>

void re(int& x);

template<class T, class... Ts> void re(T& t, Ts&... ts);

void pr(int x);

void pr(const char *x);

void ps();
template<class T, class... Ts> void ps(const T& t, const Ts&... ts);

template<typename T, typename U> struct seg_tree_lazy {
    int S, H;

    T zero;
    std::vector<T> value;

    U noop;
    std::vector<bool> dirty;
    std::vector<U> prop;

    seg_tree_lazy<T, U>(int _S, T _zero = T(), U _noop = U());
    void set_leaves(std::vector<T> &leaves);
    void apply(int i, U &update);
    void rebuild(int i);
    void propagate(int i);
    void upd(int i, int j, U update);
    T query(int i, int j);
};

using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)

char belts[110000][12];
int n,m,q;

void compute(int* to, int row) {
    for (int i = 0; i < m; i++) {
        if (belts[row][i] == '>') {
            for (int j = i+1; j < m; j++) {
                if (belts[row][j] == '<') {
                    to[i] = 10;
                    break;
                }
                else if (belts[row][j] == '^') {
                    to[i] = j;
                    break;
                }
                else if (j == m-1) {
                    to[i] = 12;
                    break;
                }
            }
        }

        else if (belts[row][i] == '<') {
            if (i == 0) to[i] = 11;
            else to[i] = to[i-1];
        }

        else to[i] = i;
    }
}

struct node {
    int loc;
    int to[13];

    node() { rep(i,13) to[i]=i; }

    node operator+(node ot) {
        node ret;
        for (int i = 0; i < 13; i++) {
            ret.to[i] = ot.to[to[i]];
        }
        return ret;
    }
};

struct update {
    bool d;
    update() : d(false) {}
    node operator()(node ot) {
        if (!d) return ot;

        node ret;
        ret.loc = ot.loc;
        compute(ret.to, ret.loc);
        return ret;
    }

    update operator+(update u) {
        u.d |= d;
        return u;
    }
};

void solve()
{
    re(n,m,q);
    for (int i = 0; i < n; i++) scanf("%s", belts[i]);

    vector<node> leaves(n);
    for (int i = 0; i < n; i++) {
        leaves[i].loc = n-i-1;
        compute(leaves[i].to, n-i-1);
    }

    seg_tree_lazy<node, update> seg(n);
    seg.set_leaves(leaves);

    rep(qq,q) {
        char op;
        scanf(" %c", &op);

        if (op == 'A') {
            int x, y;
            re(x,y); x--; y--;
            node to = seg.query(n-x-1, n-1);

            if (to.to[y] == 10) {
                ps(-1,-1);
            }
            else if (to.to[y] < 10) {
                ps(0, to.to[y]+1);
            }
            else {
                int st = 0, ed = x;
                while (st < ed) {
                    int md = (st+ed)/2;
                    node to2 = seg.query(n-x-1, n-md-1);
                    if (to2.to[y] > 10) ed = md;
                    else st = md+1;
                }

                if (to.to[y] == 11) ps(st+1, 0);
                else ps(st+1, m+1);
            }
        }

        else {
            int x, y; char c;
            scanf("%d %d %c", &x, &y, &c); x--; y--;
            belts[x][y] = c;
            update u; u.d = 1;
            seg.upd(n-x-1, n-x-1, u);
        }
    }
}

int main() {

    solve();
}

template<typename T, typename U>
seg_tree_lazy<T, U>::seg_tree_lazy(int _S, T _zero, U _noop) {
        zero = _zero, noop = _noop;
        for (S = 1, H = 1; S < _S; ) S *= 2, H++;

        value.resize(2*S, zero);
        dirty.resize(2*S, false);
        prop.resize(2*S, noop);
    }

 template<typename T, typename U>
    void seg_tree_lazy<T, U>::set_leaves(vector<T> &leaves) {
        copy(leaves.begin(), leaves.end(), value.begin() + S);

        for (int i = S - 1; i > 0; i--)
            value[i] = value[2 * i] + value[2 * i + 1];
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::apply(int i, U &update) {
        value[i] = update(value[i]);
        if(i < S) {
            prop[i] = prop[i] + update;
            dirty[i] = true;
        }
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::rebuild(int i) {
        for (int l = i/2; l; l /= 2) {
            T combined = value[2*l] + value[2*l+1];
            value[l] = prop[l](combined);
        }
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::propagate(int i) {
        for (int h = H; h > 0; h--) {
            int l = i >> h;

            if (dirty[l]) {
                apply(2*l, prop[l]);
                apply(2*l+1, prop[l]);

                prop[l] = noop;
                dirty[l] = false;
            }
        }
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::upd(int i, int j, U update) {
        i += S, j += S;
        propagate(i), propagate(j);

        for (int l = i, r = j; l <= r; l /= 2, r /= 2) {
            if((l&1) == 1) apply(l++, update);
            if((r&1) == 0) apply(r--, update);
        }

        rebuild(i), rebuild(j);
    }

  template<typename T, typename U>
    T seg_tree_lazy<T, U>::query(int i, int j){
        i += S, j += S;
        propagate(i), propagate(j);

        T res_left = zero, res_right = zero;
        for(; i <= j; i /= 2, j /= 2){
            if((i&1) == 1) res_left = res_left + value[i++];
            if((j&1) == 0) res_right = value[j--] + res_right;
        }
        return res_left + res_right;
    }

void re(int& x) { scanf("%d", &x); }

template<class T, class... Ts> void re(T& t, Ts&... ts) {
    re(t); re(ts...);
}

void pr(int x) { printf("%d", x); }

void pr(const char *x) { printf("%s", x); }

void ps() { pr("\n"); }
template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
    pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
}


#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#include <tuple>

void re(int& x);

template<class T, class... Ts> void re(T& t, Ts&... ts);

void pr(int x);

void pr(const char *x);

void ps();
template<class T, class... Ts> void ps(const T& t, const Ts&... ts);

template<typename T, typename U> struct seg_tree_lazy {
    int S, H;

    T zero;
    std::vector<T> value;

    U noop;
    std::vector<bool> dirty;
    std::vector<U> prop;

    seg_tree_lazy<T, U>(int _S, T _zero = T(), U _noop = U());
    void set_leaves(std::vector<T> &leaves);
    void apply(int i, U &update);
    void rebuild(int i);
    void propagate(int i);
    void upd(int i, int j, U update);
    T query(int i, int j);
};

using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)

char belts[110000][12];
int n,m,q;

void compute(int* to, int row) {
    for (int i = 0; i < m; i++) {
        if (belts[row][i] == '>') {
            for (int j = i; j < m; j++) {
                if (belts[row][j] == '<') {
                    to[i] = 10;
                    break;
                }
                else if (belts[row][j] == '^') {
                    to[i] = j;
                    break;
                }
                else if (j == m-1) {
                    to[i] = 12;
                    break;
                }
            }
        }

        else if (belts[row][i] == '<') {
            if (i == 0) to[i] = 11;
            else to[i] = to[i-1];
        }

        else to[i] = i;
    }
}

struct node {
    int loc;
    int to[13];

    node() { rep(i,13) to[i]=i; }

    node operator+(node ot) {
        node ret;
        for (int i = 0; i < 13; i++) {
            ret.to[i] = ot.to[to[i]];
        }
        return ret;
    }
};

struct update {
    bool d;
    update() : d(false) {}
    node operator()(node ot) {
        if (!d) return ot;

        node ret;
        ret.loc = ot.loc;
        compute(ret.to, ret.loc);
        return ret;
    }

    update operator+(update u) {
        u.d |= d;
        return u;
    }
};

void solve()
{
    re(n,m,q);
    for (int i = 0; i < n; i++) scanf("%s", belts[i]);

    vector<node> leaves(n);
    for (int i = 0; i < n; i++) {
        leaves[i].loc = n-i-1;
        compute(leaves[i].to, n-i-1);
    }

    seg_tree_lazy<node, update> seg(n);
    seg.set_leaves(leaves);

    rep(qq,q) {
        char op;
        scanf(" %c", &op);

        if (op == 'A') {
            int x, y;
            re(x,y); x--; y--;
            node to = seg.query(n-x-1, n-1);

            if (to.to[y] == 10) {
                ps(-1,-1);
            }
            else if (to.to[y] < 10) {
                ps(0, to.to[y]+1);
            }
            else {
                int st = 0, ed = x;
                while (st < ed) {
                    int md = (st+ed)/2;
                    node to2 = seg.query(n-x-1, n-md-1);
                    if (to2.to[y] > 10) ed = md;
                    else st = md+1;
                }

                if (to.to[y] == 11) ps(st+1, 0);
                else ps(st+1, m+1);
            }
        }

        else {
            int x, y; char c;
            scanf("%d %d %c", &x, &y, &c); x--; y--;
            belts[x][y] = c;
            update u; u.d = 1;
            seg.upd(n-x-1, n-x-1, u);
        }
    }
}

int main() {

    solve();
}

template<typename T, typename U>
seg_tree_lazy<T, U>::seg_tree_lazy(int _S, T _zero, U _noop) {
        zero = _zero, noop = _noop;
        for (S = 1, H = 1; S < _S; ) S *= 2, H++;

        value.resize(2*S, zero);
        dirty.resize(2*S, false);
        prop.resize(2*S, noop);
    }

 template<typename T, typename U>
    void seg_tree_lazy<T, U>::set_leaves(vector<T> &leaves) {
        copy(leaves.begin(), leaves.end(), value.begin() + S);

        for (int i = S - 1; i > 0; i--)
            value[i] = value[2 * i] + value[2 * i + 1];
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::apply(int i, U &update) {
        value[i] = update(value[i]);
        if(i < S) {
            prop[i] = prop[i] + update;
            dirty[i] = true;
        }
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::rebuild(int i) {
        for (int l = i/2; l; l /= 2) {
            T combined = value[2*l] + value[2*l+1];
            value[l] = prop[l](combined);
        }
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::propagate(int i) {
        for (int h = H; h > 0; h--) {
            int l = i >> h;

            if (dirty[l]) {
                apply(2*l, prop[l]);
                apply(2*l+1, prop[l]);

                prop[l] = noop;
                dirty[l] = false;
            }
        }
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::upd(int i, int j, U update) {
        i += S, j += S;
        propagate(i), propagate(j);

        for (int l = i, r = j; l <= r; l /= 2, r /= 2) {
            if((l&1) == 1) apply(l++, update);
            if((r&1) == 0) apply(r--, update);
        }

        rebuild(i), rebuild(j);
    }

  template<typename T, typename U>
    T seg_tree_lazy<T, U>::query(int i, int j){
        i += S, j += S;
        propagate(i), propagate(j);

        T res_left = zero, res_right = zero;
        for(; i <= j; i /= 2, j /= 2){
            if((i&1) == 1) res_left = res_left + value[i++];
            if((j&1) == 0) res_right = value[j--] + res_right;
        }
        return res_left + res_right;
    }

void re(int& x) { scanf("%d", &x); }

template<class T, class... Ts> void re(T& t, Ts&... ts) {
    re(t); re(ts...);
}

void pr(int x) { printf("%d", x); }

void pr(const char *x) { printf("%s", x); }

void ps() { pr("\n"); }
template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
    pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
}


#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#include <tuple>

void re(int& x);

template<class T, class... Ts> void re(T& t, Ts&... ts);

void pr(int x);

void pr(const char *x);

void ps();
template<class T, class... Ts> void ps(const T& t, const Ts&... ts);

template<typename T, typename U> struct seg_tree_lazy {
    int S, H;

    T zero;
    std::vector<T> value;

    U noop;
    std::vector<bool> dirty;
    std::vector<U> prop;

    seg_tree_lazy<T, U>(int _S, T _zero = T(), U _noop = U());
    void set_leaves(std::vector<T> &leaves);
    void apply(int i, U &update);
    void rebuild(int i);
    void propagate(int i);
    void upd(int i, int j, U update);
    T query(int i, int j);
};

using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)

char belts[110000][12];
int n,m,q;

void compute(int* to, int row) {
    for (int i = 0; i < m; i++) {
        if (belts[row][i] == '>') {
            for (int j = i; j < m; j++) {
                if (belts[row][j] == '<') {
                    to[i] = 10;
                    break;
                }
                else if (belts[row][j] == '^') {
                    to[i] = j;
                    break;
                }
                else if (j == m-1) {
                    to[i] = 12;
                    break;
                }
            }
        }

        else if (belts[row][i] == '<') {
            if (i == 0) to[i] = 11;
            else to[i] = to[i-1];
        }

        else to[i] = i;
    }
}

struct node {
    int loc;
    int to[13];

    node() { rep(i,13) to[i]=i; }

    node operator+(node ot) {
        node ret;
        for (int i = 0; i < 13; i++) {
            ret.to[i] = ot.to[to[i]];
        }
        return ret;
    }
};

struct update {
    bool d;
    update() : d(false) {}
    node operator()(node ot) {
        if (!d) return ot;

        node ret;
        ret.loc = ot.loc;
        compute(ret.to, ret.loc);
        return ret;
    }

    update operator+(update u) {
        u.d |= d;
        return u;
    }
};

void solve()
{
    re(n,m,q);
    for (int i = 0; i < n; i++) scanf("%s", belts[i]);

    vector<node> leaves(n);
    for (int i = 0; i < n; i++) {
        leaves[i].loc = n-i-1;
        compute(leaves[i].to, n-i-1);
    }

    seg_tree_lazy<node, update> seg(n);
    seg.set_leaves(leaves);

    rep(qq,q) {
        char op;
        scanf(" %c", &op);

        if (op == 'A') {
            int x, y;
            re(x,y); x--; y--;
            node to = seg.query(n-x-1, n-1);

            //printf("toto %d\n", to.to[y]);

            if (to.to[y] == 10) {
                ps(-1,-1);
            }
            else if (to.to[y] < 10) {
                ps(0, to.to[y]+1);
            }
            else {
                int st = 0, ed = x;
                while (st < ed) {
                    int md = (st+ed+1)/2;
                    node to2 = seg.query(n-x-1, n-md-1);
                    if (to2.to[y] > 10) st = md;
                    else ed = md-1;
                }

                if (to.to[y] == 11) ps(st+1, 0);
                else ps(st+1, m+1);
            }
        }

        else {
            int x, y; char c;
            scanf("%d %d %c", &x, &y, &c); x--; y--;
            belts[x][y] = c;
            update u; u.d = 1;
            seg.upd(n-x-1, n-x-1, u);
        }
    }
}

int main() {

    solve();
}

template<typename T, typename U>
seg_tree_lazy<T, U>::seg_tree_lazy(int _S, T _zero, U _noop) {
        zero = _zero, noop = _noop;
        for (S = 1, H = 1; S < _S; ) S *= 2, H++;

        value.resize(2*S, zero);
        dirty.resize(2*S, false);
        prop.resize(2*S, noop);
    }

 template<typename T, typename U>
    void seg_tree_lazy<T, U>::set_leaves(vector<T> &leaves) {
        copy(leaves.begin(), leaves.end(), value.begin() + S);

        for (int i = S - 1; i > 0; i--)
            value[i] = value[2 * i] + value[2 * i + 1];
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::apply(int i, U &update) {
        value[i] = update(value[i]);
        if(i < S) {
            prop[i] = prop[i] + update;
            dirty[i] = true;
        }
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::rebuild(int i) {
        for (int l = i/2; l; l /= 2) {
            T combined = value[2*l] + value[2*l+1];
            value[l] = prop[l](combined);
        }
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::propagate(int i) {
        for (int h = H; h > 0; h--) {
            int l = i >> h;

            if (dirty[l]) {
                apply(2*l, prop[l]);
                apply(2*l+1, prop[l]);

                prop[l] = noop;
                dirty[l] = false;
            }
        }
    }

  template<typename T, typename U>
    void seg_tree_lazy<T, U>::upd(int i, int j, U update) {
        i += S, j += S;
        propagate(i), propagate(j);

        for (int l = i, r = j; l <= r; l /= 2, r /= 2) {
            if((l&1) == 1) apply(l++, update);
            if((r&1) == 0) apply(r--, update);
        }

        rebuild(i), rebuild(j);
    }

  template<typename T, typename U>
    T seg_tree_lazy<T, U>::query(int i, int j){
        i += S, j += S;
        propagate(i), propagate(j);

        T res_left = zero, res_right = zero;
        for(; i <= j; i /= 2, j /= 2){
            if((i&1) == 1) res_left = res_left + value[i++];
            if((j&1) == 0) res_right = value[j--] + res_right;
        }
        return res_left + res_right;
    }

void re(int& x) { scanf("%d", &x); }

template<class T, class... Ts> void re(T& t, Ts&... ts) {
    re(t); re(ts...);
}

void pr(int x) { printf("%d", x); }

void pr(const char *x) { printf("%s", x); }

void ps() { pr("\n"); }
template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
    pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
}


#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 10, sq = 1000;
int n, m, q;
char c[MAX_N][10 +10];
pair<int, int> ans[MAX_N][10 + 10];
void upd(int x){
	for(int k = x; k < x + sq; k++){
		for(int i = 1; i <= m; i++){
			ans[k][i] = {0, 0};
		}
	}
	for(int k = x; k < x + sq; k++){
		for(int i = 1; i <= m; i++){
			if(c[k][i] == '<'){
				ans[k][i] = ans[k][i - 1];
			}
			else{
				break;
			}
		}
		for(int i = m; i >= 1; i--){
			if(c[k][i] == '>'){
				ans[k][i] = ans[k][i + 1];
			}
			else{
				break;
			}
		}
		for(int i = 1; i <= m; i++){
			if(c[k][i] == '^'){
				if(k == x){
					ans[x][i] = {x - 1, i};
				}
				else{
					ans[k][i] = ans[k - 1][i];
				}
				for(int j = i + 1; i <= m; j++){
					if(c[k][i] == '<'){
						ans[k][i] = ans[k][i - 1];
					}
					else{
						break;
					}
				}
				for(int j = i - 1; j >= 1; j--){
					if(c[k][i] == '>'){
						ans[k][i] = ans[k][i + 1];
					}
					else{
						break;
					}
				}
			}
		}
	}
}
pair<int, int> get(int x, int y){
	if(ans[x][y].first != 0 && ans[x][y].second != 0 && ans[x][y].second != m + 1){
		return get(ans[x][y].first, ans[x][y].second);
	}
	return ans[x][y];
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>q;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin>>c[i][j];
		}
	}
	for(int i = 1; i <= m; i++){
		ans[0][i] = {0, i};
	}
	for(int i = 1; i <= n; i++){
		ans[i][0] = {i, 0};
		ans[i][m + 1] = {i, m + 1};
	}
	for(int i = 1; i <= n; i += sq){
		upd(i);
	}
	while(q--){
		char no;
		int x, y;
		cin>>no;
		if(no == 'A'){
			cin>>x>>y;
			pair<int, int> a = get(x, y);
			if(a.first == 0 && a.second == 0){
				cout<<-1<<" "<<-1<<endl;
			}
			else{
				cout<<a.first<<" "<<a.second<<endl;
			}
		}
		if(no == 'C'){
			cin>>x>>y>>no;
			c[x][y] = no;
			upd(((x - 1) / sq) * sq + 1);
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 10, sq = 1000;
int n, m, q;
char c[MAX_N][10 +10];
pair<int, int> ans[MAX_N][10 + 10];
void upd(int x){
	for(int k = x; k < x + sq; k++){
		for(int i = 1; i <= m; i++){
			ans[k][i] = {0, 0};
		}
	}
	for(int k = x; k < x + sq; k++){
		for(int i = 1; i <= m; i++){
			if(c[k][i] == '<'){
				ans[k][i] = ans[k][i - 1];
			}
			else{
				break;
			}
		}
		for(int i = m; i >= 1; i--){
			if(c[k][i] == '>'){
				ans[k][i] = ans[k][i + 1];
			}
			else{
				break;
			}
		}
		for(int i = 1; i <= m; i++){
			if(c[k][i] == '^'){
				if(k == x){
					ans[x][i] = {x - 1, i};
				}
				else{
					ans[k][i] = ans[k - 1][i];
				}
				for(int j = i + 1; i <= m; j++){
					if(c[k][j] == '<'){
						ans[k][j] = ans[k][j - 1];
					}
					else{
						break;
					}
				}
				for(int j = i - 1; j >= 1; j--){
					if(c[k][j] == '>'){
						ans[k][j] = ans[k][j + 1];
					}
					else{
						break;
					}
				}
			}
		}
	}
}
pair<int, int> get(int x, int y){
	if(ans[x][y].first != 0 && ans[x][y].second != 0 && ans[x][y].second != m + 1){
		return get(ans[x][y].first, ans[x][y].second);
	}
	return ans[x][y];
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>q;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin>>c[i][j];
		}
	}
	for(int i = 1; i <= m; i++){
		ans[0][i] = {0, i};
	}
	for(int i = 1; i <= n; i++){
		ans[i][0] = {i, 0};
		ans[i][m + 1] = {i, m + 1};
	}
	for(int i = 1; i <= n; i += sq){
		upd(i);
	}
	while(q--){
		char no;
		int x, y;
		cin>>no;
		if(no == 'A'){
			cin>>x>>y;
			pair<int, int> a = get(x, y);
			if(a.first == 0 && a.second == 0){
				cout<<-1<<" "<<-1<<endl;
			}
			else{
				cout<<a.first<<" "<<a.second<<endl;
			}
		}
		if(no == 'C'){
			cin>>x>>y>>no;
			c[x][y] = no;
			upd(((x - 1) / sq) * sq + 1);
		}
	}
}


#include <bits/stdc++.h>
#define DIM 100007
#define INF 1000000007
#define LEN 317LL
using namespace std;

long long n,m,q;
char c[DIM][11];
pair<int,int> next[DIM][11];

int st,fn;
pair<int,int> dfs(int row, int col)
{
    if(row < st || row > fn || col < 1 || col > m)
         return {row,col};

    if(next[row][col]!=(pair<int,int>){0,0}) return next[row][col];
    next[row][col] = {-1,-1};
    if(c[row][col] == '>')
        next[row][col] = dfs(row,col+1);
    else if(c[row][col] == '<')
        next[row][col] = dfs(row,col-1);
    else
        next[row][col] = dfs(row-1,col);
    return next[row][col];
}

void update(int num)
{
    st = (num-1)*LEN+1;
    fn = min(n,st+LEN-1);
    for(int i = st; i <= fn; ++i)
        for(int j = 1; j <= m; ++j)
            next[i][j] = {0,0};

    for(int i = st; i <= fn; ++i)
        for(int j = 1; j <= m; ++j)
            if(next[i][j] == (pair<int,int>){0,0})
                dfs(i,j);

}

pair<int,int> query(int row, int col)
{
    if(next[row][col].second>0 && next[row][col].second <= m
       && next[row][col].first>0 && next[row][col].first <= n
        && next[row][col]!=(pair<int,int>){-1,-1}) return query(next[row][col].first,next[row][col].second);
    return next[row][col];
}
int main()
{
    cin>>n>>m>>q;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            cin>>c[i][j];

    long long nblock = n/LEN + (n%LEN!=0);

    for(int i = 1; i <= nblock; ++i)
        update(i);

    /*for(int i = 1; i <= n; ++i)
        {for(int j = 1; j <= m; ++j)
        cout<<next[i][j].first<<' '<<next[i][j].second<<"      ";
    cout<<endl;}*/

    for(int i = 1; i <= q; ++i)
    {
        char op;
        int r, col;
        cin>>op>>r>>col;
        if(op == 'A')
        {
            pair<int,int> p = query(r,col);
            cout<<p.first<<' '<<p.second<<endl;
        }
        else
        {
            char nw;
            cin>>nw;
            c[r][col] = nw;
            update((r-1)/LEN+1);
        }
    }

    return 0;
}