[Question]
  [
A [band matrix](https://en.wikipedia.org/wiki/Band_matrix) is a matrix whose non-zero entries fall within a diagonal *band*, consisting of the main diagonal and zero or more diagonals on either side of it. (The [main diagonal](https://en.wikipedia.org/wiki/Main_diagonal) of a matrix consists of all entries \$a\_{i,j}\$ for which \$i=j\$.) For this challenge, we will only be considering square matrices.
For example, given this matrix:
```
1 2 0 0
3 4 5 0
0 6 0 7
0 0 8 9
```
this is the band:
```
1 2
3 4 5
  6 0 7
    8 9
```
The main diagonal (`1 4 0 9`) and the diagonals above it (`2 5 7`) and below it (`3 6 8`) are the only places non-zero elements are found; all other elements are zero. (Some of the elements in the band may also be zero.)
The *bandwidth* of the matrix is the smallest number \$k\$ such that all non-zero elements are contained within a band consisting of the main diagonal, \$k\$ diagonals above it, and \$k\$ diagonals below it. The bandwidth of the above matrix is 1: all non-zero elements fall within the main diagonal, the one diagonal above it, or the one diagonal below it. For another example:
```
1 1 0 0
1 1 1 0
1 1 1 1
0 1 1 1
```
The bandwidth of this matrix is 2, because it takes two diagonals above and below the main diagonal to catch all non-zero elements:
```
1 1 0
1 1 1 0
1 1 1 1
  1 1 1
```
Note that for the purposes of this challenge, the band must extend the same distance on either side of the main diagonal, which is why the diagonal `0 0` is included above.
Mathematically, the bandwidth is the smallest number \$k\$ such that for every entry \$a\_{i,j}\$ in the matrix, \$a\_{i,j} = 0\$ if \$|i-j|>k\$.
## Challenge
Given a square matrix containing nonnegative integers, output its bandwidth.
The matrix will always have at least one nonzero entry.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"): make your code (measured in bytes) as short as possible.
## Test cases
```
1
=>
0
1 0 0
0 1 0
0 0 1
=>
0
1 2 0 0
3 4 5 0
0 6 0 7
0 0 8 9
=>
1
1 1 0 0
1 1 1 0
1 1 1 1
0 1 1 1
=>
2
16 18  8
 6 14 22
20 10 12
=>
2
0 0 0 1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
=>
3
```
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
ŒṪIAṀ
```
[Try it online!](https://tio.run/##y0rNyan8///opIc7V3k6PtzZ8P9w@6OmNf//R3NFRxvGxupwgWgdAx0DIFMh2kDHEM4CsuHyRnAVxjomOqZwNWZAcXO4egsdS7gOhJkgtiEK2xBulyHCDjMdQwsdC7AMkGmiY2QEZEcbAVUBkRFUFcgWVDcaILmdGF4sVywA "Jelly – Try It Online")
```
ŒṪ     -- indices of non-zero values
  I    -- reduce each index by subtraction
   A   -- get the absolute values
    Ṁ  -- select the maximum
```
[Answer]
# [R](https://www.r-project.org/), 38 bytes
```
function(A)max(abs(row(A)-col(A))*!!A)
```
[Try it online!](https://tio.run/##lU7LDsIgELzzFdsbazBhqfVx8NBPwRpME9smtMb@PS4UTUy9mCxhZlhmxgcHZwju0TdTO/Syxs7O0l5G6Ycns20z3PnCTVHUGJzs7OTbWZIiRCGcvLb2xqxcWH5tWAGjQMcRpYKdgiphVvZJPyTMc1RwQt5YG9DbYMH0jSkZZPzLgHOIzTlAxEziDsYoYeKveAyuWuucmLM@VP9LuU@FGF4 "R – Try It Online")
Test harness taken from [Robin Ryder's answer](https://codegolf.stackexchange.com/a/243456/67312).
R has [some weird built-ins](https://codegolf.stackexchange.com/a/219617/67312). Given a matrix `M`, `row` will return a matrix of the same size with each entry equal to its row number, and `col` likewise, but with columns. That is, \$row(M)\_{ij}=i\$ and \$col(M)\_{ij}=j\$. So `abs(row(A)-col(A))` gives us a matrix of possible bandwidths like so for a \$5\times 5\$ matrix:
```
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    1    2    3    4
[2,]    1    0    1    2    3
[3,]    2    1    0    1    2
[4,]    3    2    1    0    1
[5,]    4    3    2    1    0
```
Then we "filter" the entries where `A` is nonzero and take the maximum to obtain the bandwidth.
[Answer]
# [R](https://www.r-project.org/), ~~41~~ 40 bytes
-1 byte thanks to Giuseppe
```
function(A)max(diff(t(which(t(A)|A,T))))
```
[Try it online!](https://tio.run/##lVC7DsIwDNzzFR5jyUOdUh4DQ/@BH6iKQjO0SFURHfj3ckkDEoKFyFHuHOfulHHxdKTF34Z2CtfB1tw3sz0H7@1k711oO5w1P2o5Mdbibd9MY5itijIb4zHbXMDKleXbFh1yQkUsUwpthKqE0dmm/i5h1F7owJj4FtCXwIr1E2sSyPiXAHwU4jAw0VORwTkxLr6K2/FX6iI7Zq83Lf6lyFPhu54 "R – Try It Online")
Note that the matrix \$A|A^T\$ has the same bandwidth as the matrix \$A\$, but is symmetrical. We can therefore consider only its lower-triangular part, for which row index is greater than column index.
The formula given in the question is \$\max(|i-j|)\$ such that \$A\_{ij}\neq 0\$; this is equivalent to \$\max(i-j)\$ such that \$(A|A^T)\_{ij}\neq 0\$ (without the absolute value). The call `which(x,T)` returns a 2-column matrix listing the row and column indices of all `TRUE` values in `x`; we need to transpose the output since `diff` acts column-wise.
This saves 3 bytes compared to the more obvious strategy:
```
function(A)max(abs(which(!!A,T)%*%c(1,-1)))
```
Edit: [Outgolfed](https://codegolf.stackexchange.com/a/243509/86301) by Giuseppe's 38 byte answer.
[Answer]
# Python 3, 68 bytes
```
lambda a,e=enumerate:max(abs(i-j)for i,r in e(a)for j,c in e(r)if c)
```
[Try it online!](https://tio.run/##fY5BbsMgEEX3nGJ2gYpGgNPUjeRepK4q4mCFyMYIiNqe3mWwk@wqYcb/z5s/@N90nlxV@zD3TTsPejyeNGhuGuOuowk6mcOof6g@RmqfL6yfAliePweG6iIvvFtkYLaHjs1odjoadCdvHBVsG4w@UbaNfrCJblrXug07EICnMH1HDl8cgonXIUFTRh/ghmUKodz5GGxMdNSeWpc4uivHGAPcmh1civgnjt0iM04XgWk@oO4pYuy2mM2SNO9EECJBgCACZLlzvfuqdCrYwUvp7bN@LUwNb0hJpJZ5rPJeZcmTS5bK1B5kDVCTnCF3oBRRGchHrQCGPp4g1if995fnqj8 "Python 3.8 (pre-release) – Try It Online")
Finds the largest \$|i-j|\$ among all entries \$a\_{ij} \neq 0\$.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 13 bytes
```
Ｉ⌈ＥＡ⌈Ｅι∧λ↔⁻μκ
```
[Try it online!](https://tio.run/##TYnLCsIwFET3fsUsb@EKvh@4Kl25KLgPWcQqGEzS0iTi31@DYHE4cJiZ7mHGrjdO5DLakKgxMVFr3tZnXzzQOQw5UcX4Hy2jDjdyRdfYu5zu1NqQI3nGs/rlJKKUWjJWjEVB8wxQa8aGsZ16eXbffz/1woFx1FrL/OU@ "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
   Ａ            Input array
  Ｅ             Map over rows
      ι         Current row
     Ｅ          Map over elements
        λ       Current element
       ∧        Logical And
           μ    Column index
          ⁻     Subtract
            κ   Row index
         ↔      Absolute value
    ⌈           Take the maximum
 ⌈              Take the maximum
Ｉ               Cast to string
                Implicitly print
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~10~~ 6 bytes
```
&f-|X>
```
[Try it online!](https://tio.run/##y00syfmfoJCpUBLlp2CvoKcQq/BfLU23JsLuv0KEi0JUhUJI7H9DrmhDBQMFA2sgYQihgAxrhViQuBFExljBRMEUImcGxOYQRRYKllBlUAMMwRDOMIQYaYhkmrGCNdgoMyBtDjIAKG4AsRFmtQHMMXiZsVwG@gZcXFwA "MATL – Try It Online")
Port of @ovs's Jelly solution.
*(-4 bytes thanks to @LuisMendo)*
---
*Older*
### [MATL](https://github.com/lmendo/MATL), 16 bytes
```
&+t"tX@&Rs~?X@q.
```
[Try it out!](https://matl.suever.net/?code=%26%2Bt%22tX%40%26Rs%7E%3FX%40q.&inputs=%5B0+0+0+1+0%3B+0+0+0+0+0%3B+0+0+0+0+0%3B+0+0+0+0+0%3B+0+0+0+0+0%5D&version=22.4.0) [Check all cases](https://tio.run/##y00syfmfoJCpUBLlp2CvoKcQq/BfTbtEqSTCQS2ouM4@wqFQ739srEJ5hUKEi0JUhUJI7H9DrmhDBQMFA2sgYQihgAxrhViQuBFExljBRMEUImcGxOYQRRYKllBlUAMMwRDOMIQYaYhkmrGCNdgoMyBtDjIAKG4AsRFmtQHMMXiZsVwG@gZcXFwA "MATL – Try It Online")
Outputs a transformed version of the input (input + input's transpose) and then the bandwidth. Not sure if extraneous output like this is usually allowed; if not, the "cleaner" version is just 1 byte longer: `&+XH"HX@&Rs~?X@q.` [Try it out](https://matl.suever.net/?code=%26%2BXH%22HX%40%26Rs%7E%3FX%40q.&inputs=%5B0+0+0+1+0%3B+0+0+0+0+0%3B+0+0+0+0+0%3B+0+0+0+0+0%3B+0+0+0+0+0%5D&version=22.4.0)
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 50 bytes
```
a->m=0;matrix(#a,,i,j,a[i,j]&&m=max(m,abs(i-j)));m
```
[Try it online!](https://tio.run/##fU5RCsMgDL2KdFAUIqjrOoe0N9gJih/ZR4dlDun60Z3eqS3b3yDkveQlLwk4O34PcSRdRN77ThiPy@xWekAABxPgkLKta995XKkHvL2o4xNjzPiIITzeFAnvSZjdc0m0ykVFRoqMARmuuFCZiQQBwgiQJSe0palK@wgNnIrQpvpcBjRctpFtM6P8oixOcndpQWrQJkEDShmVtBQqa9npd1TsT/xh1rL4AQ "Pari/GP – Try It Online")
[Answer]
# [J](http://jsoftware.com/), ~~38~~ ~~31~~ 23 bytes
```
>./@,@(~:&0*|@-/~@i.@#)
```
-7 bytes by removing some useless parentheses  
-8 bytes, thanks @ovs (see comment below)
[Try it online!](https://tio.run/##jU7LCsJADLzvVwxarJV222wf1oKyKHgqHsQfkNKiXjzUo/TX17haoT1JEjLJDJPczES6DdYFXPiIUHAFErtjuTcbGWpfz7tiFi2eOgg7fZV66hlPiMNW4lS3D1Tntm6FqKvLHQ0I5FA/xIgd3kQ2fr1nEyRvVtltzGNqUca1tCjHaqSlr9MH0RANr2agHGzBfpRAKSiWcapeliJ1Rq/9EeYF "J – Try It Online")
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 63 bytes
```
x=>x.map((r,i)=>r.map((c,j)=>v=c?Math.max(i-j,j-i,v):v),v=0)&&v
```
[Try it online!](https://tio.run/##LY1LCsMgFEXnXcUbBQUV@/@A6Qq6gw4UmzSKjcHII7u30vRODudMrjdoZpvclPkYX13pVVlUu4iPmQhJzFHVplUs81VQ2fvD5KG2hTjumeeOIb0hZagkbRosNo5zDJ0I8U16orewAwlys4cDHCslnKqfKyVc4KrFPAWX9XPUv5@s2vxPsJYOTaB15Qs "JavaScript (Node.js) – Try It Online")
[Answer]
# JavaScript (ES7), ~~58~~ 55 bytes
```
m=>m.map(q=(r,y)=>r.map(v=>q=!v|(n=y*y--)<q?q:n))|q**.5
```
[Try it online!](https://tio.run/##fY5PU8MgEMXvfIq1l0I0NMS2xj/Ekx576bWXmJIaJ0CANDMZ62ePhKq9OcOw7G/fe8tH0ReutHXbxUrvxVjxUfJcUlm02HBsbwbCcxvanueGX/UnrPgQDXFMnsyzeVCEnEwU0dVohTnWVuB55eaEWlHsX@tGbAdV4oTQTm87W6sDJtS1Td3h2U7t1IzQStuXonzHDngOnwigER1I4OAuQi@bfhAkv3gB14sz3hzlm7CEPHpzqZXTjaCNPuAKywl@kZEhxCCBBCXAwu3rhNIAb2EJq4DXvr8L4wzuJ8HZNVX2V1lIYSFhDSwDyJB3siWkKUr9zJ8UhZTLuuRn/T@vbw "JavaScript (Node.js) – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 9 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ĀDƶsøƶøαà
```
(Or `ø‚Ā€ƶ`øαà` as minor alternative.)
[Try it online](https://tio.run/##yy9OTMpM/f//SIPLsW3Fh3cc23Z4x7mNhxf8/x8dbaADgoY6BrE6ULYBkexYAA) or [verify all test cases](https://tio.run/##yy9OTMpM/V@m5JlXUFpipaCkU3loeVT6uS1Zh3brcCn5l5ZAhO0r/x9pcDm2rfjwjmPbDu84t/Hwgv86h7bZ/4@OjjaMjdVRAFI6BjoGsTrRBjqGUBrIgkkZQSWNdUx0TKHSZkAxc6hCCx1LmFKYOSCWIRLLEGq2IdxUMx1DCx0LoDCQYaJjZARkGQFVAJERRAXIZGTnGMCdSIgdGwsA).
**Explanation:**
```
Ā        # Transform each non-0 integer in the input-matrix to a 1
 D       # Duplicate this matrix of 0s/1s
  ƶ      # Multiply each inner value by its 1-based row-index
 s       # Swap so the matrix of 0s/1s is at the top again
  øƶø    # Do the same for the columns
         # (where `ø` is a zip/transpose, to swap rows/columns)
     α   # Take the absolute difference of the values at the same positions
      à  # Pop and push the flattened maximum
         # (which is output implicitly as result)
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `G`, 7 bytes
```
vT:ẏ-ȧf
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJHIiwiIiwidlQ64bqPLcinZiIsIiIsIltbMTYsMTgsOF0sWzYsMTQsMjJdLFsyMCwxMCwxMl1dIl0=) or [Run all the test cases!](https://vyxal.pythonanywhere.com/#WyJQaiIsIsabIiwidlQ64bqPLcinZiIsIkc7WsabYCA9PiBgaiIsIltbWzFdXSxbWzEsMCwwXSxbMCwxLDBdLFswLDAsMV1dLFtbMSwyLDAsMF0sWzMsNCw1LDBdLFswLDYsMCw3XSxbMCwwLDgsOV1dLFtbMSwxLDAsMF0sWzEsMSwxLDBdLFsxLDEsMSwxXSxbMCwxLDEsMV1dLFtbMTYsMTgsOF0sWzYsMTQsMjJdLFsyMCwxMCwxMl1dLFtbMCwwLDAsMSwwXSxbMCwwLDAsMCwwXSxbMCwwLDAsMCwwXSxbMCwwLDAsMCwwXSxbMCwwLDAsMCwwXV1dIl0=)
Port of Jelly answer.
## How?
```
vT:ẏ-ȧf
vT      # Get the truthy indices of each
  :ẏ    # Duplicate and get length range [0, length)
    -ȧ  # Subtract the two and get the absolute values (implicit vectorization with both)
      f # Flatten
        # G flag takes the maximum of the top of the stack
```
]