Here's my code : http://pastebin.com/WPfr9ZX4
Initially all columns and rows have N items each. After item is placed on the grid I subtract 1 from all the columns and rows and update the result accordingly. On test 7 I get TLE. How can I make this faster?