import java.io.*;
import java.util.*;
class node {
int x, y, k;
node(int x, int y, int k) {
this.x = x;
this.y = y;
this.k = k;
}
}
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
String[] input = reader.readLine().trim().split(" ");
int n = Integer.parseInt(input[0]);
int k = Integer.parseInt(input[1]);
char[][] map = new char[n + 1][n + 1];
for (int i = 0; i < n; i++) map[i] = reader.readLine().trim().toCharArray();
// 三个状态:走到的格子、以及到达该格子的无敌步数
boolean[][][] vis = new boolean[n + 1][n + 1][k + 1];
int[] x = new int[] {-1,1,0,0};
int[] y = new int[] {0,0,-1,1};
vis[0][0][0] = true;
Queue<node> queue = new LinkedList<>();
queue.offer(new node(0, 0, 0)); // 起点没有无敌
int cnt = 0;
boolean flag = false;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
node cur = queue.poll();
// 找到终点
if (cur.x == n - 1 && cur.y == n - 1) {
System.out.println(cnt);
return;
}
for (int j = 0; j < 4; j++) {
int tx = cur.x + x[j];
int ty = cur.y + y[j];
// 目前有的无敌步数,注意除了墙壁不能走,陷阱和普通路都会消耗无敌步数
int kk = cur.k;
if (tx < 0 || ty < 0 || tx >= n || ty >= n) continue;
// 如果当前状态已经走过,或者是墙壁,都不能走(无论是否无敌)
if (map[tx][ty] == '#') continue;
if (map[tx][ty] == 'X') {
// 陷阱,无敌状态可以走
if (kk > 0) {
if (vis[tx][ty][kk - 1]) continue;
queue.offer(new node(tx, ty, kk - 1));
vis[tx][ty][kk - 1] = true;
}
} else if (map[tx][ty] == '%') {
if (vis[tx][ty][kk > 0 ? kk - 1 : 0]) continue;
// 变无敌,获得k步
queue.offer(new node(tx, ty, k));
vis[tx][ty][kk > 0 ? kk - 1 : 0] = true;
// 拿了之后就不能再拿
map[tx][ty] = '.';
} else {
if (vis[tx][ty][kk > 0 ? kk - 1 : 0]) continue;
queue.offer(new node(tx, ty, kk > 0 ? kk - 1 : 0));
vis[tx][ty][kk > 0 ? kk - 1 : 0] = true;
}
}
}
// 整个完成一轮,步数++
cnt++;
}
System.out.println(-1);
}
}