#include<bits/stdc++.h>
using namespace std;
long long n,m,c,sx,sy,zx,zy;
char a[505][505];
int fx[4]={0,1,0,-1};
int fy[4]={1,0,-1,0};
struct Node{
	int x,y,step;
};
queue<Node> q;
//找到出口E   现在起点是S  。是空地,#是墙
//智能上下左右移动。移动每一个单位距离需要耗费1个时间
//  12345
//1 #####
//2 #####
//3 #####
//4 ####E
//5 #####
//
//深搜:有一条路,一直走到黑,走不动了又退回来 
//广搜: 一开始有几条路,就登记,然后一个级别一个级别的走
//
//我习近平
//
//做核酸 
//1.建队列
//2.顶点入队列
//3.叫儿子排队		每次看轮到谁,就遍历他的儿子 
//4.自己出列 
//
//行	列	步数 
//2   2   0       
//2   3	1	
//3	2	1
//2	4	2
//3	3	2	
//4	2	2 
//3	4	3
//4	3	3
//4	4	4	<---
//4   5	5
int main(){
	scanf("%lld%lld%lld",&n,&m,&c);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
			if(a[i][j]=='S') sx=i,sy=j;
			if(a[i][j]=='E') zx=i,zy=j;
		}
	}
	q.push({sx,sy,0});
	a[sx][sy]='#';
	while(!q.empty()){
		int x=q.front().x;
		int y=q.front().y;
		int step=q.front().step;
		for(int i=0;i<=3;i++){
			int tx=x+fx[i];
			int ty=y+fy[i];
			if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&a[tx][ty]!='#'){
				a[tx][ty]='#';
				q.push({tx,ty,step+1});
				if(tx==zx&&ty==zy) {
					cout<<(step+1)*c; 
					return 0;
				}
			}
		}
		q.pop();
	} 
	cout<<-1;

}

/**************************************************************
	Problem: 2109
	User: admin
	Language: C++
	Result: Accepted
	Time:27 ms
	Memory:2328 kb
****************************************************************/