Refactored to allow only equal sized grids (width==height)