#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]!=make_pair(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] == make_pair(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]!=make_pair(-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;
}


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

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

pair<int,int> p0 = make_pair(0,0), p1 = make_pair(-1,-1);

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]!=p0) 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] == p0)
                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]!=p1) 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;
}


#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];

pair<int,int> p0 = make_pair(0,0), p1 = make_pair(-1,-1);

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]!=p0) 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] == p0)
                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]!=p1) 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;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define ss(x) (int) x.size()
#define FOR(i, j, n) for(int i = j; i <= n; ++i)
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl;
#define ios cin.tie(0); ios_base::sync_with_stdio(0)

using namespace std;

const int nax = 1e5 + 111;
const int P = 400;

int n, m, q, a, b;
char s[nax][12];
int t[nax][12];
int L[nax][12];
int R[nax][12];
int sum[nax][12][2];
int id[nax];
int Left[nax];
int Right[nax];
pair<int, int> go[nax][12];

map <char, int> mapa;

//////// 0 - lewo , 1 - prawo, 2 - gora

bool bad(int r, int w) {
	if(r <= 0)
		return 1;
	if(w == 0 || w == m + 1)
		return 1;
	return 0;
}

int daj(int row, int l, int r, int type) {
	int res = sum[row][r][type];
	if(l)
		res -= sum[row][l - 1][type];
	return res;
}

void rebuild(int block) {
	for(int i = Left[block]; i <= Right[block]; ++i) {

		int Last = 0;
		for(int j = 0; j <= m + 1; ++j) {
			L[i][j] = Last;
			if(j) {
				for(int g = 0; g < 2; ++g)
					sum[i][j][g] = sum[i][j - 1][g];
			}
			if(t[i][j] <= 1)
				sum[i][j][t[i][j]] += 1;
			else
				Last = j;
		}

		Last = m + 1;
		for(int j = m + 1; 0 <= j; --j) {
			R[i][j] = Last;
			if(t[i][j] == 2)
				Last = j;
		}

		go[i][0] = mp(i, 0);
		go[i][m + 1] = mp(i, m + 1);

		for(int j = 1; j <= m; ++j) {
			if(t[i][j] == 2) {
				if(i == Left[block])
					go[i][j] = mp(i - 1, j);
				else
					go[i][j] = go[i - 1][j];
			}
			if(t[i][j] == 0) {
				int e = L[i][j];
				if(daj(i, e, j, 1) > 0)
					go[i][j] = mp(-1, -1);
				else
					go[i][j] = go[i][e];
			}
			if(t[i][j] == 1) {
				int e = R[i][j];
				if(daj(i, j, e, 0) > 0)
					go[i][j] = mp(-1, -1);
				else {
					if(i == Left[block] && e <= m)
						go[i][j] = mp(i - 1, e);
					else if(e != m + 1)
						go[i][j] = go[i - 1][e];
					else
						go[i][j] = mp(i, m + 1);
				}
			}
		}
	}
}

void solve(int row, int col) {
	while(!bad(row, col)) {
		int rr = go[row][col].fi;
		int cc = go[row][col].se;
		row = rr;
		col = cc;
	}
	printf("%d %d\n", row, col);
}

