J, 75 72 71 70 68 66 bytes
+/@,@;@((-:&(/:~)"1(;/-#:i.4)&{&>)#])&,[:g@|:&>g=.[:;@,<@(1}.<\)\.
This takes all submatrices, filters them based on whether their 4 corners sorted matches the target sorted, and returns the sum.
The two parts that required the most experimentation to golf were:
(;/-#:i.4)&{
The phrase to pull the 4 corners from a matrix. It generates the numbers 0 through 3 in binary, negates the ones, and uses those as a 2d index for "take"{
:0 0
top left corner0 _1
top right corner_1 0
bottom left corner_1 _1
bottom right corner
[:g@|:&>g=.[:;@,<@(1}.<\)\.
The phrase to generate all submatrixes. It has two parts:<@(1}.<\)\.
First we generate all possible heights for valid full width rectangles. We do this by calculating all prefixes of all suffixes, deleting the first element of each prefix list, because it has dimension 1.[:;@,
is a technicality required to keep the list flat. Note that we also save this phrase asg
...[:g@|:&>
We now reapplyg
to the transpose of every result from the first step, generating all the possible widths for each possible height. Together, these step generate all possible widths for every possible height rectangle, accounting for all possible submatrices.