APL (Dyalog Extended), 48 bytes (SBCS)
Anonymous tacit prefix function. Takes Boolean (0/1) matrix with 0s indicating the shape. Requires 0-based indexing.
{(=/⍵-⍥⍴e)∧i≡,e←(⊂∘⊃+∘⍳∘|1+⊃∘⌽-⊃)i←⍸⍵}0⍉⍤⌂deb⍣2⊢1⍉⍤⌂deb⍣2⊢
0...⊢ with 0 as left argument and the unmodified argument as right argument:
...⍣2 repeat twice
...⍤⌂debdelete ending (leading and trailing) blanks (1s), then:
⍉ transpose (so we end up deleting the top and bottom blank rows too)
{...} apply the following lambda, with ⍵ representing its argument:
⍸⍵indices where there are 1s
i← save as i
(...) apply the following tacit function to that:
⊃ the [index of the] first [blank]
...- subtract that from:
⊃∘ the [index of the] first of the:
⌽ reversed [indices of blanks, i.e. the index of the last blank]
1+ increment that [to get inclusive range]
∘| take the absolute value of that [getting the absolute range], then:
∘⍳ get the indices of that range, then:
...+ add that to:
⊂∘ the whole of...
⊃ [index of the] first [blank, giving a matrix of indices of the entire supposed blank area]
e← save as e
, ravel into a list of indices
i≡ identical to the actual list of indices of blanks?
(...)∧ and...
⍵...e considering the argument and the matrix of supposed blanks...
-⍥ are the differences in...
⍴ shapes [between heights and widths]
=/ equal? [i.e. does the height-difference match the width-difference?]
APL (Dyalog Extended), 48 bytes (SBCS)
{(=/⍵-⍥⍴e)∧i≡,e←(⊂∘⊃+∘⍳∘|1+⊃∘⌽-⊃)i←⍸⍵}0⍉⍤⌂deb⍣2⊢
APL (Dyalog Extended), 48 bytes (SBCS)
Anonymous tacit prefix function. Takes Boolean (0/1) matrix with 0s indicating the shape. Requires 0-based indexing.
{(=/⍵-⍥⍴e)∧i≡,e←(⊂∘⊃+∘⍳∘|1+⊃∘⌽-⊃)i←⍸⍵}1⍉⍤⌂deb⍣2⊢
0...⊢ with 0 as left argument and the unmodified argument as right argument:
...⍣2 repeat twice
...⍤⌂debdelete ending (leading and trailing) blanks (1s), then:
⍉ transpose (so we end up deleting the top and bottom blank rows too)
{...} apply the following lambda, with ⍵ representing its argument:
⍸⍵indices where there are 1s
i← save as i
(...) apply the following tacit function to that:
⊃ the [index of the] first [blank]
...- subtract that from:
⊃∘ the [index of the] first of the:
⌽ reversed [indices of blanks, i.e. the index of the last blank]
1+ increment that [to get inclusive range]
∘| take the absolute value of that [getting the absolute range], then:
∘⍳ get the indices of that range, then:
...+ add that to:
⊂∘ the whole of...
⊃ [index of the] first [blank, giving a matrix of indices of the entire supposed blank area]
e← save as e
, ravel into a list of indices
i≡ identical to the actual list of indices of blanks?
(...)∧ and...
⍵...e considering the argument and the matrix of supposed blanks...
-⍥ are the differences in...
⍴ shapes [between heights and widths]
=/ equal? [i.e. does the height-difference match the width-difference?]
APL (Dyalog Extended), 48 bytes (SBCS)
{(=/⍵-⍥⍴e)∧i≡,e←(⊂∘⊃+∘⍳∘|1+⊃∘⌽-⊃)i←⍸⍵}0⍉⍤⌂deb⍣2⊢