Chop off the matrix to get the desired sum Definition
Given a matrix \$M\$ of non-negative integers and a non-negative integer \$k\$, we define \$F_k\$ as the &quot;chop-off&quot; function that removes all rows and all columns in \$M\$ that contain \$k\$.
Example:
$$\begin{align}M=\pmatrix{\color{red}6&amp;\color{red}1&amp;\color{white}{\bbox[red,1pt]{5}}\\1&amp;2&amp;\color{red}8\\\color{red}9&amp;\color{red}8&amp;\color{white}{\bbox[red,1pt]{5}}\\6&amp;0&amp;\color{red}4}\\\\F_5(M)=\pmatrix{1&amp;2\\6&amp;0}\end{align}$$
Your task
Given \$M\$ and a target sum \$S\$, your task is to find all possible values of \$k\$ such that the sum of the remaining elements in \$F_k(M)\$ is equal to \$S\$.
Example:
Given the above matrix \$M\$ and \$S=9\$:

\$k=5\$ is a solution, because \$F_5(M)=\pmatrix{1&amp;2\\6&amp;0}\$ and \$1+2+6+0=9\$
\$k=1\$ is the only other possible solution: \$F_1(M)=\pmatrix{5\\4}\$ and \$5+4=9\$

So the expected output would be \$\{1,5\}\$.
Clarifications and rules

The input is guaranteed to admit at least one solution.
The sum of the elements in the original matrix is guaranteed to be greater than \$S\$.
You may assume \$S&gt;0\$. It means that an empty matrix will never lead to a solution.
The values of \$k\$ may be printed or returned in any order and in any reasonable, unambiguous format.
You are allowed not to deduplicate the output (e.g. \$[1,1,5,5]\$ or \$[1,5,1,5]\$ are considered valid answers for the above example).
This is code-golf.

Test cases
M = [[6,1,5],[1,2,8],[9,8,5],[6,0,4]]
S = 9
Solution = {1,5}

M = [[7,2],[1,4]]
S = 7
Solution = {4}

M = [[12,5,2,3],[17,11,18,8]]
S = 43
Solution = {5}

M = [[7,12],[10,5],[0,13]]
S = 17
Solution = {0,13}

M = [[1,1,0,1],[2,0,0,2],[2,0,1,0]]
S = 1
Solution = {2}

M = [[57,8,33,84],[84,78,19,14],[43,14,81,30]]
S = 236
Solution = {19,43,57}

M = [[2,5,8],[3,5,8],[10,8,5],[10,6,7],[10,6,4]]
S = 49
Solution = {2,3,4,7}

M = [[5,4,0],[3,0,4],[8,2,2]]
S = 8
Solution = {0,2,3,4,5,8}
K (ngn/k), 39 bytes

{a@&amp;y=x{+//x*(&amp;/'b)&amp;\:&amp;/b:~x=y}/:a:,/x}


Try it online!

thanks @Adám for this explanation:

{…} function, x is M and y is S

 ,/x flatten M (these are the k candidates)

 a: assign to a

 x{…}/: apply the following function to each while using M as fixed left argument (x):

  x=y Boolean matrix indicating where elements of M equal the current k candidate

  ~ negate that 

  b: assign that to b

  &amp;/ AND reduction (finds columns without that k)

  (…)&amp;\: AND that with each of the following:

   &amp;/'b AND reduction of each (finds rows without that k)

  x* multiply M by that

  +// grand sum

 y= list of Booleans indicating where S equals those sums

 &amp; indices of Trues

 a@ use that to index into the elements (the k candidates)