import java.util.Scanner;

/**
 * ISC Computer Science Practical Examination
 * Topic: 2D Arrays (Matrix Boundary Rotation)
 * Class: MatrixBoundaryRotation
 */
public class MatrixBoundaryRotation {

    // Helper method to check if a number is prime
    public static boolean isPrime(int num) {
        if (num <= 1) return false;
        if (num == 2 || num == 3) return true;
        if (num % 2 == 0 || num % 3 == 0) return false;
        for (int i = 5; i * i <= num; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }

    // Helper method to display a matrix
    public static void displayMatrix(int[][] mat, int m, int n) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%4d", mat[i][j]);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        // Input dimensions
        System.out.print("M = ");
        int m = in.nextInt();
        System.out.print("N = ");
        int n = in.nextInt();

        // 1. Validate matrix dimensions (2 < M < 10 and 2 < N < 10)
        if (m <= 2 || m >= 10 || n <= 2 || n >= 10) {
            System.out.println("INVALID SIZE");
            return;
        }

        // Input matrix elements
        int[][] mat = new int[m][n];
        System.out.println("ENTER ELEMENTS:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                mat[i][j] = in.nextInt();
                if (mat[i][j] <= 0) {
                    System.out.println("INVALID ELEMENT: Must be positive integer");
                    return;
                }
            }
        }

        // Input rotation parameter K
        System.out.print("K = ");
        int k = in.nextInt();

        int boundaryLength = 2 * (m + n) - 4;
        // 2. Validate K (1 <= K < 2(M + N) - 4)
        if (k < 1 || k >= boundaryLength) {
            System.out.println("INVALID VALUE OF K");
            return;
        }

        // Display Original Matrix
        System.out.println("\nORIGINAL MATRIX");
        displayMatrix(mat, m, n);

        // 3. Extract Boundary into 1D Array (Clockwise Order)
        int[] boundary = new int[boundaryLength];
        int idx = 0;

        // Top Row (Left to Right): [0][0] to [0][N-1]
        for (int j = 0; j < n; j++) {
            boundary[idx++] = mat[0][j];
        }
        // Right Column (Top to Bottom): [1][N-1] to [M-1][N-1]
        for (int i = 1; i < m; i++) {
            boundary[idx++] = mat[i][n - 1];
        }
        // Bottom Row (Right to Left): [M-1][N-2] down to [M-1][0]
        for (int j = n - 2; j >= 0; j--) {
            boundary[idx++] = mat[m - 1][j];
        }
        // Left Column (Bottom to Top): [M-2][0] down to [1][0]
        for (int i = m - 2; i >= 1; i--) {
            boundary[idx++] = mat[i][0];
        }

        // 4. Rotate 1D Array Clockwise by K positions
        // Clockwise shift: an element at index i moves to (i + K) % boundaryLength
        int[] rotatedBoundary = new int[boundaryLength];
        for (int i = 0; i < boundaryLength; i++) {
            rotatedBoundary[(i + k) % boundaryLength] = boundary[i];
        }

        // 5. Create Rotated Matrix (Copy original inner elements, replace boundary)
        int[][] rotatedMat = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                rotatedMat[i][j] = mat[i][j];
            }
        }

        // Re-inject rotated boundary in the same clockwise order
        idx = 0;
        // Top Row
        for (int j = 0; j < n; j++) {
            rotatedMat[0][j] = rotatedBoundary[idx++];
        }
        // Right Column
        for (int i = 1; i < m; i++) {
            rotatedMat[i][n - 1] = rotatedBoundary[idx++];
        }
        // Bottom Row
        for (int j = n - 2; j >= 0; j--) {
            rotatedMat[m - 1][j] = rotatedBoundary[idx++];
        }
        // Left Column
        for (int i = m - 2; i >= 1; i--) {
            rotatedMat[i][0] = rotatedBoundary[idx++];
        }

        // Display Rotated Matrix
        System.out.println("\nMATRIX AFTER ROTATING THE BOUNDARY BY " + k);
        displayMatrix(rotatedMat, m, n);

        // 6. Check Corner Elements for Primes
        int[] corners = {
            rotatedMat[0][0],
            rotatedMat[0][n - 1],
            rotatedMat[m - 1][n - 1],
            rotatedMat[m - 1][0]
        };

        int primeCount = 0;
        for (int c : corners) {
            if (isPrime(c)) {
                primeCount++;
            }
        }

        System.out.println("\nNUMBER OF PRIME CORNERS: " + primeCount);
    }
}
