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

int main(){
    int i, j, k;
    int rows, cols;
    int **mat;

    scanf("%d %d", &rows, &cols);

    mat = (int **)malloc(sizeof(int *) * rows);
    if(mat == NULL){
        printf("ERROR: Failed to allocate memory for rows.\n");
        return 0;
    }

    for(i = 0; i < rows; i++){
        mat[i] = (int *)malloc(sizeof(int) * cols);
        if(mat[i] == NULL){
            printf("ERROR: Failed to allocate memory for columns at row %d.\n", i);
            return 0;
        }
    }

    k = rows * cols;
    for(i = 0; i < rows; i++){
        for(j = 0; j < cols; j++){
            mat[i][j] = k--;
        }
    }

    for(i = 0; i < rows; i++){
        for(j = 0; j < cols; j++){
            printf("%3d ", mat[i][j]);
        }
        printf("\n");
    }

    for(i = 0; i < rows; i++){
        free(mat[i]);
    }
    free(mat);

    printf("Memory freed and program ended.\n");
    return 0;
}