generalized to any number of rows, columns