Write a program or function that takes in a positive integer \$N\$ and a grid of decimal digits (\$0\$ to \$9\$) with width \$W\$ and height \$H\$ (which are also positive integers). You can assume that \$N\$ will be less than or equal to the larger of \$W\$ and \$H\$ (\$N \le \max(W,H)\$).
Print or return the largest contiguous \$N\$-digit number that appears horizontally or vertically in the grid, written in normal reading order or in reverse.
- Diagonal lines of digits are not considered.
- The grid does not wrap around, i.e. it does not have periodic boundary conditions.
For example, the \$3\times3\$ grid
928
313
049
would have 9
as the output for \$N = 1\$, 94
as the output for \$N = 2\$, and 940
as the output for \$N = 3\$.
The \$4\times3\$ grid
7423
1531
6810
would have 8
as the output for \$N = 1\$, 86
for \$N = 2\$, 854
for \$N = 3\$, and 7423
for \$N = 4\$.
The \$3\times3\$ grid
000
010
000
would have output 1
for \$N = 1\$, and 10
for N = 2 and N = 3 (010
is also valid for N = 3).
The \$1\times1\$ grid
0
would have output 0
for \$N = 1\$.
You can take the input in any convenient reasonable format. e.g. the grid could be a newline separated string of digits, or a multidimensional array, or a list of lists of digits, etc. Leading zeros are allowed in the output if they were part of the grid.
This is code-golf, so the shortest code in bytes wins, but I'll also award brownie points (i.e. more likely upvotes) for answers that can show that their algorithm is computationally efficient.