There are O(n 2 ) cells associated with cases 1 and 2, each of which has a total amount of work bounded by a constant, hence the total amount of work associated with the cells not in O is also O(n 2 ).
the overall worst-case complexity of the algorithm under these conditions is O(n 2 ).