#include <stdio.h>
#include <stdlib.h>

int main()
{
    int N, K;

    scanf("%i %i",&N,&K);
    fflush(stdin);
    int farma [N][N][2];

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++){
            scanf("%i",&farma[i][j][0]);
            farma[i][j][1] = 0;
        }
    }
    int xpos,ypos,xmin,ymin;
    int num = 0;
    int res = 2;
    int dcheck;


for (int val = 1; val <= K; val++) {
        dcheck = 0;

        if (res == 1) {
            num++;
            res = 2;
        }
        else if (res == 0)
            res = 2;

        for (int y = 0; y < N && res != 0; y++) {

        for (int x = 0; x < N && res != 0; x++) {

    if (farma[x][y][1] == 1 || farma[x][y][0] != val)
        continue;

        xpos = x + 1;

        ypos = y + 1;

        ymin = y - 1;

        xmin = x - 1;

    if(dcheck == 1) {
    if ((farma[xmin][y][0] == val && farma[xmin][y][1] == 0) || (farma[xmin][ymin][0] == val && farma[xmin][ymin][1] == 0) || (farma[x][ymin][0] == val && farma[x][ymin][1] == 0) || ((farma[xpos][ymin][0] == val || (x == 0 && y == 0)) && farma[xpos][ymin][1] == 0)){
        res = 0;
        break;
        }
    }else
    dcheck = 1;

    //Top left
    if (x == 0 && y == 0) {
        topleft:

        if (farma[xpos][y][0] == val){
            farma[xpos][y][1] == 1;

            if (farma[x][ypos][0] == val){
                farma[x][ypos][1] == 1;

                if (farma[xpos][ypos][0] == val){
                    farma[xpos][ypos][1] == 1;
                    continue;
                }else{
                    res = 0;
                    break;
                }
            }else if (farma[xpos][ypos][0] != val){
                    res = 1;
                    continue;
                    }
        }else if (farma[xpos][ypos][0] != val){
            res = 1;
            continue;

        }else{
        res = 0;
        break;
        }
    //Bottom right
    }else if ( x == N - 1 && y == N - 1){
        bottomright:

        if (farma[x][ymin][0] == val) {
            farma[x][ymin][1] = 1;

            if (farma[xmin][y][0] == val) {
                farma[xmin][y][1] = 1;

                if (farma[xmin][ymin][0] == val) {
                    farma[xmin][ymin][1] = 1;
                    res = 1;
                    continue;

                }else{
                res = 0;
                break;
                }
            }else if (farma[xmin][ymin][0] != val){
            res = 1;
            continue;
            }
            else {
            res = 0;
            break;
            }
        }else if (farma[xmin][ymin][0] != val){
            if(farma[xmin][y][0] == val)
                farma[xmin][y][1] = 1;
            res = 1;
            continue;
        }else{
        res = 0;
        break;
        }

    //Bottom left
    }else if (x == 0 && y == N - 1) {
        bottomleft:

        if(farma[xpos][y][0] == val){
            farma[xpos][y][1] = 1;

            if (farma[x][ymin][0] == val && farma[xpos][ymin][0] == val) {
                farma[x][ymin][1] = 1;
                farma[xpos][ymin][1] = 1;
                res = 1;
                continue;
            }else if (farma[x][ymin][0] != val && farma[xpos][ymin][0] != val) {
                res = 1;
                continue;
            }else {
                res = 0;
                break;
            }
        }if (farma[x][ymin][0] == val && farma[xpos][ymin][0] != val){
            farma[x][ymin][1] = 1;
            continue;
        }else if (farma[x][ymin][0] != val && farma[xpos][ymin][0] != val){
            res = 1;
            continue;
        }else {
            res = 0;
            break;
        }
    //Top Right
    }else if (x == N - 1 && y == 0) {
        topright:

        if (farma[x][ypos][0] == val){
            farma[x][ypos][1] = 1;

            if (farma[xmin][ypos][0] == val && farma[xmin][y][0] == val){
                farma[xmin][ypos][1] = 1;
                farma[xmin][y][1] = 1;
                res = 1;
                continue;
            }else if (farma[xmin][ypos][0] != val && farma[xmin][y][0] != val) {
                res = 1;
                continue;
            }else {
                res = 0;
                break;
            }
        }else if (farma[xmin][ypos][0] != val && farma[xmin][y][0] == val) {
                farma[xmin][y][1] = 1;
                res = 1;
                continue;
        }else if (farma[xmin][ypos][0] != val) {
                farma[xmin][ypos][1] = 1;
                res = 1;
                continue;
        }else {
                res = 0;
                break;
        }
    //Top Side
    }else if (x == 0) {
        if (farma[xpos][y][0] == val){
            farma[xpos][y][1] = 1;

            if (farma[xmin][y][0] == val){
                farma[xmin][y][1] = 1;

                if (farma[xmin][ypos][0] == val && farma[xpos][ypos][0] == val && farma[x][ypos][0] == val){
                    farma[x][ypos][1] = 1;
                    farma[xmin][ypos][1] = 1;
                    farma[xpos][ypos][1] = 1;
                    res = 1;
                    continue;
                }else if (farma[xmin][ypos][0] != val && farma[xpos][ypos][0] != val && farma[x][ypos][0] != val){
                    res = 1;
                    continue;
                }else {
                    res = 0;
                    break;
                }
            }else if (farma[xmin][ypos][0] == val) {
                farma[xmin][ypos][1] == 1;
                goto topleft;
            }else {
                res = 0;
                break;
            }
    }else if (farma[xmin][y][0] == val && (farma[xmin][ypos][0] != val && farma[xpos][ypos][0] != val && farma[x][ypos][0] != val)){
        farma[xmin][y][1] = 1;
        res = 1;
        continue;
        }else if (farma[xpos][ypos][0] != val){
            goto topright;
        }else {
            res = 0;
            break;
        }

    //Bottom Side
    }else if (y == N - 1) {
        if (farma[xmin][y][0] == val) {
            farma[xmin][y][1] = 1;

            if (farma[xpos][y][0] == val){
                farma[xpos][y][1] = 1;

                if (farma[xmin][ymin][0] != val && farma[xpos][ymin][0] != val && farma[x][ymin][0] != val) {
                    res = 1;
                    continue;
                }else if (farma[xmin][ymin][0] == val && farma[xpos][ymin][0] == val && farma[x][ymin][0] == val) {
                    farma[xmin][ymin][1] = 1;
                    farma[xpos][ymin][1] = 1;
                    farma[x][ymin][0] = 1;
                    res = 1;
                    continue;
                }else {
                    res = 0;
                    break;
                    }

                }else if (farma[xpos][ymin][0] != val) {
                    goto bottomright;

                }else {
                    res = 0;
                    break;
                    }
        }else if (farma[xmin][ymin][0] != val) {
            goto bottomleft;

        }else {
            res = 0;
            break;
            }

    //Left Side
    }else if (x == 0) {

        if (farma[xpos][ypos][0] != val && farma[x][ypos][0] != val) {
            goto bottomleft;

        }else if (farma[xpos][ymin][0] != val && farma[x][ymin][0] != val) {
            goto topleft;

        }else if (farma[xpos][y][0] == val) {
            farma[xpos][y][1] = 1;


        }else if (farma[xpos][ymin][0] != val && farma[x][ymin][0] != val && farma[xpos][ypos][0] != val && farma[x][ypos][0] != val) {
            res = 1;
            continue;

        }else if (farma[xpos][ypos][0] != val && farma[x][ypos][0] == val && farma[xpos][ymin][0] != val && farma[x][ymin][0] == val && farma[xpos][y][0] != val) {
            farma[x][ypos][0] = 1;
            farma[x][ymin][0] = 1;
            res = 1;
            continue;

        }else if (farma[xpos][ypos][0] == val && farma[x][ypos][0] == val && farma[xpos][ymin][0] == val && farma[x][ymin][0] == val && farma[xpos][y][0] == val) {
            farma[xpos][ypos][0] = 1;
            farma[x][ypos][0] = 1;
            farma[xpos][ymin][0] = 1;
            farma[x][ymin][0] = 1;
            farma[xpos][y][0] = 1;
            res = 1;
            continue;
        }else {
            res = 0;
            break;
        }

    //Right Side
    }else if (x == N - 1) {

        if (farma[x][ypos][0] != val && farma[xmin][ypos][0] != val) {
            goto bottomright;

        }else if (farma[x][ymin][0] != val && farma[xmin][ymin][0] != val) {
            goto topright;

        }else if (farma[x][ymin][0] == val && farma[xmin][ymin][0] == val && farma[x][ypos][0] == val && farma[xmin][ypos][0] == val && farma[xmin][y][0] == val) {
            farma[x][ymin][1] = 1;
            farma[xmin][ymin][1] = 1;
            farma[x][ypos][1] = 1;
            farma[xmin][ypos][1] = 1;
            farma[xmin][y][1] = 1;
            res = 1;
            continue;

        }else if (farma[x][ymin][0] == val && farma[xmin][ymin][0] != val && farma[x][ypos][0] == val && farma[xmin][ypos][0] != val && farma[xmin][y][0] != val) {
            farma[x][ymin][1] = 1;
            farma[x][ypos][1] = 1;
            res = 1;
            continue;

        }else {
            res = 0;
            break;
            }
    }else {
    //RIGHT
    if (farma[xpos][y][0] == val){
        farma[xpos][y][1] = 1;

        if (farma[xpos][ymin][0] == val){
            farma[xpos][ymin][1] = 1;

            if (farma[x][ymin][0] == val) {
                farma[x][ymin][1] = 1;

                if (farma[xmin][ymin][0] == val) {
                    farma[xmin][ymin][1] = 1;

                    if (farma[xmin][y][0] == val) {
                        farma[xmin][y][1] = 1;

                        if (farma[x][ypos][0] == val && farma[xmin][ypos][0] == val && farma[xpos][ypos][0] == val) {
                            res = 1;
                            farma[x][ypos][1] = 1;
                            farma[xmin][ypos][1] = 1;
                            farma[xpos][ypos][1] = 1;
                            continue;

                        }
                        else {
                            res = 0;
                            break;
                            }
                    }else {
                        res = 0;
                        break;
                            }
                }else if (farma[x][ypos][0] == val && farma[xpos][y][0] == val) {
                    res = 1;
                    farma[x][ypos][1] = 1;
                    farma[xpos][y][1] = 1;
                    continue;

                }else if (farma[x][ypos][0] != val && farma[xpos][y][0] != val) {
                    res = 1;
                    continue;
                }
                else {
                    res = 0;
                    break;
                }
            }else {
                res = 0;
                break;
            }
        }else if (farma[x][ypos][0] != val && farma[xmin][ypos][0] != val && farma[xpos][ypos][0] != val && farma[x][ymin][0] != val && farma[xmin][ymin][0] != val) {
            res = 1;
            continue;
            }
        else {
            res = 0;
            break;
        }
    }
    //LEFT
    else if (farma[xmin][y][0] == val) {
            farma[xmin][y][1] = 1;

        if (farma[xmin][ymin][0] == val && farma[x][ymin][0] == val){
                if (farma[x][ypos][0] == val && farma[xmin][ypos][0] == val && farma[xpos][ymin][0] != val && farma[xpos][ypos][0] != val) {
                    farma[x][ypos][1] = 1;
                    farma[xmin][ypos][1] = 1;
                    res = 1;
                    continue;
                    }
        }else{
            res = 1;
            continue;
            }
            //1 X 1
            //0 Y 0
            //1 0 1
    }
    //DOWN
    else if (farma[x][ypos][0] == val){
            farma[x][ypos][1] = 1;

            if (farma[x][ymin][0] == val){
                farma[x][ymin][1] = 1;

                if (farma[xmin][ypos][0] != val && farma[xpos][ypos][0] != val && farma[xmin][ymin][0] != val && farma[xpos][ymin][0] != val) {
                    res = 1;
                    continue;
                    }
            }else {
                if (farma[xmin][ypos][0] != val && farma[xpos][ypos][0] != val && farma[xmin][ymin][0] != val && farma[xpos][ymin][0] != val) {
                    res = 1;
                    continue;
                    }else {
                        res = 0;
                        break;
                        }
        }
    }
    //UP
    else if (farma[x][ymin][0] == val) {
            farma[x][ymin][1] = 1;

            if (farma[xmin][ypos][0] != val && farma[xpos][ypos][0] != val && farma[xmin][ymin][0] != val && farma[xpos][ymin][0] != val){
                res = 1;
                continue;
                }
        }
    else if (farma[xmin][ypos][0] != val && farma[xpos][ypos][0] != val && farma[xmin][ymin][0] != val && farma[xpos][ymin][0] != val) {
    res = 1;
    continue;
    }else {
        res = 0;
    break;
        }
    }
    }
  }
}
    printf("%i",num);

    return 0;
}