works for any size matrix