# Remove surrounding zeroes of a 2d array

This is a 2-dimensional version of this question.

Given a non-empty 2-dimensional array/matrix containing only non-negative integers:

$$\begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & 0 & 1 & 0 \\ {\color{Red}0} & {\color{Red}0} & 0 & 0 & 1 \\ {\color{Red}0} & {\color{Red}0} & 1 & 1 & 1 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix}$$

Output the array with surrounding zeroes removed, i.e. the largest contiguous subarray without surrounding zeroes:

$$\begin{bmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 1 & 1 \end{bmatrix}$$

## Examples:

$$\begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & 0 & 1 & 0 \\ {\color{Red}0} & {\color{Red}0} & 0 & 0 & 1 \\ {\color{Red}0} & {\color{Red}0} & 1 & 1 & 1 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 1 & 1 \end{bmatrix}$$

Input:
[[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 0, 1, 1, 1], [0, 0, 0, 0, 0]]

Output:
[[0, 1, 0], [0, 0, 1], [1, 1, 1]]


$$\begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & 0 & 0 & 3 \\ {\color{Red}0} & 0 & 0 & 0 \\ {\color{Red}0} & 5 & 0 & 0 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 0 & 0 & 3 \\ 0 & 0 & 0 \\ 5 & 0 & 0 \end{bmatrix}$$

Input:
[[0, 0, 0, 0], [0, 0, 0, 3], [0, 0, 0, 0], [0, 5, 0, 0], [0, 0, 0, 0]]

Output:
[[0, 0, 3], [0, 0, 0], [5, 0, 0]]


$$\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \mapsto \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}$$

Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]


$$\begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} \end{bmatrix}$$

Input:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Output:
[]


$$\begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ 1 & 1 & 1 & 1 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 1 & 1 & 1 & 1 \end{bmatrix}$$

Input:
[[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0]]

Output:
[[1, 1, 1, 1]]


$$\begin{bmatrix} {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}$$

Input:
[[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]]

Output:
[[1], [1], [1]]


$$\begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 1 \\ 1 & 1 & 1 & 1 \end{bmatrix} \mapsto \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 1 \\ 1 & 1 & 1 & 1 \end{bmatrix}$$

Input:
[[1, 1, 1, 1], [1, 2, 3, 1], [1, 1, 1, 1]]

Output:
[[1, 1, 1, 1], [1, 2, 3, 1], [1, 1, 1, 1]]

• @MattH Nothing is difficult in a non-esoteric language. :) Just difficult to make it short. Commented Jul 27, 2018 at 16:20
• Can we give a falsey output instead of an empty matrix, for the last test case? Commented Jul 27, 2018 at 18:21
• Also, if output can be a non-square matrix, please add a test case for that. Commented Jul 27, 2018 at 18:44
• A test case that broke my earlier submission: [[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0]] (the result having a width/height of 1) Commented Jul 27, 2018 at 20:24
• Hey, is it possible to add the test case $$\begin{bmatrix}1&1&1&1\\1&2&3&1\\1&1&1&1\end{bmatrix}$$ Commented Jul 28, 2018 at 10:35

# 05AB1E (legacy), 13 bytes

2Fζ2FRDv¬O_i¦


Explanation:

2F                    # Loop two times:
ζ                   #  Zip/transpose; swapping rows/columns
#  (takes the input-matrix implicitly in the first iteration)
2F                 #  Inner loop two times:
R                #  Reverse the rows
Dv              #  Inner loop over the rows:
¬             #   Get the first row (without popping the matrix)
O_i          #   If the row consists only of 0s:
¦         #    Remove this first row from the matrix
# (output the result implicitly)

• The 2nd test case should output [[0, 0, 3], [0, 0, 0], [5, 0, 0]]. Commented Oct 28, 2018 at 21:58
• @Shaggy Almost 5 months later, but it's fixed now (and 1 byte smaller in the process). xD Commented Feb 12, 2019 at 14:08

# Attache, 40 bytes

Fixpoint[{Flip!_[N[0=Sum!_@0]...#_]}//4]


Try it online! Same business as below, just a bit smarter, splitting the process into four steps instead of two.

## Alternatives

47 bytes: Fixpoint[{Reverse=>Tr!_[N[0=Sum!_@0]...#_]}//4]

48 bytes: Fixpoint[{MatrixRotate!_[N[0=Sum!_@0]...#_]}//4]

## Attache, 68 bytes

Fixpoint[{n.=Dim@_@-1Tr[{_@1!in~-n'0or Sum!_@0}\Enumerate@_<:0]}//2]


Try it online!

Twice: This removes any first or last row whose sum is 0, then transposes the array. Then, this process is repeated until the result does not change.

# JavaScript (Node.js), 100 bytes

f=(a,i=4,b=a[0])=>b&&i?/[1-9]/.test(b)?f([...b].map((c,j)=>a.map(d=>d.pop())),i-1):f(a.slice(1),i):a


Try it online!