#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <queue>
#include <ctime>
using namespace std;
template <class T> void checkmin(T &t,T x){if (x < t) t = x;}
template <class T> void checkmax(T &t,T x){if (x > t) t = x;}
template <class T> void _checkmin(T &t, T x){if (t == -1) t = x; if (x < t) t = x;}
template <class T> void _checkmax(T &t, T x){if (t == -1) t = x; if (x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long lld;
#define foreach(it,v) for (__typeof((v).begin()) it = (v).begin();it != (v).end();it++)
#define DEBUG(a) cout << #a" = " << (a) << endl;
#define DEBUGARR(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
const int N = 1005;
const int px[4] = {-1, 0, 1, 0};
const int py[4] = {0, 1, 0, -1};
int m, n, Mod;
int sx, sy, sd;
int ex, ey;
int d[N][N][4];
int f[N][N][4];
struct Node {
	int x, y, d;
	Node(int x, int y, int d):x(x),y(y),d(d){}
};
char mat[N][N];

void push(queue <Node> &q, int x, int y, int _d, int pd, int pf) {
		if (x < 0 || x >= m || y < 0 || y >= n || mat[x][y] == '*') return;
		if (d[x][y][_d] == -1) {
			d[x][y][_d] = pd;
			f[x][y][_d] = pf;
			q.push(Node(x, y, _d));
		} else if (d[x][y][_d] == pd) {
			f[x][y][_d] += pf;
			f[x][y][_d] %= Mod;
		}
}

void bfs() {
	memset(d, 0xFF, sizeof(d));
	memset(f, 0, sizeof(f));
	d[sx][sy][sd] = 0;
	f[sx][sy][sd] = 1;
	queue <Node> q;
	q.push(Node(sx, sy, sd));
	while (!q.empty()) {
		Node x = q.front();
		q.pop();
		int myd = d[x.x][x.y][x.d];
		int myf = f[x.x][x.y][x.d];
		int _d = (x.d + 1) % 4;
		push(q, x.x, x.y, (x.d + 1) % 4, myd + 1, myf);
		push(q, x.x, x.y, (x.d + 3) % 4, myd + 1, myf);
		push(q, x.x + px[x.d], x.y + py[x.d], x.d, myd + 1, myf);
	}
}

int main(){
#ifdef cwj
	freopen("in", "r", stdin);
#endif
	int Tc = 0;
	while (scanf("%d%d%d", &m, &n, &Mod) && Mod) {
		for (int i = 0; i < m; i++)
			scanf("%s", mat[i]);
		char dd;
		scanf("%d%d%d%d %c", &sx, &sy, &ex, &ey, &dd);
		if (dd == 'N') sd = 0;
		if (dd == 'E') sd = 1;
		if (dd == 'S') sd = 2;
		if (dd == 'W') sd = 3;
		bfs();
		printf("Case %d: %d ", ++Tc, Mod);
		if (d[ex][ey][0] == -1) {
			puts("-1");
		} else {
			int Min = 0x7FFFFFFF;
			for (int i = 0; i < 4; i++) checkmin(Min, d[ex][ey][i]);
			int ans = 0;
			for (int i = 0; i < 4; i++) {
				if (d[ex][ey][i] == Min) {
					ans += f[ex][ey][i];
					ans %= Mod;
				}
			}
			printf("%d\n", ans);
		}
	}
}
