Solve SRM 146, Div 2, Problem RectangularGrid