Square Block Detection
&quot;Image processing method&quot; here really boils down to ... &quot;for&quot; loops.
You'd walk the main diagonal (or the first non-main diagonal next to it), and as soon as you hit a value, you count the values right of that aren't 0. Soon as you know how many these are, you're done. Half that number, add it to the number of the row, and tadah, center of square.
To deal with overlap is trivial: once you've reached the end of a row go one element further and then you go up until you hit either the diagonal or another non-zero value.
Note that you say &quot;sparse&quot; matrix: depending on the matrix storage format, the &quot;how many entries are set in this row&quot; might already be how the whole thing is stored, making this even easier.