#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N = (1<<16)+5;
const int INF = 0x3f3f3f3f;
char s[52][52];
int n,m,T;
struct Node {
int x,y;
Node(int a=0,int b=0) {
x=a;
y=b;
}
} st,ed;
vector<Node>g;
int d[18][18],dis[51][51],f[N][16];
int dx[4]= {0,0,-1,1};
int dy[4]= {-1,1,0,0};
void bfs(int pos) {
queue<Node>q;
memset(dis,INF,sizeof(dis));
dis[g[pos].x][g[pos].y]=0;
q.push(g[pos]);
while(!q.empty()) {
Node u=q.front();
q.pop();
for(int i=0; i<4; ++i) {
int x=u.x+dx[i],y=u.y+dy[i];
if(x<1||x>n||y<1||y>m||s[x][y]==‘#‘)continue;
if(dis[x][y]==INF) {
dis[x][y]=dis[u.x][u.y]+1;
q.push(Node(x,y));
}
}
}
for(int i=0; i<g.size(); ++i)if(i!=pos) {
d[pos][i]=dis[g[i].x][g[i].y];
}
}
int main() {
scanf("%d%d%d",&n,&m,&T);
for(int i=1; i<=n; ++i)scanf("%s",s[i]+1);
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j) {
if(s[i][j]==‘K‘)st=Node(i,j);
else if(s[i][j]==‘Q‘)ed=Node(i,j);
else if(s[i][j]==‘G‘)g.push_back(Node(i,j));
}
}
g.push_back(st);
g.push_back(ed);
memset(d,INF,sizeof(d));
memset(f,INF,sizeof(f));
for(int i=0; i<g.size()-1; ++i)bfs(i);
int ss=g.size()-2,tt=g.size()-1,tot=(1<<(g.size()-2));
for(int i=0; i<g.size()-2; ++i) {
f[1<<i][i]=d[ss][i];
}
for(int i=1; i<tot; ++i) {
for(int j=0; j<g.size()-2; ++j) {
if(!(i&(1<<j))||f[i][j]>=INF)continue;
for(int k=0; k<g.size()-2; ++k) {
if((i&(1<<k))||d[j][k]>=INF)continue;
f[i|(1<<k)][k]=min(f[i][j]+(__builtin_popcount(i)+1)*d[j][k],f[i|(1<<k)][k]);
}
}
}
int ret=0;
for(int i=1; i<tot; ++i) {
for(int j=0; j<g.size()-2; ++j) {
if(f[i][j]!=INF||d[j][tt]!=INF) {
int tmp=f[i][j]+(__builtin_popcount(i)+1)*d[j][tt];
if(tmp<=T) ret=max(ret,__builtin_popcount(i));
}
}
}
printf("%d\n",ret);
return 0;
}