# How do I compute the convolution of two kernels of the same size in practice?

Suppose I have a 256-by-256 input matrix called $$X$$ and two 3-by-3 kernels called $$K_1$$ and $$K_2$$. By the associativity of convolution

$$$$(X \star K_1) \star K_2 = X \star (K_1 \star K_2)$$$$

I would like to calculate $$K = K_1 \star K_2$$ first and then form $$X \star K$$, because that is computationally cheaper. The problem is: how do I calculate $$K_1 \star K_2$$ in practice? I assume it will be a 5-by-5 kernel, but how do I handle the boundary where some corresponding elements don't exist?