#include <stdio.h>

template <class T, int rows, int cols>
class Matrix {
    T data[rows][cols];
public:
    Matrix();
    T& At(int row, int col) { return data[row][col]; }
    const T& At(int row, int col) const { return data[row][col]; }
    void Print(const char *fmt) const;
};

template<class T, int rows, int cols>
Matrix<T, rows, cols>::Matrix()
{
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            data[i][j] = 0;
}

template<class T, int a, int b, int c>
Matrix<T, a, c> operator*(const Matrix<T, a, b> &x, const Matrix<T, b, c> &y)
{
    Matrix<T, a, c> z;
    for (int i = 0; i < a; i++)
        for (int j = 0; j < c; j++)
            for (int k = 0; k < b; k++)
                z.At(i, j) += x.At(i, k) * y.At(k, j);
    return z;
}

template<class T, int rows, int cols>
void Matrix<T, rows, cols>::Print(const char *fmt) const
{
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            printf(fmt, data[i][j]);
        printf("\n");
    }
}

int main()
{
    int i, j, k;
    Matrix<int, 2, 3> x;
    Matrix<int, 3, 2> y;
    Matrix<int, 2, 2> z;
    Matrix<int, 3, 3> u;

    k = 1;
    for (i = 0; i < 2; i++)
        for (j = 0; j < 3; j++) {
            x.At(i, j) = k;
            k++;
        }

    for (i = 0; i < 3; i++)
        for (j = 0; j < 2; j++) {
            y.At(i, j) = k;
            k++;
        }

    z = x * y;
    z.Print("%3d ");

    u = y * x;
    u.Print("%3d ");

    // x * x;

    return 0;
}
