I don't know withing SUMIF (or COUNTIF) how to approach it
you could consider creating an output row or column that solves the IF
CLAUSE or condition wiht a 1 for true and 0 for false. In that same formula
or another resulting column / row you could multiply to target cell value
with that 0 or 1. You now SUM the result row/column you just created
This would give you the output
You could hide the in between columns/rows if need be
Look at SUMPRODUCT which allows summation of multiple conditions across a
(any) number of columns. Perhaps an example of what you want would enable a
fuller reply.