Added row and column check to the isLegal method.