#include <iostream>
#include <queue>
using namespace std;
const int MAX = 105;
struct state
{
int x, y, dis, remain;
state(int x, int y, int d, int r):
x(x), y(y), dis(d), remain(r) { }
};
int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, 1, 0, -1};
bool graph[MAX][MAX];
bool visit[MAX][MAX][MAX];
int m, n, d;
bool inGraph(int x, int y)
{
return x >= 0 && x < m && y >= 0 && y < n;
}
void bfs()
{
memset(visit, false, sizeof(visit));
queue<state> q;
q.push(state(0, 0, 0, d));
visit[0][0][d] = true;
while(!q.empty())
{
state temp = q.front(); q.pop();
if(temp.x == m-1 && temp.y == n-1)
{
cout << temp.dis << endl;
return;
}
for(int i = 0; i < 4; ++ i)
{
int nx = temp.x + mx[i];
int ny = temp.y + my[i];
if(inGraph(nx, ny) && graph[nx][ny] && !visit[nx][ny][temp.remain])
{
q.push(state(nx, ny, temp.dis+1, temp.remain));
visit[nx][ny][temp.remain] = true;
}
for(int j = 1; j < temp.remain; ++ j)
{
nx += mx[i];
ny += my[i];
if(inGraph(nx, ny) && graph[nx][ny] && !visit[nx][ny][temp.remain-j-1])
{
q.push(state(nx, ny, temp.dis+1, temp.remain-j-1));
visit[nx][ny][temp.remain-j-1] = true;
}
}
}
}
cout << "impossible" << endl;
}
int main()
{
freopen("data.in", "r", stdin);
char temp;
cin >> m >> n >> d;
memset(graph, false, sizeof(graph));
for(int i = 0; i < m; ++ i)
for(int j = 0; j < n; ++ j)
{
cin >> temp;
graph[i][j] = temp == 'P';
}
bfs();
return 0;
}