#include <iostream>

void fillSpiral(int m, int n, int matrix[100][100]) {
    int num = 1;
    int total = m * n;
    int i = 0, j = 0;
    int di = 0, dj = 1;

    while (num <= total) {
        matrix[i][j] = num++;
        if (i + di >= m || i + di < 0 || j + dj >= n || j + dj < 0 || matrix[i + di][j + dj] != 0) {
            int temp = di;
            di = dj;
            dj = -temp;
        }
        i += di;
        j += dj;
    }
}

int main() {
    int m = 4, n = 5;
    int matrix[100][100] = {0};
    fillSpiral(m, n, matrix);

    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            std::cout << matrix[i][j] << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