int main() {
	mapa['<'] = 0;
	mapa['>'] = 1;
	mapa['^'] = 2;
	scanf("%d %d %d", &n, &m, &q);
	FOR(i, 0, n / P) {
		Left[i] = n + 1;
		Right[i] = -1;
	}
	FOR(i, 1, n) {
		t[i][0] = t[i][m + 1] = 2;
		scanf("%s", s[i] + 1);
		FOR(j, 1, m)
			t[i][j] = mapa[s[i][j]];
		id[i] = i / P;
		Left[id[i]] = min(Left[id[i]], i);
		Right[id[i]] = max(Right[id[i]], i);
	}

	FOR(i, 0, n / P)
		rebuild(i);

	while(q--) {
		char c;
		scanf(" %c", &c);
		scanf("%d %d", &a, &b);
		if(c == 'A')
			solve(a, b);
		else {
			scanf(" %c", &c);
			t[a][b] = mapa[c];
			rebuild(a / P);
		}
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
vector<string> grid;
vector<vii> jump;
const int Size = 2;
int n,m;
void dfs(int r,int c)
{
	if(grid[r][c]=='<')
	{
		if(c==0)		jump[r][c] = ii(r,c);
		else
		{
			if(grid[r][c-1]=='>')	jump[r][c] = ii(r,c-1);
			else					jump[r][c] = jump[r][c-1];
		}
		return;
	}
	if(grid[r][c]=='^')
	{
		if(r%Size==0)	jump[r][c] = ii(r,c);
		else			jump[r][c] = jump[r-1][c];
		return;
	}
	if(c==m-1)
	{
		jump[r][c] = ii(r,c);
		return;
	}
	dfs(r,c+1);
	if(grid[r][c+1]=='<')	jump[r][c] = ii(r,c+1);
	else					jump[r][c] = jump[r][c+1];
}
int main()
{
	string s;
	int q,i,j,k,r,c,x,y;
	scanf("%d %d %d",&n,&m,&q);
	jump.assign(n,vii(m,{0,0}));
	for(i=0;i<n;++i)
	{
		cin >> s;
		grid.push_back(s);
	}
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			dfs(i,j);
	while(q--)
	{
		char type;
		cin >> type >> x >> y;
		x--;y--;
		if(type=='A')
		{
			int update = Size * m;
			while(x>=0&&y>=0&&x<n&&y<m&&update>=0)
			{
				r = jump[x][y].first;
				c = jump[x][y].second;
				if(r==x&&c==y)
				{
					if(grid[x][y]=='>')				y++;
					else if(grid[x][y]=='^')		x--;
					else if(grid[x][y]=='<')		y--;
				}
				else
				{
					x = r;
					y = c;
				}
				update--;
			}
			if(x>=0&&y>=0&&x<n&&y<m)	printf("-1 -1\n");
			else						printf("%d %d\n",x+1,y+1);
		}
		if(type=='C')
		{
			cin >> type;
			grid[x][y]=type;
			for(j=0;j<m;++j)		dfs(x,j);
			x++;
			while(x<n&&x%Size!=0)
			{
				for(j=0;j<m;++j)
					dfs(x,j);
				x++;
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
vector<string> grid;
vector<vii> jump;
const int size = 318;
int n,m;
void dfs(int r,int c)
{
	if(c==m-1)
	{
		jump[r][c] = ii(r,c);
		return;
	}
	if(grid[r][c]=='<')
	{
		if(c==0)		jump[r][c] = ii(r,c);
		else
		{
			if(grid[r][c-1]=='>')	jump[r][c] = ii(r,c-1);
			else					jump[r][c] = jump[r][c-1];
		}
		return;
	}
	if(grid[r][c]=='^')
	{
		if(r%size==0)	jump[r][c] = ii(r,c);
		else			jump[r][c] = jump[r-1][c];
		return;
	}
	dfs(r,c+1);
	if(grid[r][c+1]=='<')	jump[r][c] = ii(r,c+1);
	else					jump[r][c] = jump[r][c+1];
}
int main()
{
	string s;
	int q,i,j,k,r,c,x,y;
	scanf("%d %d %d",&n,&m,&q);
	jump.assign(n,vii(m,{0,0}));
	for(i=0;i<n;++i)
	{
		cin >> s;
		grid.push_back(s);
	}
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			dfs(i,j);
	while(q--)
	{
		char type;
		cin >> type >> x >> y;
		x--;y--;
		if(type=='A')
		{
			int update = size * m;
			while(x>=0&&y>=0&&x<n&&y<m&&update>=0)
			{
				r = jump[x][y].first;
				c = jump[x][y].second;
				if(r==x&&c==y)
				{
					if(grid[x][y]=='>')				y++;
					else if(grid[x][y]=='^')		x--;
					else if(grid[x][y]=='<')		y--;
				}
				else
				{
					x = r;
					y = c;
				}
				update--;
			}
			if(x>=0&&y>=0&&x<n&&y<m)	printf("-1 -1\n");
			else						printf("%d %d\n",x+1,y+1);
		}
		if(type=='C')
		{
			cin >> type;
			grid[x][y]=type;
			for(j=0;j<m;++j)		dfs(x,j);
			x++;
			while(x<n&&x%size!=0)
			{
				for(j=0;j<m;++j)
					dfs(x,j);
				x++;
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
vector<string> grid;
vector<vii> jump;
const int Size = 318;
int n,m;
void dfs(int r,int c)
{
	if(grid[r][c]=='<')
	{
		if(c==0)		jump[r][c] = ii(r,c);
		else
		{
			if(grid[r][c-1]=='>')	jump[r][c] = ii(r,c-1);
			else					jump[r][c] = jump[r][c-1];
		}
		return;
	}
	if(grid[r][c]=='^')
	{
		if(r%Size==0)	jump[r][c] = ii(r,c);
		else			jump[r][c] = jump[r-1][c];
		return;
	}
	if(c==m-1)
	{
		jump[r][c] = ii(r,c);
		return;
	}
	dfs(r,c+1);
	if(grid[r][c+1]=='<')	jump[r][c] = ii(r,c+1);
	else					jump[r][c] = jump[r][c+1];
}
int main()
{
	string s;
	int q,i,j,k,r,c,x,y;
	scanf("%d %d %d",&n,&m,&q);
	jump.assign(n,vii(m,{0,0}));
	for(i=0;i<n;++i)
	{
		cin >> s;
		grid.push_back(s);
	}
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			dfs(i,j);
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			cout << jump[i][j].first << " "  << jump[i][j].second << " ";
		}
		printf("\n");
	}
	while(q--)
	{
		char type;
		cin >> type >> x >> y;
		x--;y--;
		if(type=='A')
		{
			int update = Size * m;
			while(x>=0&&y>=0&&x<n&&y<m&&update>=0)
			{
				r = jump[x][y].first;
				c = jump[x][y].second;
				if(r==x&&c==y)
				{
					if(grid[x][y]=='>')				y++;
					else if(grid[x][y]=='^')		x--;
					else if(grid[x][y]=='<')		y--;
				}
				else
				{
					x = r;
					y = c;
				}
				update--;
			}
			if(x>=0&&y>=0&&x<n&&y<m)	printf("-1 -1\n");
			else						printf("%d %d\n",x+1,y+1);
		}
		if(type=='C')
		{
			cin >> type;
			grid[x][y]=type;
			for(j=0;j<m;++j)		dfs(x,j);
			x++;
			while(x<n&&x%Size!=0)
			{
				for(j=0;j<m;++j)
					dfs(x,j);
				x++;
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
vector<string> grid;
vector<vii> jump;
const int Size = 318;
int n,m;
void dfs(int r,int c)
{
	if(c==m-1)
	{
		jump[r][c] = ii(r,c);
		return;
	}
	if(grid[r][c]=='<')
	{
		if(c==0)		jump[r][c] = ii(r,c);
		else
		{
			if(grid[r][c-1]=='>')	jump[r][c] = ii(r,c-1);
			else					jump[r][c] = jump[r][c-1];
		}
		return;
	}
	if(grid[r][c]=='^')
	{
		if(r%Size==0)	jump[r][c] = ii(r,c);
		else			jump[r][c] = jump[r-1][c];
		return;
	}
	dfs(r,c+1);
	if(grid[r][c+1]=='<')	jump[r][c] = ii(r,c+1);
	else					jump[r][c] = jump[r][c+1];
}
int main()
{
	string s;
	int q,i,j,k,r,c,x,y;
	scanf("%d %d %d",&n,&m,&q);
	jump.assign(n,vii(m,{0,0}));
	for(i=0;i<n;++i)
	{
		cin >> s;
		grid.push_back(s);
	}
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			dfs(i,j);
	while(q--)
	{
		char type;
		cin >> type >> x >> y;
		x--;y--;
		if(type=='A')
		{
			int update = Size * m;
			while(x>=0&&y>=0&&x<n&&y<m&&update>=0)
			{
				r = jump[x][y].first;
				c = jump[x][y].second;
				if(r==x&&c==y)
				{
					if(grid[x][y]=='>')				y++;
					else if(grid[x][y]=='^')		x--;
					else if(grid[x][y]=='<')		y--;
				}
				else
				{
					x = r;
					y = c;
				}
				update--;
			}
			if(x>=0&&y>=0&&x<n&&y<m)	printf("-1 -1\n");
			else						printf("%d %d\n",x+1,y+1);
		}
		if(type=='C')
		{
			cin >> type;
			grid[x][y]=type;
			for(j=0;j<m;++j)		dfs(x,j);
			x++;
			while(x<n&&x%Size!=0)
			{
				for(j=0;j<m;++j)
					dfs(x,j);
				x++;
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
vector<string> grid;
vector<vii> jump;
const int Size = 2;
int n,m;
void dfs(int r,int c)
{
	if(grid[r][c]=='<')
	{
		if(c==0)		jump[r][c] = ii(r,c);
		else
		{
			if(grid[r][c-1]=='>')	jump[r][c] = ii(r,c-1);
			else					jump[r][c] = jump[r][c-1];
		}
		return;
	}
	if(c==m-1)
	{
		jump[r][c] = ii(r,c);
		return;
	}
	if(grid[r][c]=='^')
	{
		if(r%Size==0)	jump[r][c] = ii(r,c);
		else			jump[r][c] = jump[r-1][c];
		return;
	}
	dfs(r,c+1);
	if(grid[r][c+1]=='<')	jump[r][c] = ii(r,c+1);
	else					jump[r][c] = jump[r][c+1];
}
int main()
{
	string s;
	int q,i,j,k,r,c,x,y;
	scanf("%d %d %d",&n,&m,&q);
	jump.assign(n,vii(m,{0,0}));
	for(i=0;i<n;++i)
	{
		cin >> s;
		grid.push_back(s);
	}
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			dfs(i,j);
	while(q--)
	{
		char type;
		cin >> type >> x >> y;
		x--;y--;
		if(type=='A')
		{
			int update = Size * m;
			while(x>=0&&y>=0&&x<n&&y<m&&update>=0)
			{
				r = jump[x][y].first;
				c = jump[x][y].second;
				if(r==x&&c==y)
				{
					if(grid[x][y]=='>')				y++;
					else if(grid[x][y]=='^')		x--;
					else if(grid[x][y]=='<')		y--;
				}
				else
				{
					x = r;
					y = c;
				}
				update--;
			}
			if(x>=0&&y>=0&&x<n&&y<m)	printf("-1 -1\n");
			else						printf("%d %d\n",x+1,y+1);
		}
		if(type=='C')
		{
			cin >> type;
			grid[x][y]=type;
			for(j=0;j<m;++j)		dfs(x,j);
			x++;
			while(x<n&&x%Size!=0)
			{
				for(j=0;j<m;++j)
					dfs(x,j);
				x++;
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
vector<string> grid;
vector<vii> jump;
const int Size = 318;
int n,m;
void dfs(int r,int c)
{
	if(grid[r][c]=='<')
	{
		if(c==0)		jump[r][c] = ii(r,c);
		else
		{
			if(grid[r][c-1]=='>')	jump[r][c] = ii(r,c-1);
			else					jump[r][c] = jump[r][c-1];
		}
		return;
	}
	if(grid[r][c]=='^')
	{
		if(r%Size==0)	jump[r][c] = ii(r,c);
		else			jump[r][c] = jump[r-1][c];
		return;
	}
	if(c==m-1)
	{
		jump[r][c] = ii(r,c);
		return;
	}
	dfs(r,c+1);
	if(grid[r][c+1]=='<')	jump[r][c] = ii(r,c+1);
	else					jump[r][c] = jump[r][c+1];
}
int main()
{
	string s;
	int q,i,j,k,r,c,x,y;
	scanf("%d %d %d",&n,&m,&q);
	jump.assign(n,vii(m,{0,0}));
	for(i=0;i<n;++i)
	{
		cin >> s;
		grid.push_back(s);
	}
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			dfs(i,j);
/*	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			cout << jump[i][j].first << " "  << jump[i][j].second << " ";
		}
		printf("\n");
	}		*/
	while(q--)
	{
		char type;
		cin >> type >> x >> y;
		x--;y--;
		if(type=='A')
		{
			int update = Size * m;
			while(x>=0&&y>=0&&x<n&&y<m&&update>=0)
			{
				r = jump[x][y].first;
				c = jump[x][y].second;
				if(r==x&&c==y)
				{
					if(grid[x][y]=='>')				y++;
					else if(grid[x][y]=='^')		x--;
					else if(grid[x][y]=='<')		y--;
				}
				else
				{
					x = r;
					y = c;
				}
				update--;
			}
			if(x>=0&&y>=0&&x<n&&y<m)	printf("-1 -1\n");
			else						printf("%d %d\n",x+1,y+1);
		}
		if(type=='C')
		{
			cin >> type;
			grid[x][y]=type;
			for(j=0;j<m;++j)		dfs(x,j);
			x++;
			while(x<n&&x%Size!=0)
			{
				for(j=0;j<m;++j)
					dfs(x,j);
				x++;
			}
		}
	}
}


#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define ms multiset
#define pb push_back
#define pll pair<ll,ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node*, Node*>
#define uid uniform_int_distribution
#define FILE ifstream in("board.in");ofstream out("board.out");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); //uid<int> u1(5, 10); u1(rnd);

int a,b,z,G=1000;
vec<vec<char>> m;
vec<vec<pii>> p;
vec<vec<vec<pii>>> d;
vec<pii> mem;

pii iter(int x, int y, int step, int fl){
    if(fl) mem.clear();
    for(; x>=0 && y>=0 && x<a && y<b && step; step--){
        if((y+1<b && m[x][y]=='>' && m[x][y+1]=='<') || (y>0 && m[x][y]=='<' && m[x][y-1]=='>')){
            if(fl) mem.pb({-1,-1});
            return {-1, -1};
        }
        if(m[x][y]=='<') y--;
        else if(m[x][y]=='>') y++;
        else if(m[x][y]=='^') x--;
        if(fl) mem.pb({x,y});
    }
    return {x, y};
}

pii go(pii p, int k){
    for(int q=10; q>=0 && k; q--){
        if(p.F==-1 || p.S==-1 || p.F==a || p.S==b) return p;
        if((1<<q)>k) continue;
        k-=1<<q;
        p = d[q][p.F][p.S];
    }
    return p;
}

void bfs(int x, int y, int dst){
    if(dst==-1) return;
    p[x][y] = mem[dst];
    if(y>0 && m[x][y-1]=='>') bfs(x,y-1,dst-1);
    if(y+1<b && m[x][y+1]=='<') bfs(x,y+1,dst-1);
    if(x+1<a && m[x+1][y]=='^') bfs(x+1,y,dst-1);
}

bool can(pii p){
    return p.F>=0 && p.S>=0 && p.F<a && p.S<b;
}

int main() {
    cin>>a>>b>>z;
    d = vec<vec<vec<pii>>>(11, vec<vec<pii>>(a, vec<pii>(b)));
    m = vec<vec<char>>(a, vec<char>(b));
    p = vec<vec<pii>>(a, vec<pii>(b, {-5, -5}));
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) cin>>m[q][w];
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) d[0][q][w] = iter(q,w,1,0);
    for(int e=1; e<11; e++) for(int q=0; q<a; q++) for(int w=0; w<b; w++) d[e][q][w] = can(d[e-1][q][w]) ? d[e-1][d[e-1][q][w].F][d[e-1][q][w].S] : d[e-1][q][w];
    /*for(int q=0; q<a; q++){
        for(int w=0; w<b; w++) cout<<"{"<<d[0][q][w].F<<", "<<d[0][q][w].S<<"} ";
        cout<<endl;
    }
    cout<<endl;*/
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) p[q][w] = go({q,w}, G);
    /*for(int q=0; q<a; q++){
        for(int w=0; w<b; w++) cout<<"{"<<p[q][w].F<<", "<<p[q][w].S<<"} ";
        cout<<endl;
    }
    cout<<endl;*/
    for(; z; z--){
        char t; pii P; cin>>t>>P.F>>P.S; P.F--; P.S--;
        if(t=='A'){
            for(; can(P);) P = p[P.F][P.S];
            if(P.F==-1 && P.S==-1) cout<<"-1 -1\n";
            else cout<<P.F+1<<" "<<P.S+1<<"\n";
        }
        else{
            cin>>m[P.F][P.S];
            iter(P.F, P.S, G, 1);
           // for(pii j : mem) cout<<"{"<<j.F<<", "<<j.S<<"} ";
           // cout<<endl;
            for(; mem.size()<G;) mem.pb(mem.back());
           // cout<<"LL"<<endl;
            bfs(P.F, P.S, mem.size()-1);
            //cout<<"OO"<<endl;
            /*for(int q=0; q<a; q++){
                for(int w=0; w<b; w++) cout<<"{"<<p[q][w].F<<", "<<p[q][w].S<<"} ";
                cout<<endl;
            }
            cout<<endl;*/
        }
    }
}


#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define ms multiset
#define pb push_back
#define pll pair<ll,ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node*, Node*>
#define uid uniform_int_distribution
#define FILE ifstream in("board.in");ofstream out("board.out");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); //uid<int> u1(5, 10); u1(rnd);

int a,b,z,G=500;
vec<vec<char>> m;
vec<vec<pii>> p;
vec<vec<vec<pii>>> d;
vec<pii> mem;

pii iter(int x, int y, int step, int fl){
    if(fl) mem.clear();
    for(; x>=0 && y>=0 && x<a && y<b && step; step--){
        if((y+1<b && m[x][y]=='>' && m[x][y+1]=='<') || (y>0 && m[x][y]=='<' && m[x][y-1]=='>')){
            if(fl) mem.pb({-1,-1});
            return {-1, -1};
        }
        if(m[x][y]=='<') y--;
        else if(m[x][y]=='>') y++;
        else if(m[x][y]=='^') x--;
        if(fl) mem.pb({x,y});
    }
    return {x, y};
}

pii go(pii p, int k){
    for(int q=10; q>=0 && k; q--){
        if(p.F==-1 || p.S==-1 || p.F==a || p.S==b) return p;
        if((1<<q)>k) continue;
        k-=1<<q;
        p = d[q][p.F][p.S];
    }
    return p;
}

void bfs(int x, int y, int dst){
    if(dst==-1) return;
    p[x][y] = mem[dst];
    if(y>0 && m[x][y-1]=='>') bfs(x,y-1,dst-1);
    if(y+1<b && m[x][y+1]=='<') bfs(x,y+1,dst-1);
    if(x+1<a && m[x+1][y]=='^') bfs(x+1,y,dst-1);
}

bool can(pii p){
    return p.F>=0 && p.S>=0 && p.F<a && p.S<b;
}

int main() {
    cin>>a>>b>>z;
    d = vec<vec<vec<pii>>>(11, vec<vec<pii>>(a, vec<pii>(b)));
    m = vec<vec<char>>(a, vec<char>(b));
    p = vec<vec<pii>>(a, vec<pii>(b, {-5, -5}));
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) cin>>m[q][w];
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) d[0][q][w] = iter(q,w,1,0);
    for(int e=1; e<11; e++) for(int q=0; q<a; q++) for(int w=0; w<b; w++) d[e][q][w] = can(d[e-1][q][w]) ? d[e-1][d[e-1][q][w].F][d[e-1][q][w].S] : d[e-1][q][w];
    //for(int q=0; q<a; q++){for(int w=0; w<b; w++) cout<<"{"<<d[0][q][w].F<<", "<<d[0][q][w].S<<"} ";cout<<endl;}cout<<endl;
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) p[q][w] = go({q,w}, G);
    //for(int q=0; q<a; q++){for(int w=0; w<b; w++) cout<<"{"<<p[q][w].F<<", "<<p[q][w].S<<"} ";cout<<endl;}cout<<endl;
    for(; z; z--){
        char t; pii P; cin>>t>>P.F>>P.S; P.F--; P.S--;
        if(t=='A'){
            for(; can(P);) P = p[P.F][P.S];
            if(P.F==-1 && P.S==-1) cout<<"-1 -1\n";
            else cout<<P.F+1<<" "<<P.S+1<<"\n";
        }
        else{
            cin>>m[P.F][P.S];
            iter(P.F, P.S, G, 1);
            for(; mem.size()<G;) mem.pb(mem.back());
            bfs(P.F, P.S, mem.size()-1);
        }
    }
}


#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define ms multiset
#define pb push_back
#define pll pair<ll,ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node*, Node*>
#define uid uniform_int_distribution
#define FILE ifstream in("board.in");ofstream out("board.out");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); //uid<int> u1(5, 10); u1(rnd);

int a,b,z,G=1300;
vec<vec<char>> m;
vec<vec<pii>> p;
vec<vec<vec<pii>>> d;
vec<pii> mem;

pii iter(int x, int y, int step, int fl){
    if(fl) mem.clear();
    for(; x>=0 && y>=0 && x<a && y<b && step; step--){
        if((y+1<b && m[x][y]=='>' && m[x][y+1]=='<') || (y>0 && m[x][y]=='<' && m[x][y-1]=='>')){
            if(fl) mem.pb({-1,-1});
            return {-1, -1};
        }
        if(m[x][y]=='<') y--;
        else if(m[x][y]=='>') y++;
        else if(m[x][y]=='^') x--;
        if(fl) mem.pb({x,y});
    }
    return {x, y};
}

pii go(pii p, int k){
    for(int q=10; q>=0 && k; q--){
        if(p.F==-1 || p.S==-1 || p.F==a || p.S==b) return p;
        if((1<<q)>k) continue;
        k-=1<<q;
        p = d[q][p.F][p.S];
    }
    return p;
}

void bfs(int x, int y, int dst){
    if(dst==-1) return;
    p[x][y] = dst>=mem.size() ? mem.back() : mem[dst];
    if(y>0 && m[x][y-1]=='>') bfs(x,y-1,dst-1);
    if(y+1<b && m[x][y+1]=='<') bfs(x,y+1,dst-1);
    if(x+1<a && m[x+1][y]=='^') bfs(x+1,y,dst-1);
}

bool can(pii p){
    return p.F>=0 && p.S>=0 && p.F<a && p.S<b;
}

int main() {
    cin>>a>>b>>z;
    d = vec<vec<vec<pii>>>(11, vec<vec<pii>>(a, vec<pii>(b)));
    m = vec<vec<char>>(a, vec<char>(b));
    p = vec<vec<pii>>(a, vec<pii>(b, {-5, -5}));
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) cin>>m[q][w];
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) d[0][q][w] = iter(q,w,1,0);
    for(int e=1; e<11; e++) for(int q=0; q<a; q++) for(int w=0; w<b; w++) d[e][q][w] = can(d[e-1][q][w]) ? d[e-1][d[e-1][q][w].F][d[e-1][q][w].S] : d[e-1][q][w];
    //for(int q=0; q<a; q++){for(int w=0; w<b; w++) cout<<"{"<<d[0][q][w].F<<", "<<d[0][q][w].S<<"} ";cout<<endl;}cout<<endl;
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) p[q][w] = go({q,w}, G);
    //for(int q=0; q<a; q++){for(int w=0; w<b; w++) cout<<"{"<<p[q][w].F<<", "<<p[q][w].S<<"} ";cout<<endl;}cout<<endl;
    for(; z; z--){
        char t; pii P; cin>>t>>P.F>>P.S; P.F--; P.S--;
        if(t=='A'){
            for(; can(P);) P = p[P.F][P.S];
            if(P.F==-1 && P.S==-1) cout<<"-1 -1\n";
            else cout<<P.F+1<<" "<<P.S+1<<"\n";
        }
        else{
            cin>>m[P.F][P.S];
            iter(P.F, P.S, G, 1);
            bfs(P.F, P.S, G-1);
        }
    }
}


#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define ms multiset
#define pb push_back
#define pll pair<ll,ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node*, Node*>
#define uid uniform_int_distribution
#define FILE ifstream in("board.in");ofstream out("board.out");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); //uid<int> u1(5, 10); u1(rnd);

int a,b,z,G=300;
vec<vec<char>> m;
vec<vec<pii>> n;

pii iter(int x, int y){
    int fx = (x/G)*G-1;
    for(; x>=0 && y>=0 && x<a && y<b;){
        if(x==fx) return {fx, y};
        if(n[x][y].F!=-7) return n[x][y];
        if(y+1<b && m[x][y]=='>' && m[x][y+1]=='<') return {-1, -1};
        if(m[x][y]=='<') y--;
        else if(m[x][y]=='>') y++;
        else if(m[x][y]=='^') x--;
    }
    return {x,y};
}

void rebuild(int bl){
    int sx = G*bl, fx = min(a-1, sx+G-1);
    for(int x = sx; x<=fx; x++) for(int y=0; y<b; y++) n[x][y] = {-7, -7};
    for(int x = sx; x<=fx; x++) for(int y=0; y<b; y++) n[x][y] = iter(x,y);
}

int main() {
    cin>>a>>b>>z;
    m = vec<vec<char>>(a, vec<char>(b));
    n = vec<vec<pii>>(a, vec<pii>(b, {-7, -7}));
    for(int q=0; q<a;q ++) for(int w=0; w<b; w++) cin>>m[q][w];
    for(int q=0; q<a; q++) for(int w=0; w<b; w++) n[q][w] = iter(q,w);
    for(; z; z--){
        char t; int x,y; cin>>t>>x>>y; x--; y--;
        if(t=='A'){
            for(; x>=0 && y>=0 && x<a && y<b;){
                pii p = n[x][y];
                x = p.F;
                y = p.S;
            }
            if(x==-1 && y==-1) cout<<"-1 -1\n";
            else cout<<x+1<<" "<<y+1<<"\n";
        }
        else{
            cin>>m[x][y];
            rebuild(x/G);
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
typedef long long ll;
typedef pair<ll,ll>pll;
typedef __int128 lll;
const ll mod=1e9+9;
const int mn=1e5+10;
string s[mn];
int nxt[mn][12];
int seg[mn*4][12];
int t[12];
int n,m,q;
void mer(int c[12],int a[12],int b[12]){
    for(int i=1;i<=m;i++){
        if(b[i]>=mn)t[i]=b[i];
        else t[i]=a[b[i]];
    }
    memcpy(c,t,sizeof(t));
}
#define mid ((l+r)>>1)
void upd(int x,int l,int r,int a){
    if(l==r)memcpy(seg[x],nxt[a],sizeof(seg[x]));
    else{
        if(a<=mid)upd(x*2,l,mid,a);
        else upd(x*2+1,mid+1,r,a);
        mer(seg[x],seg[x*2],seg[x*2+1]);
    }
}
void eval(int i){
    for(int j=1;j<=m;j++){
        if(j==1&&s[i][j]=='<')nxt[i][j]=mn*2+i;
        if(j==m&&s[i][j]=='>')nxt[i][j]=mn*3+i;
        if(s[i][j]=='^')nxt[i][j]=j;
        if(j<m&&s[i][j]=='>'&&s[i][j+1]=='<')nxt[i][j]=nxt[i][j+1]=4*mn;
    }
    for(int j=2;j<=m;j++)if(s[i][j]=='<')nxt[i][j]=nxt[i][j-1];
    for(int j=m-1;j;j--)if(s[i][j]=='>')nxt[i][j]=nxt[i][j+1];
    upd(1,0,n,i);
}
int qu(int x,int l,int r,int a,int b){
    if(r==a)return seg[x][b];
    if(a<=mid)return qu(x*2,l,mid,a,b);
    int v=qu(x*2+1,mid+1,r,a,b);
    if(v>=mn)return v;
    return qu(x*2,l,mid,mid,v);
}
int main(){
    cin.tie(0);
    cin.sync_with_stdio(0);
    memset(seg,0x3f,sizeof(seg));
    cin>>n>>m>>q;
    for(int i=1;i<=n;i++){
        cin>>s[i];
        s[i]=" "+s[i];
    }
    for(int i=1;i<=m;i++)nxt[0][i]=mn+i;
    upd(1,0,n,0);
    for(int i=1;i<=n;i++){
        eval(i);
    }
    while(q--){
        string aa;
        int a,b;
        cin>>aa;
        if(aa=="A") {
            cin>>a>>b;
            int v=qu(1,0,n,a,b);
            if(v<mn*2)printf("0 %d\n",v-mn);
            else if(v<mn*3)printf("%d 0\n",v-mn*2);
            else if(v<mn*4)printf("%d %d\n",v-mn*3,m+1);
            else printf("-1 -1\n");
        }
        else{
            cin>>a>>b>>aa;
            s[a][b]=aa[0];
            eval(a);
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ii pair <int,int>
const int N=1e5+1000,M=20;
const int sqt=320;
int n,m,q;
ii nex[N][M];
string arr[N];
void dfs(int x,int y) {
	if(arr[x][y]=='^') {
		if(x==0 || (x-1)/sqt<x/sqt)
			nex[x][y]={x,y};
		else
			nex[x][y]=nex[x-1][y];
		return;
	}
	if(arr[x][y]=='<') {
		if(y==0)
			nex[x][y]={x,y};
		else
			if(arr[x][y-1]=='>')
				nex[x][y]={x,y-1};
			else
				nex[x][y]=nex[x][y-1];
		return;
	}
	if(y==m-1) {
		nex[x][y]={x,y};
		return;
	}
	dfs(x,y+1);
	if(arr[x][y+1]=='<')
		nex[x][y]={x,y+1};
	else
		nex[x][y]=nex[x][y+1];
}
int main() {
	cin>>n>>m>>q;
	for(int i=0;i<n;i++) {
		cin>>arr[i];
		for(int j=0;j<m;j++)
			dfs(i,j);
	}
	while(q--) {
		char type,c;
		int x,y;
		cin>>type;
		cin>>x>>y;
		x--;
		y--;
		if(type=='C') {
			cin>>c;
			arr[x][y]=c;
			int i=x;
			do {
				for(int j=0;j<m;j++)
					dfs(i,j);
				i++;
			}
			while(i<n && i%sqt);
		}
		else {
			int cnt=m*sqt;
			while(x<n && x>=0 && y<m && y>=0 && cnt>=0) {
				if(nex[x][y].first!=x || nex[x][y].second!=y) {
					ii p=nex[x][y];
					x=p.first;
					y=p.second;
				}
				else {
					if(arr[x][y]=='^')
						x--;
					else if(arr[x][y]=='>')
						y++;
					else
						y--;
				}
				cnt--;
			}
			if(cnt==-112)
				continue;
			if(x>=n || x<0 || y<0 || y>=m)
				cout<<x+1<<" "<<y+1<<endl;
			else
				cout<<"-1 -1"<<endl;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
typedef long long ll;
#define MAX 100005
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

char s[MAX][15];
int B,bot,top;
pair<int, int> mp[MAX][15];
pair<int, int> dfs(int i, int j) {
    if (mp[i][j] != make_pair(0, 0)) return mp[i][j];
    if (s[i][j]=='>') {
        return mp[i][j] = dfs(i,j+1);
    }
    if (s[i][j]=='<') return mp[i][j] = dfs(i,j-1);
    assert(s[i][j]=='^');
    if (i-1 < top) return mp[i][j] = {i-1,j};
    return mp[i][j] = dfs(i-1,j);
}
void init(int bid) {
    bot = n - (bid)*B; assert(bot>0);
    top = max(bot-B+1,1);
    for(int i=bot; i>=top; i--) {
        REQ(j,m) {
            if ((s[i][j]=='>' && s[i][j+1]=='<')
               || (s[i][j]=='<' && s[i][j-1]=='>')
                )mp[i][j]={-1,-1};
            else mp[i][j]={0,0};
        }
    }
    for(int i=bot; i>=top; i--)
        REQ(j,m) if (mp[i][j] == make_pair(0, 0)) {
            dfs(i,j);
        }
}
char ope[2];
pair<int, int> walk(int x, int y) {
    auto p = mp[x][y];
    if (p.first==-1 || p.first==0 || p.second==0 || p.second==m+1) return p;
    return walk(p.first, p.second);
}
int main() {
    int q;
    scanf("%d%d%d", &n, &m, &q);
    REQ(i,n) scanf("%s", s[i]+1);
    B = sqrt(n);
    for(int i=1;i<=n;i++) {
        mp[i][0] = {i,0}; mp[i][m+1]={i,m+1};
    }
    REQ(j,m) mp[0][j] = {0,j};
    int MB = (n-1)/B;
    for(int i=0;i<=MB;i++) init(i);
    int x,y;
    while(q--) {
        scanf("%s%d%d", ope, &x, &y);
        if (ope[0]=='C') {
            scanf("%s", ope);
            s[x][y] = ope[0];
            init((n-x)/B);
        } else {
            auto p = walk(x,y);
            printf("%d %d\n", p.first, p.second);
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxm = 20;

int n, m, q;
char con[maxn][maxm];
pair<int, int> nxt[maxn][maxm];
bool marked[maxn][maxm];
bool st[maxn][maxm];
int block;

void dfs(int i, int j){
	if (marked[i][j]){
		if (!st[i][j]){
			nxt[i][j] = make_pair(-2, -2);
			st[i][j] = true;
		}
		return;
	}

	marked[i][j] = true;

	switch (con[i][j]){
		case '^':
			if (i-1 == -1){
				nxt[i][j] = make_pair(-1, j);
				st[i][j] = true;
				return;
			}
			if ((i-1) / block < i/block){
				nxt[i][j] = make_pair(i-1, j);
				st[i][j] = true;
				return;
			}
			dfs(i-1, j);
			nxt[i][j] = nxt[i-1][j];
			st[i][j] = true;
			break;
		case '>':
			if (j + 1 == m){
				nxt[i][j] = make_pair(i, m);
				st[i][j] = true;
				return;
			}
			dfs(i, j+1);
			nxt[i][j] = nxt[i][j+1];
			st[i][j] = true;
			break;
		case '<':
			if (j-1 == -1){
				nxt[i][j] = make_pair(i, -1);
				st[i][j] = true;
				return;
			}
			dfs(i, j-1);
			nxt[i][j] = nxt[i][j-1];
			st[i][j] = true;
			break;
	}
}

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

int main(){
	scanf("%d%d%d", &n, &m, &q);
	block = sqrt(n);
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			scanf(" %c", &con[i][j]);
		}
	}

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

	memset(st, false, sizeof st);
	memset(marked, false, sizeof marked);
	while (q--){
		char det;
		scanf(" %c", &det);
		if (det == 'A'){
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			int t;
			while (check(x, y)){
				t = x;
				x = nxt[x][y].first;
				y = nxt[t][y].second;
			}
			printf("%d %d\n", x+1, y+1);
		}
		else {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			char c;
			scanf(" %c", &c);
			con[x][y] = c;
			for (int i = (x/block) * block; i/block == x/block && i < n; i++){
				for (int j = 0; j < m; j++){
					if (!st[i][j]){
						dfs(i, j);
					}
				}
			}

			for (int i = (x/block) * block; i/block == x/block && i < n; i++){
				memset(st[i], false, sizeof st[i]);
				memset(marked[i], false, sizeof marked[i]);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxm = 20;

int n, m, q;
char con[maxn][maxm];
pair<int, int> nxt[maxn][maxm];
bool marked[maxn][maxm];
bool st[maxn][maxm];
int block;

void dfs(int i, int j){
	if (marked[i][j]){
		if (!st[i][j]){
			nxt[i][j] = make_pair(-2, -2);
		//	st[i][j] = true;
		}
		return;
	}

	marked[i][j] = true;

	switch (con[i][j]){
		case '^':
			if (i-1 == -1){
				nxt[i][j] = make_pair(-1, j);
				st[i][j] = true;
				return;
			}
			if ((i-1) / block < i/block){
				nxt[i][j] = make_pair(i-1, j);
				st[i][j] = true;
				return;
			}
			dfs(i-1, j);
			nxt[i][j] = nxt[i-1][j];
			st[i][j] = true;
			break;
		case '>':
			if (j + 1 == m){
				nxt[i][j] = make_pair(i, m);
				st[i][j] = true;
				return;
			}
			dfs(i, j+1);
			nxt[i][j] = nxt[i][j+1];
			st[i][j] = true;
			break;
		case '<':
			if (j-1 == -1){
				nxt[i][j] = make_pair(i, -1);
				st[i][j] = true;
				return;
			}
			dfs(i, j-1);
			nxt[i][j] = nxt[i][j-1];
			st[i][j] = true;
			break;
	}
}

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

int main(){
	scanf("%d%d%d", &n, &m, &q);
	block = max(100,(int) sqrt(n));
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			scanf(" %c", &con[i][j]);
		}
	}

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

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

	memset(st, false, sizeof st);
	memset(marked, false, sizeof marked);
	while (q--){
		char det;
		scanf(" %c", &det);
		if (det == 'A'){
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			int t;
			while (check(x, y)){
				t = x;
				x = nxt[x][y].first;
				y = nxt[t][y].second;
			}
			printf("%d %d\n", x+1, y+1);
		}
		else {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			char c;
			scanf(" %c", &c);
			con[x][y] = c;
			for (int i = x; i/block == x/block && i < n; i++){
				for (int j = 0; j < m; j++){
					if (!st[i][j]){
						dfs(i, j);
					}
				}
			}

			for (int i = x; i/block == x/block && i < n; i++){
				memset(st[i], false, sizeof st[i]);
				memset(marked[i], false, sizeof marked[i]);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxm = 20;

int n, m, q;
char con[maxn][maxm];
pair<int, int> nxt[maxn][maxm];
bool marked[maxn][maxm];
bool st[maxn][maxm];
int block;

void dfs(int i, int j){
	if (marked[i][j]){
		if (!st[i][j]){
			nxt[i][j] = make_pair(-2, -2);
		//	st[i][j] = true;
		}
		return;
	}

	marked[i][j] = true;

	switch (con[i][j]){
		case '^':
			if (i-1 == -1){
				nxt[i][j] = make_pair(-1, j);
				st[i][j] = true;
				return;
			}
			if ((i-1) / block < i/block){
				nxt[i][j] = make_pair(i-1, j);
				st[i][j] = true;
				return;
			}
			dfs(i-1, j);
			nxt[i][j] = nxt[i-1][j];
			st[i][j] = true;
			break;
		case '>':
			if (j + 1 == m){
				nxt[i][j] = make_pair(i, m);
				st[i][j] = true;
				return;
			}
			dfs(i, j+1);
			nxt[i][j] = nxt[i][j+1];
			st[i][j] = true;
			break;
		case '<':
			if (j-1 == -1){
				nxt[i][j] = make_pair(i, -1);
				st[i][j] = true;
				return;
			}
			dfs(i, j-1);
			nxt[i][j] = nxt[i][j-1];
			st[i][j] = true;
			break;
	}
}

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

int main(){
	scanf("%d%d%d", &n, &m, &q);
	block = sqrt(n);
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			scanf(" %c", &con[i][j]);
		}
	}

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

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

	memset(st, false, sizeof st);
	memset(marked, false, sizeof marked);
	while (q--){
		char det;
		scanf(" %c", &det);
		if (det == 'A'){
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			int t;
			while (check(x, y)){
				t = x;
				x = nxt[x][y].first;
				y = nxt[t][y].second;
			}
			printf("%d %d\n", x+1, y+1);
		}
		else {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			char c;
			scanf(" %c", &c);
			con[x][y] = c;
			for (int i = x; i/block == x/block; i++){
				for (int j = 0; j < m; j++){
					if (!st[i][j]){
						dfs(i, j);
					}
				}
			}

			for (int i = x; i/block == x/block; i++){
				memset(st[i], false, sizeof st[i]);
				memset(marked[i], false, sizeof marked[i]);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxm = 20;

int n, m, q;
char con[maxn][maxm];
pair<int, int> nxt[maxn][maxm];
bool marked[maxn][maxm];
bool st[maxn][maxm];
int block;

void dfs(int i, int j){
	if (marked[i][j]){
		if (!st[i][j]){
			nxt[i][j] = make_pair(-2, -2);
		//	st[i][j] = true;
		}
		return;
	}

	marked[i][j] = true;

	switch (con[i][j]){
		case '^':
			if (i-1 == -1){
				nxt[i][j] = make_pair(-1, j);
				st[i][j] = true;
				return;
			}
			if ((i-1) / block < i/block){
				nxt[i][j] = make_pair(i-1, j);
				st[i][j] = true;
				return;
			}
			dfs(i-1, j);
			nxt[i][j] = nxt[i-1][j];
			st[i][j] = true;
			break;
		case '>':
			if (j + 1 == m){
				nxt[i][j] = make_pair(i, m);
				st[i][j] = true;
				return;
			}
			dfs(i, j+1);
			nxt[i][j] = nxt[i][j+1];
			st[i][j] = true;
			break;
		case '<':
			if (j-1 == -1){
				nxt[i][j] = make_pair(i, -1);
				st[i][j] = true;
				return;
			}
			dfs(i, j-1);
			nxt[i][j] = nxt[i][j-1];
			st[i][j] = true;
			break;
	}
}

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

int main(){
	scanf("%d%d%d", &n, &m, &q);
	block = sqrt(n);
	/*sdfs*/
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			scanf(" %c", &con[i][j]);
		}
	}

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

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

	memset(st, false, sizeof st);
	memset(marked, false, sizeof marked);
	while (q--){
		char det;
		scanf(" %c", &det);
		if (det == 'A'){
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			int t;
			while (check(x, y)){
				t = x;
				x = nxt[x][y].first;
				y = nxt[t][y].second;
			}
			printf("%d %d\n", x+1, y+1);
		}
		else {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			char c;
			scanf(" %c", &c);
			con[x][y] = c;
			for (int i = x; i/block == x/block && i < n; i++){
				for (int j = 0; j < m; j++){
					if (!st[i][j]){
						dfs(i, j);
					}
				}
			}

			for (int i = x; i/block == x/block && i < n; i++){
				memset(st[i], false, sizeof st[i]);
				memset(marked[i], false, sizeof marked[i]);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxm = 20;

int n, m, q;
char con[maxn][maxm];
pair<int, int> nxt[maxn][maxm];
bool marked[maxn][maxm];
bool st[maxn][maxm];
int block;

void dfs(int i, int j){
	if (marked[i][j]){
		if (!st[i][j]){
			nxt[i][j] = make_pair(-2, -2);
		//	st[i][j] = true;
		}
		return;
	}

	marked[i][j] = true;

	switch (con[i][j]){
		case '^':
			if (i-1 == -1){
				nxt[i][j] = make_pair(-1, j);
				st[i][j] = true;
				return;
			}
			if ((i-1) / block < i/block){
				nxt[i][j] = make_pair(i-1, j);
				st[i][j] = true;
				return;
			}
			dfs(i-1, j);
			nxt[i][j] = nxt[i-1][j];
			st[i][j] = true;
			break;
		case '>':
			if (j + 1 == m){
				nxt[i][j] = make_pair(i, m);
				st[i][j] = true;
				return;
			}
			dfs(i, j+1);
			nxt[i][j] = nxt[i][j+1];
			st[i][j] = true;
			break;
		case '<':
			if (j-1 == -1){
				nxt[i][j] = make_pair(i, -1);
				st[i][j] = true;
				return;
			}
			dfs(i, j-1);
			nxt[i][j] = nxt[i][j-1];
			st[i][j] = true;
			break;
	}
}

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

int main(){
	scanf("%d%d%d", &n, &m, &q);
	block = max(10, (int) sqrt(n));
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			scanf(" %c", &con[i][j]);
		}
	}

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

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

	memset(st, false, sizeof st);
	memset(marked, false, sizeof marked);
	while (q--){
		char det;
		scanf(" %c", &det);
		if (det == 'A'){
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			int t;
			while (check(x, y)){
				t = x;
				x = nxt[x][y].first;
				y = nxt[t][y].second;
			}
			printf("%d %d\n", x+1, y+1);
		}
		else {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			char c;
			scanf(" %c", &c);
			con[x][y] = c;
			for (int i = x; i/block == x/block; i++){
				for (int j = 0; j < m; j++){
					if (!st[i][j]){
						dfs(i, j);
					}
				}
			}

			for (int i = x; i/block == x/block; i++){
				memset(st[i], false, sizeof st[i]);
				memset(marked[i], false, sizeof marked[i]);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxm = 20;

int n, m, q;
char con[maxn][maxm];
pair<int, int> nxt[maxn][maxm];
bool marked[maxn][maxm];
bool st[maxn][maxm];
int block;

void dfs(int i, int j){
	if (marked[i][j]){
		if (!st[i][j]){
			nxt[i][j] = make_pair(-2, -2);
		//	st[i][j] = true;
		}
		return;
	}

	marked[i][j] = true;

	switch (con[i][j]){
		case '^':
			if (i-1 == -1){
				nxt[i][j] = make_pair(-1, j);
				st[i][j] = true;
				return;
			}
			if ((i-1) / block < i/block){
				nxt[i][j] = make_pair(i-1, j);
				st[i][j] = true;
				return;
			}
			dfs(i-1, j);
			nxt[i][j] = nxt[i-1][j];
			st[i][j] = true;
			break;
		case '>':
			if (j + 1 == m){
				nxt[i][j] = make_pair(i, m);
				st[i][j] = true;
				return;
			}
			dfs(i, j+1);
			nxt[i][j] = nxt[i][j+1];
			st[i][j] = true;
			break;
		case '<':
			if (j-1 == -1){
				nxt[i][j] = make_pair(i, -1);
				st[i][j] = true;
				return;
			}
			dfs(i, j-1);
			nxt[i][j] = nxt[i][j-1];
			st[i][j] = true;
			break;
	}
}

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

int main(){
	scanf("%d%d%d", &n, &m, &q);
	block = max(2,(int) sqrt(n));
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			scanf(" %c", &con[i][j]);
		}
	}

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

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

	memset(st, false, sizeof st);
	memset(marked, false, sizeof marked);
	while (q--){
		char det;
		scanf(" %c", &det);
		if (det == 'A'){
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			int t;
			while (check(x, y)){
				t = x;
				x = nxt[x][y].first;
				y = nxt[t][y].second;
			}
			printf("%d %d\n", x+1, y+1);
		}
		else {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			char c;
			scanf(" %c", &c);
			con[x][y] = c;
			for (int i = x; i/block == x/block && i < n; i++){
				for (int j = 0; j < m; j++){
					if (!st[i][j]){
						dfs(i, j);
					}
				}
			}

			for (int i = x; i/block == x/block && i < n; i++){
				memset(st[i], false, sizeof st[i]);
				memset(marked[i], false, sizeof marked[i]);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxm = 20;

int n, m, q;
char con[maxn][maxm];
pair<int, int> nxt[maxn][maxm];
bool marked[maxn][maxm];
bool st[maxn][maxm];
int block;

void dfs(int i, int j){
	if (marked[i][j]){
		if (!st[i][j]){
			nxt[i][j] = make_pair(-2, -2);
			st[i][j] = true;
		}
		return;
	}

	marked[i][j] = true;

	switch (con[i][j]){
		case '^':
			if (i-1 == -1){
				nxt[i][j] = make_pair(-1, j);
				st[i][j] = true;
				return;
			}
			if ((i-1) / block > i/block){
				nxt[i][j] = make_pair(i-1, j);
				st[i][j] = true;
				return;
			}
			dfs(i-1, j);
			nxt[i][j] = nxt[i-1][j];
			st[i][j] = true;
			break;
		case '>':
			if (j + 1 == m){
				nxt[i][j] = make_pair(i, m);
				st[i][j] = true;
				return;
			}
			dfs(i, j+1);
			nxt[i][j] = nxt[i][j+1];
			st[i][j] = true;
			break;
		case '<':
			if (j-1 == -1){
				nxt[i][j] = make_pair(i, -1);
				st[i][j] = true;
				return;
			}
			dfs(i, j-1);
			nxt[i][j] = nxt[i][j-1];
			st[i][j] = true;
			break;
	}
}

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

int main(){
	scanf("%d%d%d", &n, &m, &q);
	block = max(0, (int) sqrt(n));
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			scanf(" %c", &con[i][j]);
		}
	}

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

	memset(st, false, sizeof st);
	memset(marked, false, sizeof marked);
	while (q--){
		char det;
		scanf(" %c", &det);
		if (det == 'A'){
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			int t;
			while (check(x, y)){
				t = x;
				x = nxt[x][y].first;
				y = nxt[t][y].second;
			}
			printf("%d %d\n", x+1, y+1);
		}
		else {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			char c;
			scanf(" %c", &c);
			con[x][y] = c;
			for (int i = x; i/block <= x/block; i++){
				for (int j = 0; j < m; j++){
					if (!st[i][j]){
						dfs(i, j);
					}
				}
			}

			for (int i = x; i/block <= x/block; i++){
				memset(st[i], false, sizeof st[i]);
				memset(marked[i], false, sizeof marked[i]);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxm = 20;

int n, m, q;
char con[maxn][maxm];
pair<int, int> nxt[maxn][maxm];
bool marked[maxn][maxm];
bool st[maxn][maxm];
int block;

void dfs(int i, int j){
	if (marked[i][j]){
		if (!st[i][j]){
			nxt[i][j] = make_pair(-2, -2);
		//	st[i][j] = true;
		}
		return;
	}

	marked[i][j] = true;

	switch (con[i][j]){
		case '^':
			if (i-1 == -1){
				nxt[i][j] = make_pair(-1, j);
				st[i][j] = true;
				return;
			}
			if ((i-1) / block < i/block){
				nxt[i][j] = make_pair(i-1, j);
				st[i][j] = true;
				return;
			}
			dfs(i-1, j);
			nxt[i][j] = nxt[i-1][j];
			st[i][j] = true;
			break;
		case '>':
			if (j + 1 == m){
				nxt[i][j] = make_pair(i, m);
				st[i][j] = true;
				return;
			}
			dfs(i, j+1);
			nxt[i][j] = nxt[i][j+1];
			st[i][j] = true;
			break;
		case '<':
			if (j-1 == -1){
				nxt[i][j] = make_pair(i, -1);
				st[i][j] = true;
				return;
			}
			dfs(i, j-1);
			nxt[i][j] = nxt[i][j-1];
			st[i][j] = true;
			break;
	}
}

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

int main(){
	scanf("%d%d%d", &n, &m, &q);
	block = max(100, sqrt(n));
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			scanf(" %c", &con[i][j]);
		}
	}

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

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

	memset(st, false, sizeof st);
	memset(marked, false, sizeof marked);
	while (q--){
		char det;
		scanf(" %c", &det);
		if (det == 'A'){
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			int t;
			while (check(x, y)){
				t = x;
				x = nxt[x][y].first;
				y = nxt[t][y].second;
			}
			printf("%d %d\n", x+1, y+1);
		}
		else {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			char c;
			scanf(" %c", &c);
			con[x][y] = c;
			for (int i = x; i/block == x/block && i < n; i++){
				for (int j = 0; j < m; j++){
					if (!st[i][j]){
						dfs(i, j);
					}
				}
			}

			for (int i = x; i/block == x/block && i < n; i++){
				memset(st[i], false, sizeof st[i]);
				memset(marked[i], false, sizeof marked[i]);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxm = 20;

int n, m, q;
char con[maxn][maxm];
pair<int, int> nxt[maxn][maxm];
bool marked[maxn][maxm];
bool st[maxn][maxm];
int block;

void dfs(int i, int j){
	if (marked[i][j]){
		if (!st[i][j]){
			nxt[i][j] = make_pair(-2, -2);
			set[i][j] = true;
		}
		return;
	}

	marked[i][j] = true;

	switch (con[i][j]){
		case '^':
			if (i-1 == -1){
				nxt[i][j] = make_pair(-1, j);
				st[i][j] = true;
				return;
			}
			if ((i-1) / block != i/block){
				nxt[i][j] = make_pair(i-1, j);
				st[i][j] = true;
				return;
			}
			dfs(i-1, j);
			nxt[i][j] = nxt[i-1][j];
			st[i][j] = true;
			break;
		case '>':
			if (j + 1 == m){
				nxt[i][j] = make_pair(i, m);
				st[i][j] = true;
				return;
			}
			dfs(i, j+1);
			nxt[i][j] = nxt[i][j+1];
			st[i][j] = true;
			break;
		case '<':
			if (j-1 == -1){
				nxt[i][j] = make_pair(i, -1);
				st[i][j] = true;
				return;
			}
			dfs(i, j-1);
			nxt[i][j] = nxt[i][j-1];
			st[i][j] = true;
			break;
	}
}

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

int main(){
	scanf("%d%d%d", &n, &m, &q);
	block = sqrt(n);
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			scanf(" %c", &con[i][j]);
		}
	}

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

	memset(st, false, sizeof st);
	memset(marked, false, sizeof marked);
	while (q--){
		char det;
		scanf(" %c", &det);
		if (det == 'A'){
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			int xt, yt;
			while (check(x, y)){
				xt = x;
				yt = y;
				x = nxt[xt][yt].first;
				y = nxt[xt][yt].second;
			}
			printf("%d %d\n", x+1, y+1);
		}
		else {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			char c;
			scanf(" %c", &c);
			con[x][y] = c;
			for (int i = x; i/block == x/block; i++){
				for (int j = 0; j < m; j++){
					if (!st[i][j]){
						dfs(i, j);
					}
				}
			}

			for (int i = x; i/block == x/block; i++){
				memset(st[i], false, sizeof st[i]);
				memset(marked[i], false, sizeof marked[i]);
			}
		}
	}
	return 0;
}


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

vector<int> par;
vector<int> ch;
vector<int> trav;
vector<int> roots;

int dfs(int i,int &time){
	// cerr<<i<<" "<<time<<endl;
	if(trav[i] && trav[i]==time){
		par[i]=i;
		ch[i]=1;
		roots.push_back(i);
		// cerr<<"*****"<<endl;
		return 1;
	}
	else if(trav[i]){
		// cerr<<trav[i]<<endl;
		// cerr<<"--->"<<time<<endl;
		return 1;
	}
	trav[i]=time;
	return dfs(par[i],time);
}

int main(){
	int n;cin>>n;
	par.resize(n+1);ch.resize(n+1);
	trav.resize(n+1);
	for(int i=1;i<=n;i++){
		cin>>par[i];
	}
	for(int i=1;i<=n;i++){
		if(!trav[i])dfs(i,i);
	}
	// cerr<<"done"<<endl;
	// for(auto x:roots)cout<<x<<"**";cerr<<endl;
	int ans=roots.size()-1;
	int par1=roots.back();
	roots.pop_back();
	for(auto x:roots)
		par[x]=par1;
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)cout<<par[i]<<" ";
	return 0;
}


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

vector<int> par;
vector<int> ch;
vector<int> trav;
vector<int> roots;

int dfs(int i,int &time){
	// cerr<<i<<" "<<time<<endl;
	if(trav[i] && trav[i]==time){
		if(par[i]!=i)ch[i]++;
		par[i]=i;
		roots.push_back(i);
		// cerr<<"*****"<<endl;
		return 1;
	}
	else if(trav[i]){
		// cerr<<trav[i]<<endl;
		// cerr<<"--->"<<time<<endl;
		return 1;
	}
	trav[i]=time;
	return dfs(par[i],time);
}

int main(){
	int n;cin>>n;
	par.resize(n+1);ch.resize(n+1);
	trav.resize(n+1);
	for(int i=1;i<=n;i++){
		cin>>par[i];
	}
	for(int i=1;i<=n;i++){
		if(!trav[i])dfs(i,i);
	}
	// cerr<<"done"<<endl;
	// for(auto x:roots)cerr<<x<<"**";cerr<<endl;
	// int ans=roots.size()-1;
	int ans=0;
	int par1;
	for(auto x:ch)ans+=x;
	if(ans==roots.size()){
		par1=roots.back();
	}
	else{
		ans=roots.size()-1;
		for(auto x:roots)
			if(!ch[x]){par1=x;break;}
	}
	for(auto x:roots)
		par[x]=par1;
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)cout<<par[i]<<" ";
	return 0;
}


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

vector<int> par;
vector<int> ch;
vector<int> trav;
vector<int> roots;

int dfs(int i,int &time){
	cerr<<i<<" "<<time<<endl;
	if(trav[i] && trav[i]==time){
		if(par[i]!=i)ch[i]++;
		par[i]=i;
		roots.push_back(i);
		cerr<<"*****"<<endl;
		return 1;
	}
	else if(trav[i]){
		cerr<<trav[i]<<endl;
		cerr<<"--->"<<time<<endl;
		return 1;
	}
	trav[i]=time;
	return dfs(par[i],time);
}

int main(){
	int n;cin>>n;
	par.resize(n+1);ch.resize(n+1);
	trav.resize(n+1);
	for(int i=1;i<=n;i++){
		cin>>par[i];
	}
	for(int i=1;i<=n;i++){
		if(!trav[i])dfs(i,i);
	}
	cerr<<"done"<<endl;
	for(auto x:roots)cout<<x<<"**";cerr<<endl;
	// int ans=roots.size()-1;
	int ans=0;
	for(auto x:ch)ans+=x;
	if(ans==roots.size());
	else ans=roots.size()-1;

	int par1=roots.back();
	roots.pop_back();
	for(auto x:roots)
		par[x]=par1;
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)cout<<par[i]<<" ";
	return 0;
}


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

vector<int> par;
vector<int> ch;
vector<int> trav;
vector<int> roots;

int dfs(int i,int &time){
	// cerr<<i<<" "<<time<<endl;
	if(trav[i] && trav[i]==time){
		if(par[i]!=i)ch[i]++;
		par[i]=i;
		roots.push_back(i);
		// cerr<<"*****"<<endl;
		return 1;
	}
	else if(trav[i]){
		// cerr<<trav[i]<<endl;
		// cerr<<"--->"<<time<<endl;
		return 1;
	}
	trav[i]=time;
	return dfs(par[i],time);
}

int main(){
	int n;cin>>n;
	par.resize(n+1);ch.resize(n+1);
	trav.resize(n+1);
	for(int i=1;i<=n;i++){
		cin>>par[i];
	}
	for(int i=1;i<=n;i++){
		if(!trav[i])dfs(i,i);
	}
	// cerr<<"done"<<endl;
	// for(auto x:roots)cerr<<x<<"**";cerr<<endl;
	// int ans=roots.size()-1;
	int ans=0;
	for(auto x:ch)ans+=x;
	if(ans==roots.size());
	else ans=roots.size()-1;

	int par1=roots.back();
	roots.pop_back();
	for(auto x:roots)
		par[x]=par1;
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)cout<<par[i]<<" ";
	return 0;
}


// Gear4
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define FOR0(i,n) for(int i=0, _##i=(n); i<_##i; ++i)
#define FOR(i,l,r) for(int i=(l), _##i=(r); i<_##i; ++i)
#define FORD(i,l,r) for(int i=(r), _##i=(l); --i>=_##i; )
#define SZ(a) ((int)((a).size()))
#define printCase() "Case #" << caseNum << ": "
#define pb push_back
#define mp make_pair
#define SYNC std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define ff first
#define ss second
template<typename T> using V = vector<T>;
template<typename T, typename U> using P = pair<T,U>;
using ld = long double;
using ll = long long;
#define endl '\n'
const int inf = 1e9, mod = 1e9 + 7;
const ll INF = 9e18;
const ld EPS = 1e-9, PI = acosl(-1.0);
const int N = 2e5 + 5;
int n, a[N];
vector<int> vis, vec;
void dfs(int u) {
    vis[u] = 1;
    if(!vis[a[u]]) dfs(a[u]);
    else if(vis[a[u]] == 1) vec.push_back(u);
    vis[u] = 2;
}
int main() {
    SYNC
    cin >> n;
    FOR0(i, n) cin >> a[i+1];
    int root = 0, ans = 0;
    FOR(i, 1, n+1) {
        if(a[i] == i) root = i;
    }
    FOR(i, 1, n + 1) if(!vis[i]) dfs(i);
    if(!root && vec.size()) root = vec[0];
    for(int i : vec) {
        if(root && i != root) a[i] = root;
    }
    cout << ans << endl;
    FOR(i, 1, n + 1) cout << a[i] << " ";
}


// Gear4
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define FOR0(i,n) for(int i=0, _##i=(n); i<_##i; ++i)
#define FOR(i,l,r) for(int i=(l), _##i=(r); i<_##i; ++i)
#define FORD(i,l,r) for(int i=(r), _##i=(l); --i>=_##i; )
#define SZ(a) ((int)((a).size()))
#define printCase() "Case #" << caseNum << ": "
#define pb push_back
#define mp make_pair
#define SYNC std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define ff first
#define ss second
template<typename T> using V = vector<T>;
template<typename T, typename U> using P = pair<T,U>;
using ld = long double;
using ll = long long;
#define endl '\n'
const int inf = 1e9, mod = 1e9 + 7;
const ll INF = 9e18;
const ld EPS = 1e-9, PI = acosl(-1.0);
const int N = 2e5 + 5;
int n, a[N];
vector<int> vis(N), vec;
void dfs(int u) {
    vis[u] = 1;
    if (!vis[a[u]]) dfs(a[u]);
    else if (vis[a[u]] == 1) vec.push_back(u);
    vis[u] = 2;
}
int main() {
    SYNC
    cin >> n;
    FOR0(i, n) cin >> a[i+1];
    int root = 0, ans = 0;
    FOR(i, 1, n+1) {
        if(a[i] == i) root = i;
    }
    FOR(i, 1, n + 1) if(!vis[i]) dfs(i);
    if(!root && vec.size()) root = vec[0];
    if(root) {
        for(int i : vec) {
            if(a[i] == root) continue;
            ans++, a[i] = root;
        }
    }
    cout << ans << endl;
    FOR(i, 1, n + 1) cout << a[i] << " ";
}


// Gear4
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define FOR0(i,n) for(int i=0, _##i=(n); i<_##i; ++i)
#define FOR(i,l,r) for(int i=(l), _##i=(r); i<_##i; ++i)
#define FORD(i,l,r) for(int i=(r), _##i=(l); --i>=_##i; )
#define SZ(a) ((int)((a).size()))
#define printCase() "Case #" << caseNum << ": "
#define pb push_back
#define mp make_pair
#define SYNC std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define ff first
#define ss second
template<typename T> using V = vector<T>;
template<typename T, typename U> using P = pair<T,U>;
using ld = long double;
using ll = long long;
#define endl '\n'
const int inf = 1e9, mod = 1e9 + 7;
const ll INF = 9e18;
const ld EPS = 1e-9, PI = acosl(-1.0);
const int N = 2e5 + 5;
int n, a[N];
vector<int> vis(N), vec;
void dfs(int u) {
    vis[u] = 1;
    if (!vis[a[u]]) dfs(a[u]);
    else if (vis[a[u]] == 1) vec.push_back(u);
    vis[u] = 2;
}
int main() {
    SYNC
    cin >> n;
    FOR0(i, n) cin >> a[i+1];
    int root = 0, ans = 0;
    FOR(i, 1, n+1) {
        if(a[i] == i) root = i;
    }
    FOR(i, 1, n + 1) if(!vis[i]) dfs(i);
    if(!root && vec.size()) root = vec[0];
    for(int i : vec) {
        if(root && i != root) ans++, a[i] = root;
    }
    cout << ans << endl;
    FOR(i, 1, n + 1) cout << a[i] << " ";
}


// Gear4
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define FOR0(i,n) for(int i=0, _##i=(n); i<_##i; ++i)
#define FOR(i,l,r) for(int i=(l), _##i=(r); i<_##i; ++i)
#define FORD(i,l,r) for(int i=(r), _##i=(l); --i>=_##i; )
#define SZ(a) ((int)((a).size()))
#define printCase() "Case #" << caseNum << ": "
#define pb push_back
#define mp make_pair
#define SYNC std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define ff first
#define ss second
template<typename T> using V = vector<T>;
template<typename T, typename U> using P = pair<T,U>;
using ld = long double;
using ll = long long;
#define endl '\n'
const int inf = 1e9, mod = 1e9 + 7;
const ll INF = 9e18;
const ld EPS = 1e-9, PI = acosl(-1.0);
const int N = 2e5 + 5;
int n, a[N];
int main() {
    SYNC
    cin >> n;
    FOR0(i, n) cin >> a[i+1];
    int root = 0, ans = 0;
    FOR(i, 1, n+1) {
        if(a[i] == i) root = i;
    }
    FOR(i, 1, n+1) {
        if(i != root && i == a[a[i]]) {
            if(!root) root = i;
            ans++;
            a[i] = root;
        }
    }
    cout << ans << endl;
    FOR(i, 1, n + 1) cout << a[i] << " ";
}


#include<bits/stdc++.h>
using namespace std;
int find1(int arr[],int idx,bool visited[],bool recStack[],int *root)
{
    if(recStack[idx]==true)
    {
        if(*root==-1)
            *root=idx;
        arr[idx]=*root;
        recStack[idx]=false;
        return 1;
    }
    else if(visited[idx]!=true)
    {
        visited[idx]=true;
        recStack[idx]=true;
        if(arr[idx]!=idx)
        {
            int temp=find1(arr,arr[idx],visited,recStack,root);
            recStack[idx]=false;
            return temp;
        }
        else
        {
            if(arr[idx]!=*root)
            {
                arr[idx]=*root;
                recStack[idx]=false;
                return 1;
            }
            recStack[idx]=false;
            return 0;
        }
    }
    return 0;
}
int main()
{
    int n;
    cin>>n;

    int arr[n],root=-1;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr[i]-=1;
        if(arr[i]==i)
            root=arr[i];
    }
    int ans=0;
    bool visited[n],recStack[n];
    for(int i=0;i<n;i++)
    {
        visited[i]=false;
        recStack[i]=false;
    }
    for(int i=0;i<n;i++)
    {
        if(!visited[i])
        {
             ans+=find1(arr,i,visited,recStack,&root);
             //cout<<ans<<i<<endl;
        }
    }
    cout<<ans<<endl;
    for(int i=0;i<n;i++)
        cout<<arr[i]+1<<" ";
    cout<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int find1(int arr[],int idx,bool visited[],bool recStack[],int *root)
{
    if(recStack[idx]==true)
    {
        if(*root==-1)
            *root=idx;
        arr[idx]=*root;
        recStack[idx]=false;
        return 1;
    }
    else if(!visited[idx])
    {
        visited[idx]=true;
        recStack[idx]=true;
        if(arr[idx]!=idx)
        {
            int temp=find1(arr,arr[idx],visited,recStack,root);
            recStack[idx]=false;
            return temp;
        }
        if(*root==-1)
            *root=arr[idx];

        if(arr[idx]!=*root)
        {
            arr[idx]=*root;
            recStack[idx]=false;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int n;
    cin>>n;

    int arr[n],root=-1;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr[i]-=1;
        if(arr[i]==i)
            root=arr[i];
    }
    int ans=0;
    bool visited[n],recStack[n];
    for(int i=0;i<n;i++)
    {
        visited[i]=false;
        recStack[i]=false;
    }
    for(int i=0;i<n;i++)
    {
        if(!visited[i])
            ans+=find1(arr,i,visited,recStack,&root);
    }
    cout<<ans<<endl;
    for(int i=0;i<n;i++)
        cout<<arr[i]+1<<" ";
    cout<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int find1(int arr[],int idx,bool visited[],bool recStack[],int *root)
{
    visited[idx]=true;
    if(recStack[idx]==true)
    {
        if(*root==-1)
            *root=idx;
        arr[idx]=*root;
        recStack[idx]=false;
        return 1;
    }
    recStack[idx]=true;
    if(arr[idx]!=idx)
    {
        int temp=find1(arr,arr[idx],visited,recStack,root);
        recStack[idx]=false;
        return temp;
    }
    if(*root==-1)
        *root=arr[idx];

    if(arr[idx]!=*root)
    {
        arr[idx]=*root;
        recStack[idx]=false;
        return 1;
    }
    recStack[idx]=false;
    return 0;
}
int main()
{
    int n;
    cin>>n;

    int arr[n],root=-1;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr[i]-=1;
        if(arr[i]==i)
            root=arr[i];
    }
    int ans=0;
    bool visited[n],recStack[n];
    for(int i=0;i<n;i++)
    {
        visited[i]=false;
        recStack[i]=false;
    }
    for(int i=0;i<n;i++)
    {
        if(!visited[i])
            ans+=find1(arr,i,visited,recStack,&root);
    }
    cout<<ans<<endl;
    for(int i=0;i<n;i++)
        cout<<arr[i]+1<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];
int par2[MAXN];
int p2[MAXN];
vector<int> cycle;
int find(int u)
{
   if(u == par[u])
        return u;
   return find(par[u]);
}

int find2(int u)
{
   if(u == par2[u])
        return u;
   return find2(par2[u]);
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        {
            par[b] = a;
        }
}

void connect2(int u,int v)
{
   int a = find2(u);
   int b = find2(v);
   if(a != b)
        {
            par2[b] = a;
        }
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = par2[i] = i;
}

int main()
{

int n;
cin >> n;
init();
int cycles = 0;
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        if(p[i] == i)
           continue;
       if(find(p[i]) != find(i)) connect(p[i],i);
       else
          {
          	cycle.push_back(i);
          	++cycles;
          }
     }
  set<int> s;
  for(int i = 1 ; i <= n ; i++)
     s.insert(find(i));
  for(int i = 1 ; i <= n ; i++)
   {
  // 	cout << i << ' ' << find(i) << '\n';
   }
  if(s.size() == 1 and cycles)
     {
     	p[cycle.back()] = cycle.back();
     	cout << 1 << '\n';
       for(int i = 1 ; i <= n ; i++)
    cout << p[i] << ' ';
     	return 0;
     }
   if(cycles)
     --cycles;
  int ans = s.size() - 1 + cycles;
  cout << ans << '\n';
  for(auto &k : cycle)
   {
   	if(cycles > 1)
   	 {
   	 	p[k] = k;
   	 	--cycles;
   	 }
   }
   vector<int> link;
   for(auto &k : s)
       link.push_back(k);
  int root = link[0];
  for(int i = 1 ; i < link.size() ; i++)
          p[link[i]] = root;
  for(int i = 1 ; i <= n ; i++)
    cout << p[i] << ' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];
vector<int> g[MAXN];
int find(int u)
{
   if(u == par[u])
        return u;
   return find(par[u]);
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        {
            par[b] = a;
        }
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = i;
}

int main()
{

int n;
cin >> n;
init();
int cycles = 0;
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        if(p[i] == i)
           continue;
       if(find(p[i]) != find(i)) connect(p[i],i);
       else
          {
          	p[i] = i;
          	connect(p[i],i);
          	++cycles;
          }
     }

set<int> s;
for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
      g[find(i)].push_back(i);
    }
vector<int> ans;
for(auto &k : s)
   {
       ans.push_back(k);
   }
   /*for(int i = 0 ; i < ans.size() ; i++)
    {
    	 int k = ans[i];
    	 if(k != p[k])
    	 {
    	 	 for(auto &v : g[k])
    	 	  {
    	 	  	 if(v == p[v])
    	 	  	    {
    	 	  	    	ans[i] = v;
    	 	  	    	break;
    	 	  	    }
    	 	  }
    	 }
    }*/
    s.clear();
  for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
    }
    if(s.size() == 1)
      {
      	 cout << cycles << '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p[i] << ' ';
        return 0;
      }
int root = ans[0];
for(int i = 1 ; i < ans.size()  ; i++)
    {
         p[ans[i]] = root;
    }
    cout << (int)ans.size()-1<< '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p[i] << ' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];
vector<int> g[MAXN];
int find(int u)
{
   if(u == par[u])
        return u;
   return find(par[u]);
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        {
            par[b] = a;
        }
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = i;
}

int main()
{

int n;
cin >> n;
init();
int cycles = 0;
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        if(p[i] == i)
           continue;
       if(find(p[i]) != find(i)) connect(p[i],i);
       else
          {
          	p[i] = i;
          	connect(p[i],i);
          	++cycles;
          }
     }

set<int> s;
for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
      g[find(i)].push_back(i);
    }
vector<int> ans;
for(auto &k : s)
   {
       ans.push_back(k);
   }
   /*for(int i = 0 ; i < ans.size() ; i++)
    {
    	 int k = ans[i];
    	 if(k != p[k])
    	 {
    	 	 for(auto &v : g[k])
    	 	  {
    	 	  	 if(v == p[v])
    	 	  	    {
    	 	  	    	ans[i] = v;
    	 	  	    	break;
    	 	  	    }
    	 	  }
    	 }
    }*/
int root = ans[0];
for(int i = 1 ; i < ans.size()  ; i++)
    {
         p[ans[i]] = root;
    }
    cout << max((int)ans.size()-1,cycles) << '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p[i] << ' ';

}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int p[MAXN];
int par[MAXN];
int find(int u)
{
	if(u == par[u])
	     return u;

    return par[u] = find(par[u]);
}

void connect(int u,int v)
{
	par[find(v)] = find(u);
}

int main() {
	int n;
	cin >> n;
	int root = -1;
	for(int i = 1 ; i <= n ; i++)
	  {
	  	par[i] = i;
	  	cin >> p[i];
	  	if(p[i] == i)
	  	  root = i;
	  }
	  int ans = 0;
	  for(int i = 1 ; i <= n ; i++)
	  {
	  	   if(find(i) == find(p[i]))
	  	   {

	  	   	   if(root == -1)
	  	   	      {
	  	   	      	  root = find(i);
	  	   	      }

	  	   	   ans+=p[i] != root;
	  	   	      p[i] = root;
	  	   }
	  	   connect(i,p[i]);
	  }
	  cout << ans << '\n';
	  for(int i = 1 ; i <= n ; i++)
	     cout << p[i] << ' ';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];
int par2[MAXN];
int p2[MAXN];
vector<int> cycle;
int find(int u)
{
   if(u == par[u])
        return u;
   return find(par[u]);
}

int find2(int u)
{
   if(u == par2[u])
        return u;
   return find2(par2[u]);
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        {
            par[b] = a;
        }
}

void connect2(int u,int v)
{
   int a = find2(u);
   int b = find2(v);
   if(a != b)
        {
            par2[b] = a;
        }
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = par2[i] = i;
}

int main()
{

int n;
cin >> n;
init();
int cycles = 0;
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        if(p[i] == i)
           continue;
       if(find(p[i]) != find(i)) connect(p[i],i);
       else
          {
          	cycle.push_back(i);
          	++cycles;
          }
     }
  set<int> s;
  for(int i = 1 ; i <= n ; i++)
     s.insert(find(i));
  for(int i = 1 ; i <= n ; i++)
   {
  // 	cout << i << ' ' << find(i) << '\n';
   }
  if(s.size() == 1 and cycles == 1)
     {
     	p[cycle.back()] = cycle.back();
     	cout << 1 << '\n';
       for(int i = 1 ; i <= n ; i++)
    cout << p[i] << ' ';
     	return 0;
     }
   if(cycles)
     --cycles;
  int ans = s.size() - 1 + cycles;
  cout << ans << '\n';
  for(auto &k : cycle)
   {
   	if(cycles > 1)
   	 {
   	 	p[k] = k;
   	 	--cycles;
   	 }
   }
   vector<int> link;
   for(auto &k : s)
       link.push_back(k);
  int root = link[0];
  for(int i = 1 ; i < link.size() ; i++)
          p[link[i]] = root;
  for(int i = 1 ; i <= n ; i++)
    cout << p[i] << ' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];
vector<int> g[MAXN];
int find(int u)
{
   if(u == par[u])
        return u;
   return find(par[u]);
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        {
            par[b] = a;
        }
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = i;
}

int main()
{

int n;
cin >> n;
init();
int cycles = 0;
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        if(p[i] == find(i))
           continue;
       if(find(p[i]) != find(i)) connect(p[i],i);
       else
          {
          	p[i] = i;
          	connect(p[i],i);
          	++cycles;
          }
     }

set<int> s;
for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
      g[find(i)].push_back(i);
    }
vector<int> ans;
for(auto &k : s)
   {
       ans.push_back(k);
   }
   /*for(int i = 0 ; i < ans.size() ; i++)
    {
    	 int k = ans[i];
    	 if(k != p[k])
    	 {
    	 	 for(auto &v : g[k])
    	 	  {
    	 	  	 if(v == p[v])
    	 	  	    {
    	 	  	    	ans[i] = v;
    	 	  	    	break;
    	 	  	    }
    	 	  }
    	 }
    }*/
int root = ans[0];
for(int i = 1 ; i < ans.size()  ; i++)
    {
         p[ans[i]] = root;
    }
    cout << ans.size() - 1 + (cycles) << '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p[i] << ' ';

}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int p[MAXN];
int par[MAXN];
int find(int u)
{
	if(u == par[u])
	     return u;

    return par[u] = find(par[u]);
}

void connect(int u,int v)
{
	par[find(v)] = find(u);
}

int main() {
	int n;
	cin >> n;
	int root = -1;
	for(int i = 1 ; i <= n ; i++)
	  {
	  	par[i] = i;
	  	cin >> p[i];
	  	if(p[i] == i)
	  	  root = i;
	  }
	  int ans = 0;
	  for(int i = 1 ; i <= n ; i++)
	  {
	  	   if(find(i) == find(p[i]))
	  	   {

	  	   	   if(root == -1)
	  	   	      {
	  	   	      	  root = find(i);
	  	   	      }

	  	   	   ans+=p[i] != root;
	  	   	      p[i] = root;
	  	   }
	  	   connect(p[i],i);
	  }
	  cout << ans << '\n';
	  for(int i = 1 ; i <= n ; i++)
	     cout << p[i] << ' ';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];

int find(int u)
{
   if(u == par[u])
        return u;
   return (par[u] = find(par[u]));
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        par[b] = a;
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = i;
}

int main()
{

int n;
cin >> n;
init();
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        connect(p[i],i);
     }

set<int> s;
for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
    }
vector<int> ans;
for(auto &k : s)
   {
       ans.push_back(k);
   }

for(int i = 0 ; i < ans.size() - 1 ; i++)
    {
         p[ans[i]] = ans[i + 1];
    }
    cout << ans.size() - 1 << '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p[i] << ' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];
vector<int> g[MAXN];
int find(int u)
{
   if(u == par[u])
        return u;
   return find(par[u]);
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        {
            par[b] = a;
        }
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = i;
}

int main()
{

int n;
cin >> n;
init();
int cycles = 0;
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        if(p[i] == i)
           continue;
       if(find(p[i]) != find(i)) connect(p[i],i);
       else
          {
          	p[i] = i;
          	connect(p[i],i);
          	++cycles;
          }
     }

set<int> s;
for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
      g[find(i)].push_back(i);
    }
vector<int> ans;
for(auto &k : s)
   {
       ans.push_back(k);
   }
   for(int i = 0 ; i < ans.size() ; i++)
    {
    	 int k = ans[i];
    	 if(k != p[k])
    	 {
    	 	 for(auto &v : g[k])
    	 	  {
    	 	  	 if(v == p[v])
    	 	  	    {
    	 	  	    	ans[i] = v;
    	 	  	    	break;
    	 	  	    }
    	 	  }
    	 }
    }
int root = ans[0];
for(int i = 1 ; i < ans.size()  ; i++)
    {
         p[ans[i]] = root;
    }
    cout << ans.size() - 1 + (cycles) << '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p[i] << ' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];
int par2[MAXN];
int p2[MAXN];
vector<int> g[MAXN];
int find(int u)
{
   if(u == par[u])
        return u;
   return find(par[u]);
}

int find2(int u)
{
   if(u == par2[u])
        return u;
   return find2(par2[u]);
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        {
            par[b] = a;
        }
}

void connect2(int u,int v)
{
   int a = find2(u);
   int b = find2(v);
   if(a != b)
        {
            par2[b] = a;
        }
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = par2[i] = i;
}

int main()
{

int n;
cin >> n;
init();
int cycles = 0;
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        p2[i] = p[i];
                connect2(p2[i],i);
        if(p[i] == i)
           continue;
       if(find(p[i]) != find(i)) connect(p[i],i);
       else
          {
          	p[i] = i;
          	++cycles;
          }
     }

set<int> s;
for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
      g[find(i)].push_back(i);
    }

   /*for(int i = 0 ; i < ans.size() ; i++)
    {
    	 int k = ans[i];
    	 if(k != p[k])
    	 {
    	 	 for(auto &v : g[k])
    	 	  {
    	 	  	 if(v == p[v])
    	 	  	    {
    	 	  	    	ans[i] = v;
    	 	  	    	break;
    	 	  	    }
    	 	  }
    	 }
    }*/
    s.clear();
  for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
    }
    if(s.size() == 1)
      {
      	 cout << cycles << '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p[i] << ' ';
        return 0;
      }
        s.clear();
  for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find2(i));
    }
    vector<int> ans;
for(auto &k : s)
   {
       ans.push_back(k);
   }
int root = ans[0];
for(int i = 1 ; i < ans.size()  ; i++)
    {
         p2[ans[i]] = root;
    }
    cout << (int)ans.size()-1<< '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p2[i] << ' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];
vector<int> g[MAXN];
int find(int u)
{
   if(u == par[u])
        return u;
   return find(par[u]);
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        {
            par[b] = a;
        }
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = i;
}

int main()
{

int n;
cin >> n;
init();
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        connect(p[i],i);
     }

set<int> s;
for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
      g[find(i)].push_back(i);
    }
vector<int> ans;
for(auto &k : s)
   {
       ans.push_back(k);
   }
   for(int i = 0 ; i < ans.size() ; i++)
    {
    	 int k = ans[i];
    	 if(k != p[k])
    	 {
    	 	 for(auto &v : g[k])
    	 	  {
    	 	  	 if(v == p[v])
    	 	  	    {
    	 	  	    	ans[i] = v;
    	 	  	    	break;
    	 	  	    }
    	 	  }
    	 }
    }
int root = ans[0];
for(int i = 1 ; i < ans.size()  ; i++)
    {
         p[ans[i]] = root;
    }
    cout << ans.size() - 1 << '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p[i] << ' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

int p[MAXN];
int par[MAXN];

int find(int u)
{
   if(u == par[u])
        return u;
   return find(par[u]);
}

void connect(int u,int v)
{
   int a = find(u);
   int b = find(v);
   if(a != b)
        par[b] = a;
}

void init()
{
   for(int i = 1 ; i < MAXN ; i++)
        par[i] = i;
}

int main()
{

int n;
cin >> n;
init();
for(int i = 1 ; i <= n ; i++)
     {
        cin >> p[i];
        connect(p[i],i);
     }

set<int> s;
for(int i = 1 ; i <= n ; i++)
    {
      s.insert(find(i));
    }
vector<int> ans;
for(auto &k : s)
   {
       ans.push_back(k);
   }
int root = ans[0];
for(int i = 1 ; i < ans.size()  ; i++)
    {
         p[ans[i]] = root;
    }
    cout << ans.size() - 1 << '\n';
    for(int i = 1 ; i <= n ; i++)
        cout << p[i] << ' ';

}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200005;

int a[MAXN];
vector<int> par(MAXN);

int find(int n)
{
    return n == par[n] ? n : par[n] = find(par[n]);
}

int main()
{
    iota(par.begin(), par.end(), 0);
    int n;
    scanf("%i", &n);
    vector<int> roots;
    vector<int> bad_nodes;
    for (int x = 1; x <= n; x++)
    {
        scanf("%i", &a[x]);
        if (x == a[x])
        {
            roots.push_back(x);
            continue;
        }
        int aa = find(x);
        int bb = find(a[x]);
        if (aa == bb)
            bad_nodes.push_back(x);
        else
            par[aa] = bb;
    }
    printf("%i\n", (int)(bad_nodes.size() + max((int)roots.size() - 1, 0)));
    int aa = -1, bb = -1;
    for (int x = 1; x <= n; x++)
    {
        int u = find(x);
        if (aa == -1)
            aa = u;
        else if (aa != u)
            bb = u;
    }
    for (int x = 1; x < roots.size(); x++)
        a[roots[x]] = aa;
    for (auto &v : bad_nodes)
    {
        int u = find(v);
        if (u == aa)
            a[v] = bb;
        else
            a[v] = aa;
    }
    if (roots.empty())
        a[bad_nodes[0]] = bad_nodes[0];
    for (int x = 1; x <= n; x++)
        printf("%i ", a[x]);
    printf("\n");
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200005;

int a[MAXN];
vector<int> par(MAXN);

int find(int n)
{
    return n == par[n] ? n : par[n] = find(par[n]);
}

int main()
{
    iota(par.begin(), par.end(), 0);
    int n;
    scanf("%i", &n);
    vector<int> roots;
    vector<int> bad_nodes;
    for (int x = 1; x <= n; x++)
    {
        scanf("%i", &a[x]);
        if (x == a[x])
        {
            roots.push_back(x);
            continue;
        }
        int aa = find(x);
        int bb = find(a[x]);
        if (aa == bb)
            bad_nodes.push_back(x);
        else
            par[aa] = bb;
    }
    printf("%i\n", (int)(bad_nodes.size() + max((int)roots.size() - 1, 0)));
    for (int x = 1; x < bad_nodes.size(); x++)
        a[bad_nodes[x]] = par[find(bad_nodes[x])] = find(bad_nodes[x-1]);
    for (int x = 1; x < roots.size(); x++)
        a[roots[x]] = par[find(roots[x])] = find(roots[x-1]);
    if (!roots.empty())
    {
        if (!bad_nodes.empty())
            a[bad_nodes[0]] = roots[0];
    }
    else if (!bad_nodes.empty())
        a[bad_nodes[0]] = bad_nodes[0];
    for (int x = 1; x <= n; x++)
        printf("%i ", a[x]);
    printf("\n");
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200005;

int a[MAXN];
vector<int> par(MAXN);

int find(int n)
{
    return n == par[n] ? n : par[n] = find(par[n]);
}

int main()
{
    iota(par.begin(), par.end(), 0);
    int n;
    scanf("%i", &n);
    vector<int> roots;
    vector<int> bad_nodes;
    for (int x = 1; x <= n; x++)
    {
        scanf("%i", &a[x]);
        if (x == a[x])
        {
            roots.push_back(x);
            continue;
        }
        int aa = find(x);
        int bb = find(a[x]);
        if (aa == bb)
            bad_nodes.push_back(x);
        else
            par[aa] = bb;
    }
    printf("%i\n", (int)(bad_nodes.size() + max((int)roots.size() - 1, 0)));
    for (int x = 1; x < bad_nodes.size(); x++)
        par[find(bad_nodes[x-1])] = find(bad_nodes[x]);
    for (int x = 1; x < roots.size(); x++)
        par[find(roots[x])] = find(roots[x-1]);
    if (!roots.empty())
    {
        if (!bad_nodes.empty())
            a[bad_nodes[0]] = roots[0];
    }
    else if (!bad_nodes.empty())
        a[bad_nodes[0]] = bad_nodes[0];
    for (int x = 1; x <= n; x++)
        printf("%i ", a[x]);
    printf("\n");
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200005;

int a[MAXN];
vector<int> par(MAXN);

int find(int n)
{
    return n == par[n] ? n : par[n] = find(par[n]);
}

int main()
{
    iota(par.begin(), par.end(), 0);
    int n;
    scanf("%i", &n);
    vector<int> roots;
    vector<int> bad_nodes;
    for (int x = 1; x <= n; x++)
    {
        scanf("%i", &a[x]);
        if (x == a[x])
        {
            roots.push_back(x);
            continue;
        }
        int aa = find(x);
        int bb = find(a[x]);
        if (aa == bb)
            bad_nodes.push_back(x);
        else
            par[aa] = bb;
    }
    printf("%i\n", (int)(bad_nodes.size() + max((int)roots.size() - 1, 0)));
    for (int x = 1; x < bad_nodes.size(); x++)
        a[bad_nodes[x-1]] = par[find(bad_nodes[x])] = find(bad_nodes[x-1]);
    for (int x = 1; x < roots.size(); x++)
        a[roots[x]] = par[find(roots[x])] = find(roots[x-1]);
    if (!roots.empty())
    {
        if (!bad_nodes.empty())
            a[bad_nodes[0]] = roots[0];
    }
    else if (!bad_nodes.empty())
        a[bad_nodes[0]] = bad_nodes[0];
    for (int x = 1; x <= n; x++)
        printf("%i ", a[x]);
    printf("\n");
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200005;

int a[MAXN];
vector<int> par(MAXN);

int find(int n)
{
    return n == par[n] ? n : par[n] = find(par[n]);
}

int main()
{
    iota(par.begin(), par.end(), 0);
    int n;
    scanf("%i", &n);
    vector<int> roots;
    vector<int> bad_nodes;
    for (int x = 1; x <= n; x++)
    {
        scanf("%i", &a[x]);
        if (x == a[x])
        {
            roots.push_back(x);
            continue;
        }
        int aa = find(x);
        int bb = find(a[x]);
        if (aa == bb)
            bad_nodes.push_back(x);
        else
            par[aa] = bb;
    }
    printf("%i\n", (int)(bad_nodes.size() + max((int)roots.size() - 1, 0)));
    for (int x = 1; x < bad_nodes.size(); x++)
        par[find(bad_nodes[x-1])] = find(bad_nodes[x]);
    for (int x = 1; x < roots.size(); x++)
        par[find(roots[x])] = find(roots[x-1]);
    if (!roots.empty())
        a[bad_nodes[0]] = roots[0];
    else
        a[bad_nodes[0]] = bad_nodes[0];
    for (int x = 1; x <= n; x++)
        printf("%i ", a[x]);
    printf("\n");
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200005;

int a[MAXN];
vector<int> par(MAXN);

int find(int n)
{
    return n == par[n] ? n : par[n] = find(par[n]);
}

int main()
{
    iota(par.begin(), par.end(), 0);
    int n;
    scanf("%i", &n);
    vector<int> roots;
    vector<int> bad_nodes;
    for (int x = 1; x <= n; x++)
    {
        scanf("%i", &a[x]);
        if (x == a[x])
        {
            roots.push_back(x);
            continue;
        }
        int aa = find(x);
        int bb = find(a[x]);
        if (aa == bb)
            bad_nodes.push_back(x);
        else
            par[aa] = bb;
    }
    auto b = a;
    printf("%i\n", (int)(bad_nodes.size() + max((int)roots.size() - 1, 0)));
    for (int x = 1; x < bad_nodes.size(); x++)
        a[bad_nodes[x-1]] = par[find(bad_nodes[x])] = find(bad_nodes[x-1]);
    for (int x = 1; x < roots.size(); x++)
        a[roots[x]] = par[find(roots[x])] = find(roots[x-1]);
    if (!roots.empty())
    {
        if (!bad_nodes.empty())
            a[bad_nodes[0]] = roots[0];
    }
    else if (!bad_nodes.empty())
        a[bad_nodes[0]] = bad_nodes[0];
    int cnt = 0;
    for (int x = 0; x <= n; x++)
        cnt += a[x] != b[x];
    if (n == 1234)
        return 0 * printf("%i\n", cnt);
    for (int x = 1; x <= n; x++)
        printf("%i ", a[x]);
    printf("\n");
}


/****************************** بسم الله الرحمن الرحيم ************************/
/*---------------------------------------------------------------------*/
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define ordered_set tree<pair<int, int>, null_type,less<pair<int,int> >, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define compare bool operator <
#define main int main()
#define sf(n) scanf ("%d", &n)
#define pf(n) printf ("%d", n)
#define sfl(n) scanf ("%lld", &n)
#define pfl(n) printf ("%lld", n)
#define pfnewline() printf("\n")
#define pfspace() printf(" ")
#define mem(x,y) memset( (x), (y), sizeof(x) )
#define max3(a,b,c)   max(a , max(b,c) )
#define min3(a,b,c)  min(a, min(b,c) )
#define Ex 2.718281828
#define pi acos(-1.0)
#define toDecimal(n) fixed<< setprecision(n)
#define rep1(i, n) for(int i = 1; i<=n;i++)
#define rep(i, n) for(int i = 0; i<n;i++)
#define sfc(ch) scanf("%c",&ch)
#define sfs(ch) scanf("%[^\n]%*c", ch);

const ll mxn = 1e5+3;

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

/** ---4 moves--**/
//int dx[] = {-1,0,0,1};
//int dy[] = {0,-1,1,0};

using namespace __gnu_pbds;
using namespace std;

//ll lcm(ll a, ll b)  return a/__gcd(a,b)*b;
//ll bigmod(int a, ll b) { if(b == 0) return 1% mod;    ll x = bigmod(a,b/2  );    x = (x * x) % mod;    if(b % 2 == 1) x = (x*a)% mod;   return x;}
string toString(int A) { stringstream ss;    ss<<A;    string tmp;    ss>>tmp;    return tmp;}
vector<int>graph[200005];
bool visited[200005];
int pr[200005];
int cnt = 0;
vector<int>bhit;
void dfs(int u)
{
    visited[u] = true;
    for(int i = 0; i<graph[u].size();i++)
    {
        int v = graph[u][i];
        if(!visited[v])
        {
            pr[v] = u;
            dfs(v);
        }
        else
        {cnt++;
        //cout << "YES " << v << endl;
        }
    }
}

main
{
    int n;
    cin >> n;
    rep1(i,n)
    {
        int k;
        cin >> k;
        if(i == k)
        {
            bhit.push_back(i);
        }
        else{
        graph[k].push_back(i);
        }
        pr[i]=k;

    }
    int nisi;
    for(int i = 0;i<bhit.size();i++)
    {
        if(!visited[bhit[i]])
        {
            nisi = bhit[i];
            dfs( bhit[i] );
        }
    }
    for(int i = 1;i<=n;i++)
    {
        if(!visited[i])
        {
            if( bhit.size() ==0 )
            {
                nisi = i;
            }
            dfs(i);
        }
    }
    for(int i = 1; i<=n;i++)
    {
        if( pr[i] == i && i !=nisi )
        {
            cnt++;
            pr[i] = nisi;
        }
    }
    cout << cnt << endl;
    rep1(i,n)
    {
        cout << pr[i] << " ";
    }
    cout << endl;
}


/****************************** بسم الله الرحمن الرحيم ************************/
/*---------------------------------------------------------------------*/
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define ordered_set tree<pair<int, int>, null_type,less<pair<int,int> >, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define compare bool operator <
#define main int main()
#define sf(n) scanf ("%d", &n)
#define pf(n) printf ("%d", n)
#define sfl(n) scanf ("%lld", &n)
#define pfl(n) printf ("%lld", n)
#define pfnewline() printf("\n")
#define pfspace() printf(" ")
#define mem(x,y) memset( (x), (y), sizeof(x) )
#define max3(a,b,c)   max(a , max(b,c) )
#define min3(a,b,c)  min(a, min(b,c) )
#define Ex 2.718281828
#define pi acos(-1.0)
#define toDecimal(n) fixed<< setprecision(n)
#define rep1(i, n) for(int i = 1; i<=n;i++)
#define rep(i, n) for(int i = 0; i<n;i++)
#define sfc(ch) scanf("%c",&ch)
#define sfs(ch) scanf("%[^\n]%*c", ch);

const ll mxn = 1e5+3;

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

/** ---4 moves--**/
//int dx[] = {-1,0,0,1};
//int dy[] = {0,-1,1,0};

using namespace __gnu_pbds;
using namespace std;

//ll lcm(ll a, ll b)  return a/__gcd(a,b)*b;
//ll bigmod(int a, ll b) { if(b == 0) return 1% mod;    ll x = bigmod(a,b/2  );    x = (x * x) % mod;    if(b % 2 == 1) x = (x*a)% mod;   return x;}
string toString(int A)
{
    stringstream ss;
    ss<<A;
    string tmp;
    ss>>tmp;
    return tmp;
}
int a[200005];
int copya[200005];
int p[200005];

int findp(int e)
{
    if(p[e] == e )
    {
        return e;
    }
    else return findp(p[e]);
}

main
{
    int n;
    cin >> n;
    rep1(i,n)
    {
        cin >> a[i];
        p[i] = i; //making n different set
        copya[i] = a[i];
    }

    int root = -1;
    for(int i = 1; i<=n;i++)
    {
        if( findp(i) != findp(a[i]) )
        {
            p[findp(i)] = findp(a[i]);
        }
        else if( findp(i) == findp(a[i]) )
        {
            a[i] = 0; // found cycle. marking the parent of ith index as the root
            if( a[i] == i && root == -1)
            {
                root = i;
            }
        }
    }
    for(int i = 1; i<=n;i++)
    {
        if(root == -1)
        {
            if( a[i] == 0 )
            {
                root = i;  ///If no self root found, then the place where the cycle stopped is considered to be the root
                break;
            }
        }
    }
    for(int i = 1; i<=n;i++)
    {
        if( a[i] == 0 )
        {
            a[i] = root; /// The places where the cycle stopped, connecting that place with the root
        }
    }
    int ans = 0;
     for(int i =1; i<=n;i++)
     {
         if( a[i] !=copya[i] )
         {
             ans++;
         }
     }
     cout << ans << endl;
     for(int i =1 ; i<= n;i++)
     {
         cout << a[i] << " ";
     }
     cout << endl;
}