Solution to Chef and Squares challenge, timing out in Java but not in C++
My first observation was the same as @EmilyL.'s: that you're performing unneeded string concatenations.  Upon investigation, however, it turned out to be a loser to substitute two invocations of System.out.print() methods for a string concatenation plus one method invocation -- the result ran about 70% slower than the original code for me.

It took a while for the lightbulb to turn on, but that slowdown is a key symptom of the underlying problem: System.out is unbuffered, at least when it's not connected to a terminal.  In a comment you described addressing the problem by building the output in a StringBuilder and then printing it all at once.  That's a viable mechanism for buffering manually, but cleaner and easier to integrate into your original solution would have been to wrap a buffered stream around System.out and print to that.  In other words, at the top of main() add ...

PrintStream out = new PrintStream(new BufferedOutputStream(System.out));


... and everywhere else print to out instead of to System.out().  Doing that cut my run time by more than 50% relative to the original code, for a maximal-size test set.