#include <iostream>
#include <vector>
int main() {
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int>> matrix(n, std::vector<int>(n));
int deadCells = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cin >> matrix[i][j];
}
}
for (int t = 0; t < m; t++) {
std::vector<std::vector<int>> tempMatrix = matrix;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] > 0) {
matrix[i][j]--;
} else if (matrix[i][j] == 0) {
if (i > 0 && matrix[i - 1][j] > 0) {
tempMatrix[i - 1][j] = 0;
deadCells++;
}
if (i < n - 1 && matrix[i + 1][j] > 0) {
tempMatrix[i + 1][j] = 0;
deadCells++;
}
if (j > 0 && matrix[i][j - 1] > 0) {
tempMatrix[i][j - 1] = 0;
deadCells++;
}
if (j < n - 1 && matrix[i][j + 1] > 0) {
tempMatrix[i][j + 1] = 0;
deadCells++;
}
}
}
}
matrix = tempMatrix;
}
std::cout << deadCells << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKaW50IG1haW4oKSB7CiAgICBpbnQgbiwgbTsKICAgIHN0ZDo6Y2luID4+IG4gPj4gbTsKCiAgICBzdGQ6OnZlY3RvcjxzdGQ6OnZlY3RvcjxpbnQ+PiBtYXRyaXgobiwgc3RkOjp2ZWN0b3I8aW50PihuKSk7CiAgICBpbnQgZGVhZENlbGxzID0gMDsKCiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgIHN0ZDo6Y2luID4+IG1hdHJpeFtpXVtqXTsKICAgICAgICB9CiAgICB9CgogICAgZm9yIChpbnQgdCA9IDA7IHQgPCBtOyB0KyspIHsKICAgICAgICBzdGQ6OnZlY3RvcjxzdGQ6OnZlY3RvcjxpbnQ+PiB0ZW1wTWF0cml4ID0gbWF0cml4OwoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG47IGorKykgewogICAgICAgICAgICAgICAgaWYgKG1hdHJpeFtpXVtqXSA+IDApIHsKICAgICAgICAgICAgICAgICAgICBtYXRyaXhbaV1bal0tLTsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAobWF0cml4W2ldW2pdID09IDApIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaSA+IDAgJiYgbWF0cml4W2kgLSAxXVtqXSA+IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgdGVtcE1hdHJpeFtpIC0gMV1bal0gPSAwOwogICAgICAgICAgICAgICAgICAgICAgICBkZWFkQ2VsbHMrKzsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaWYgKGkgPCBuIC0gMSAmJiBtYXRyaXhbaSArIDFdW2pdID4gMCkgewogICAgICAgICAgICAgICAgICAgICAgICB0ZW1wTWF0cml4W2kgKyAxXVtqXSA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgIGRlYWRDZWxscysrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoaiA+IDAgJiYgbWF0cml4W2ldW2ogLSAxXSA+IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgdGVtcE1hdHJpeFtpXVtqIC0gMV0gPSAwOwogICAgICAgICAgICAgICAgICAgICAgICBkZWFkQ2VsbHMrKzsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaWYgKGogPCBuIC0gMSAmJiBtYXRyaXhbaV1baiArIDFdID4gMCkgewogICAgICAgICAgICAgICAgICAgICAgICB0ZW1wTWF0cml4W2ldW2ogKyAxXSA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgIGRlYWRDZWxscysrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgbWF0cml4ID0gdGVtcE1hdHJpeDsKICAgIH0KCiAgICBzdGQ6OmNvdXQgPDwgZGVhZENlbGxzIDw8IHN0ZDo6ZW5kbDsKCiAgICByZXR1cm4gMDsKfQoK