You'll have to test every possible subrectangle, however you can start at the top-left and keep going left, so you can subtract the most left column of the subrectangle and add the new column each step. When you're done with the row, start with the next one.

The easiest way to do this is to write a program that takes a problem as the input and gives the source code of the solution... then you can use it to solve this problem.

Seriously though, what kind of algorithm? Don't say "any"!

string is included by iostream, so you can just omit the include and still use strings.

NP-complete commented: be 100% sure, atleast before advising. +0