#include<bits/stdc++.h>
using namespace std;
int N, M;
int board[105][105];
bool vis[105][105];
int dy[4] = {-1, 1, 0, 0};
int dx[4] = {0, 0, -1, 1};
int X, Y;
bool valid(int y, int x) {
    return 1<=y && y<=N && 1<=x && x<=M;
}

int main() {
    cin.tie(nullptr); ios::sync_with_stdio(false);
    cin >> N >> M >> Y >> X;
    for(int i=1;i<=N;++i) {
        string s; cin >> s;
        for(int j=1;j<=M;++j) {
            board[i][j] = s[j-1] == '#';
        }
    }
    int ans = 0;
    for(int i=0;i<4;++i) {
        int y = Y;
        int x = X;
        while(valid(y, x) && board[y][x] == 0) {
            if(!vis[y][x]) {
                vis[y][x] = true;
                ++ans;
            }
            y += dy[i];
            x += dx[i];
        }
    }
    cout << ans << '\n';
    return 0;
}
#include<bits/stdc++.h> 
using namespace std;
using ll=long long int;
using Int=__int128;
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>b)return 0; a=b; return 1;}
#define bitUP(x,a) ((x>>a)&1)
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double EPS = 1e-6;
long double PI = acos(-1);
const ll INF=(1LL<<62);
const int MAX=(1<<30);
constexpr ll MOD=1000000000+7;

inline void bin101(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);
}

using pii=pair<int,int>;
using pil=pair<int,ll>;
using pli=pair<ll,int>;
using pll=pair<ll,ll>;
using psi=pair<string,int>;
using pis=pair<int,string>;
using psl=pair<string,ll>;
using pls=pair<ll,string>;
using pss=pair<string,string>;

using Graph=vector<vector<int>>;

template<typename T >
struct edge {
	int to;
	T cost;
	edge()=default;
	edge(int to, T cost) : to(to), cost(cost) {}

};
template<typename T>
using WeightGraph=vector<vector<edge<T>>>;


//1-indexed vector cin
template<typename T>
inline void vin1(vector<T> &v){
	for(size_t i=1;i<v.size();i++) cin>>v[i];
}
 
//0-indexed vector cin
template<typename T>
inline void vin0(vector<T> &v){
    for(size_t i=0;i<v.size();i++) cin>>v[i];
}
 
//1-indexed vector<vector> cin
template<typename T>
inline void vin1(vector<vector<T>> &v){
    for(size_t i=1;i<v.size();i++){
        for(size_t j=1;j<v[i].size();j++) cin>>v[i][j];
    }
}
 
//0-indexed vector<vector> cin
template<typename T>
inline void vin0(vector<vector<T>> &v){
    for(size_t i=0;i<v.size();i++){
        for(size_t j=0;j<v[i].size();j++) cin>>v[i][j];
    }
}

//要素数n 初期値x
template<typename T>
inline vector<T> vmake(size_t n,T x){
	return vector<T>(n,x);
}

//a,b,c,x data[a][b][c] 初期値x
template<typename... Args>
auto vmake(size_t n,Args... args){
	auto v=vmake(args...);
	return vector<decltype(v)>(n,move(v));
}

//pair cout
template<typename T, typename U>
inline ostream &operator<<(ostream &os,const pair<T,U> &p) {
	os<<p.first<<" "<<p.second;
	return os;
}
 
//pair cin
template<typename T, typename U>
inline istream &operator>>(istream &is,pair<T,U> &p) {
	is>>p.first>>p.second;
	return is;
}
 
//ソート
template<typename T>
inline void vsort(vector<T> &v){
    sort(v.begin(),v.end());
}
 
//逆順ソート
template<typename T>
inline void rvsort(vector<T> &v){
	sort(v.rbegin(),v.rend());
}

//1ビットの数を返す
inline int popcount(int x){
	return __builtin_popcount(x);
}
//1ビットの数を返す
inline int popcount(ll x){
	return __builtin_popcountll(x);
}

ll modpow(ll x,ll N,ll mod){
    ll ret=1;
    while(N>0){
        if(bitUP(N,0)) ret=ret*x%mod;
        x=x*x%mod;
        N>>=1;
    }
    return ret;
}


void solve(){
	ll A,B,C;
	cin>>A>>B>>C;
	vector<int> v;
	v.push_back(A%10);
	bool fi=true;
	while(not (fi==false and v.back()==v[0])){
		v.push_back(v.back()*A%10);
		fi=false;
	}
	v.pop_back();
	ll size=v.size();
	ll x=modpow(B,C,size);
	x--;
	if(x==-1) x=size-1;
	cout<<v[x]<<endl;
}

int main(){
    bin101();
    int T=1;
    //cin>>T;
    while(T--) solve();
}
