Lawnmower (Small)
InterviewTime limit5sMemory limit512 MB
Decide if row and column cuts of a mower that only lowers grass can produce the given target heights from a uniform lawn.
Problem
Alice and Bob have a lawn in front of their house, an metre by metre rectangle divided into 1m x 1m squares. Every square starts with grass 100 millimetres high.
Their new lawnmower has a height setting. Set it to any integer height between 1 and 100 millimetres and it cuts every square it passes over whose grass is higher than down to height , leaving squares that are already at most untouched. You run it by entering the lawn at any point on its edge; the mower then goes in a straight line perpendicular to that edge, cutting a swath 1 metre wide, until it exits on the other side. One run therefore cuts exactly one whole row or one whole column. The height can be changed only while the mower is off the lawn, so a single run uses a single height, but you may make as many runs as you want, in any order, at any heights.
You are given a pattern that states the desired height of the grass on every square. Decide whether some sequence of runs turns the lawn into exactly that pattern.
Input
The first line holds the number of test cases . Each test case begins with a line holding two integers and . The next lines follow, and the -th of them holds integers , the desired height of the grass on the -th square of the -th row.
Limits
Output
For each test case, print one line in the form Case #x: y, where is the case number starting from 1 and is YES if the pattern can be produced with the lawnmower and NO if it cannot.