[Question]
  [
*Inspired by a [question](https://stackoverflow.com/q/69682156/2586922) (now closed) at Stack Overflow.*
Given a square matrix, let its *double trace* be defined as the sum of the entries from its main diagonal and its anti-diagonal. These are marked with `X` in the following examples:
```
X · · X
· X X · 
· X X ·
X · · X
```
```
X · · · X
· X · X ·
· · X · ·
· X · X ·
X · · · X
```
Note that for odd `n` the central entry, which belongs to both diagonals, is counted only once.
### Rules
* The matrix size can be any positive integer.
* The matrix will only contain non-negative integers.
* Any reasonable input format can be used. If the matrix is taken as an array (even a flat one) its size cannot be taken as a separate input.
* Input and output means are [flexible](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods) as usual. [Programs or functions](http://meta.codegolf.stackexchange.com/a/2422/36398) are allowed. [Standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
* Shortest wins.
### Test cases
```
5
   ->  5
```
```
3 5
4 0
     ->  12
```
```
 7  6 10
20 13 44
 5  0  1
          ->  36
```
```
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4
          ->  32
```
```
23  4 21  5
24  7  0  7
14 22 24 16
 4  7  9 12
             ->  97
```
```
22 12 10 11  1
 8  9  0  5 17
 5  7 15  4  3
 5  3  7  0 25
 9 15 19  3 21
                -> 85
```
Inputs in other formats:
```
[[5]]
[[3,5],[4,0]]
[[7,6,10],[20,13,44],[5,0,1]]
[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]
[[23,4,21,5],[24,7,0,7],[14,22,24,16],[4,7,9,12]]
[[22,12,10,11,1],[8,9,0,5,17],[5,7,15,4,3],[5,3,7,0,25],[9,15,19,3,21]]
```
```
[5]
[3 5; 4 0]
[7 6 10; 20 13 44; 5 0 1]
[4 4 4 4; 4 4 4 4; 4 4 4 4; 4 4 4 4]
[23 4 21 5; 24 7 0 7; 14 22 24 16; 4 7 9 12]
[22 12 10 11 1; 8 9 0 5 17; 5 7 15 4 3; 5 3 7 0 25; 9 15 19 3 21]
```
```
[5]
[3,5,4,0]
[7,6,10,20,13,44,5,0,1]
[4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4]
[23,4,21,5,24,7,0,7,14,22,24,16,4,7,9,12]
[22,12,10,11,1,8,9,0,5,17,5,7,15,4,3,5,3,7,0,25,9,15,19,3,21]
```
  
[Answer]
# [APL (Dyalog 18.0)](https://dyalog.com), ~~17~~ 16 [bytes](https://github.com/abrudz/SBCS)
−1 thanks to Vadim Tukaev.
Anonymous lambda.
```
{≢⍸⍵×∨∘⌽⍨∘.=⍨⍳≢⍵}
```
[Try it online!](https://tio.run/##bY4hTwNBFIQ9v6KuZhruvb3tcgKFAQRNerjLiSZ3rWkAgYCQqiZNId0GBAGNOkGCABJMTftP3h853i2OnNnMfjM7s6Oraa@4HU0vJ73y5rq8KMqiru/k/k38j/jv3YssK1m@ymojvjrc31biP8V/qTWrx7J4FL@Wh7n4d4XbDyOLJ1k/p8MjPc@PT9J63PlDp@ngrJtlNs@7e/@Ygc2RxYhaPIc@KFKbI5BBHKu0UN2SZfXBFNo4htOYU0kKGQqoH2YcEhC3vWflugYi7Ud2oMEIFuTCqANZHTDhYkI9N1NJwylRxM2vfgE "APL (Dyalog Extended) – Try It Online")
`{`…`}` [dfn](https://wikipedia.org/wiki/Direct_function); argument is `⍵`
 `⍴⍵` shape (number of rows and columns) in the argument
 `⍳` **i**ndices of an array of that size
 `=/¨` equality-reduction of each row-column pair (gives identity matrix)
 `∨∘⌽⍨` OR with mirrored self (indicates both diagonals)
 `⍵×` multiply with argument
 `≢⍸` sum (lit. length of list of indices where each index is repeated as many times as its corresponding number in that)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
TżU$Ṭḋ⁸S
```
[Try it online!](https://tio.run/##LY2xEcIwDEX7zEH5C0u2IzwHUPlc0nBZgBIaClbJAOGOjmOQZBHz41Dp6/3T0@U8DNdaj9/3aTe/xnl6LrfpUD@P5T7WmrucYyng8IgFOcBtq6GHOBJ1EI8QGCOYt1qJoNJuNMDYGKMQKgikbzJDguj/RBnphAgtyHt2DhFiTW2QSKdvi29GXe1p5ZKIlL@78gM "Jelly – Try It Online")
```
T         -- truthy indices of the argument, since every row is non-empty, this is [1 .. len(z)]
 żU$      -- zip with its reverse
    Ṭ     -- for each pair of integers, create a list with 1's at those two indices
     ḋ⁸   -- for each resulting list, take the dot product with the corresponding row vector of the input matrix
       S  -- sum the results
```
[Answer]
# [R](https://www.r-project.org/), 47 bytes
```
function(m)sum(m[(z=diag(y<-nrow(m)))|z[y:1,]])
```
[Try it online!](https://tio.run/##K/qfpmCj@z@tNC@5JDM/TyNXs7g0VyM3WqPKNiUzMV2j0kY3ryi/HCiuqVlTFV1pZagTG6v5P00jN7GkKLNCw9DK0EzHRFOTC0nEyFTHVFPzPwA "R – Try It Online")
[Answer]
# [K (ngn/k)](https://codeberg.org/ngn/k), 15 bytes
```
{+//x*|/|:\=#x}
```
[Try it online!](https://ngn.bitbucket.io/k#eJwdjkEOwyAMBO+8YqVeQnsAGwwhVn/Sc94QqWne3iXCBzxe73rfvq+UjueZzu3zfhy/EOzaYUH02pcC84ocQyFcFIKC6gKFofnA/dgbeaGqUdXRINk1QyiubuAvhtGnAwmUeteKzkF3IVCwleaTDYjGsN55DGRxXSC+cpSZJJ2WHWK0Kj6Tp5EazyGTQaAS/3qrKyU=)
Useful to have an identity matrix primitive.
Explanation:
```
{+//x*|/|:\=#x}
           =#x    / identity matrix with size of x
        |:\       / append reverse                          
      |/          / OR both matrices
    x*            / multiply by original matrix
 +//              / sum all elements
```
[Answer]
# [Python 3.10](https://docs.python.org/3.10/), 58 bytes
```
lambda m,i=0:sum(r[i:=i-1]+r[~i]*(~i!=len(m)+i)for r in m)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=LY69boMwEMd3nsLdTHOJOANxQGLvO7gMVAH1JGyQcSplyYtkyZIufaK8TQ8nnv4fd7_z9Xc-h-_J3e6DaMTn_RSG7eFRj539OnbCAjVZvZys9IbqhrbYbry5UPsuL_TWjL2TNt1QOkxeeEFO2PRF-CA7Tz6I5bwkazuS69cB9rslHMnVieBnu8Bn-59ulOtEGsPZkwtykFymL97t8WdM2baJMTmULZgCsug07AEzDlQGmENRsCyBdWwVJ6AwbqgCNBeaJXKogAPcR5SGClA9NxQrJgIiM8AcuMqgBNQRrAFLRubR5BGoVni15lhxpPjy88__)
---
# [Python 3](https://docs.python.org/3/), 62 bytes
```
lambda m:sum(r[i]+r[~i]*(i!=len(m)+~i)for i,r in enumerate(m))
```
[Try it online!](https://tio.run/##JY1NbsMgEIX3PgXd4WZUebAd6kg5CfXCVbCKZLCFSaRscnXnQZGQ3s/MN9sz/a2hPWZxFT/HMvnf2yT8Zb97GY0bT9G83Pgp3cd1sUH6@vRy9bxG4Qg/CBvu3sYpWVT14fy2xiT2517lmcUFm4fgv/Z0c@FSCTw/JRyzj2mReaIu4RZdSHKWKAEyph/HypiW@pFMR01xms7EDQLVELfUdZA9QZdWISHFZUN1pFFoSEaoCAGfC0rTQKz@NxQUiMQMBplvVA31xLqANXEPZFtMW4Aqw4ec84BI4fIb "Python 3 – Try It Online")
## Explanation
```
lambda m:
                                      for i,r in enumerate(m)  # iterate m with i = row index and r = row
                 +r[~i]*(i!=len(m)+~i)                         # add r[len(m) - i - 1] if the index does not equal i
             r[i]                                              # add r[i]
         sum(                                                ) # sum numbers generated by the loop
```
[Answer]
# [Octave](https://www.gnu.org/software/octave/), ~~36~~ 34 bytes
This uses logical indexing. As an index we use an identity matrix of the size of the input and `or` it with it's flipped version. Thanks @LuisMendo for -2 bytes!
```
@(x)sum(x(flip(e=eye(size(x)))|e))
```
[Try it online!](https://tio.run/##y08uSSxL/Z9mq6en999Bo0KzuDRXo0IjLSezQCPVNrUyVaM4syoVKK6pWZOqqfk/JbO4QCNNIz8vtVjDFCho/R8A "Octave – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~13~~ 11 bytes
This uses logical indexing. As an index we use an identity matrix of the size of the input and or it with it's flipped version, just like in my Octave answer. Thanks @LuisMendo for -2 bytes:)
```
tZyXytPY|)s
```
**Explanation**
```
t            push input twice to stack
 Zy          get size of matrix
   Xy        get an identity matrix of that size
     t       duplicate the identity matrix
      P      flip one of the identty matrices
       Y|    logical OR the two identity matrices
         )   perform (logical) indexing to the original matrix
          s  compute the sum
```
[Try it online!](https://tio.run/##y00syfn/vySqMqKyJCCyRrP4//9oQx0jHWMdE2tTHTMdcx0La0sdQwMdQ0MdQyNrQ2MdQxMdQ1MdQ7NYAA "MATL – Try It Online")
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 8 bytes
```
LÞ□Ḃ⋎*∑∑
```
[Try it Online!](https://vyxal.pythonanywhere.com/?v=1#WyIiLCIiLCJMw57ilqHhuILii44q4oiR4oiRIiwiIiwiW1sxLDIsM10sWzQsNSw2XSxbNyw4LDldXSJd)
```
 Þ□      # Identity matrix of size...
L        # len(input)
    ⋎    # OR with...
   Ḃ     # itself reversed
     *   # Multiply by input
      ∑∑ # Sum
```
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 42 bytes
```
m->sum(i=1,#m,m[i,i]+m[i,j=#m+1-i]*(i!=j))
```
[Try it online!](https://tio.run/##HYxBDoIwEEWvUnXTyidhBkolDdzAE5Au2GhqrGlQEz09Tl3N@29@fl7WWF/zdlHjlurp@U46joRDQpojYqjKuY2HVFEdw1HH3XgzZltyvn/1R9WTymt8vAT3JezVRX@MgZrPy0vbAi2s79AEQYce1HhuQC26zlsIlQdLBJM0uYMT6zyJYEik3hc3gPhfZQFZARHIn8Q3sCAnYw5kZacVbP8rbP1QHA0imEIw2w8 "Pari/GP – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 bytes
```
āDδQÂ~*˜O
```
[Try it online!](https://tio.run/##yy9OTMpM/f//SKPLuS2Bh5vqtE7P8f//PzrayFjHRMfIUMc0VifayETHXMdAxxzINAQKGukABQzNgDyQuKWOoVFsLAA "05AB1E – Try It Online")
-1 thanks to @KevinCruijssen
Wish to have the identity matrix builtin
```
g               Length
 L              Range
  D             Dup
   δ            Outer product with
    Q           Equals?
     Â          Bifurcate
      ~         Bitwise OR
       *        Multiply with input
        ˜       Flatten
         O      Sum
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), 64 bytes
```
->m{(r=0...l=m.size).sum{|y|r.sum{|x|x==y||x-~y==l ?m[y][x]:0}}}
```
[Try it online!](https://tio.run/##NY7NasMwEITvegpBLy1shHZlWXFB6YMIHdKQ0IIFwSFgxXJf3V3/wR722xlmtnt@5@nmp8MpDe@d10qp1if1@H1dP9TjmYaSS7cufem9z6X0h7/sfSu/Usgx9PFTj@M4BSHfrJAh2BhBMCDNZMAyShkq0Jtg6llwUAPqVSMNaKCqVrLAuHkbN3uJRSDco6gCxx63ErJEwDes9yYHDSBtEcflK7YgDycjhy@@I7s0WEC39zpAy01mZ7P00FbbzCo2fCV@T0R1PV9@hpLKXd7U5dy2Mo1i@gc "Ruby – Try It Online")
```
->m{              # lambda taking a 2d array and returning diagonal sum
(r=0...l=m.size)  # range 0..length 
.sum{|y|r.sum{|x| # sum the result of passing each coordinates
x==y||x-~y==l ?   # if on diagonals 
m[y][x]:0         # take value else 0
```
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~81~~ \$\cdots\$ ~~71~~ 59 bytes
```
i;f(m,l)int*m;{for(i=l*l;--i;)*m+=i%~l&&i%~-l?0:m[i];l=*m;}
```
[Try it online!](https://tio.run/##bVPBbqMwEL3nK0ZIVECMCgaapi7tYbVfkeQQEdNaNaSCSI0a0U9fduwhFHZrgT1@8@bNeAxF@FIUfa9E6VVM@6o@BZW4lMfGU7kOtAhDJfygWubK/dI3NziH@jl6qDZqJ3SO3K7HGKj2qvZ8uCwAhwFOsj3Fmx3kcMk6MUKcoIRBxiBlEE18CflWDO4YxBEDjm@M1DS1dLOb0FOi88QK8dhyOJorS8U5NjgnML6zNETXuOETnWzQ4QanxDGK4XNvyZEVjld2MapUOR0huabjGSkb6trifCg2sFlaymLbwqgVtCS0pLRMe6UpBOncCpouDG55fpfFSR6GBlPlCR5xjdXcG1bxum8CnGXxJhuiOdvzb749r3/hmzkMpvvE6YS9O7x58EwKVR/kGcMiMZiP0KpPeSy9a3L/dgCCERGwXFq2b8Xocxg@iQAqlKNeWM5OTN2gB6/@zzvW1Bw/qCJjPII26dD0R@Z3wllccdQUZ4whDk1/xp7HmvHeYHDpOW7r8gM2DEOeTd8eHJyrDWYONCwNvPPFLLpb/KSzrZ0Jr5udvsUCzS8I2p@3RaJjvO5/O3NVhvAJ3AO4LaZg0LLx4ts8l9PirgHhD2NbfxfYLbr@T1Hq/Uvbhx9/AQ "C (gcc) – Try It Online")
*Saved a bytes thanks to [att](https://codegolf.stackexchange.com/users/81203/att)!!!*  
*Saved 12 bytes thanks to [AZTECCO](https://codegolf.stackexchange.com/users/84844/aztecco)!!!*
Inputs a pointer to a flattened square array and the number of rows (because pointers in C carry no length info).  
Returns its double trace.
[Answer]
# [tinylisp](https://github.com/dloscutoff/Esolangs/tree/master/tinylisp), ~~132~~ 109 bytes
```
(load library
(d L length
(d f(q((M)(s(a(trace M)(trace(reverse M)))(*(odd?(L M))(nth(nth M(/(L M)2))(/(L M)2
```
[Try it online!](https://tio.run/##dY9LjsIwEET3nKKW1Wwm7cSErLgAOYRnEoZIIYBjjcTpM@0oYodkS1Wvqv1Jw/Qah/mxLBzvocM4fMcQXzt2OGPsp990zfrCJ9kKZwamGH56mFkFY//XxzkDEe5577oTz9lxSte80fJrJc7YphauJyqcsES1e1uUwgoeB2GNI5otqXGAFkJXQK1fCT1MbqmXTTgHtWWJQoU2by0PrXO/hnpU@QKP0lwB54VNptoYcSr5q@3HlyTewgMJ7fIP "tinylisp – Try It Online")
-23 bytes thanks to DLosc.
[Answer]
# [Pip](https://github.com/dloscutoff/pip) `-x`, ~~24~~ ~~21~~ 20 bytes
```
YEY#a$+$+a*HV:y+1+Ry
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m724ILNgWbSSboVS7IKlpSVpuhZbIl0jlRNVtFW0E7U8wqwqtQ21gyohUlAVNxWjlaKjza3NrA0NYq2jjQysDY2tTUyATFNrIDs2Fm4WAA) The matrix should be given as a command-line argument in the following form:
```
[[7;6;10];[20;13;44];[5;0;1]]
```
Or, [verify all test cases](https://ato.pxeger.com/run?1=dU8xDsIwDNx5xQk6UVWKnaRp2ZFYWBiQqihDFyQkhg4wVIiXsBQheAGfYeYjuAkryuDzXe5sX-_dvnv6aXGYhtv5cTruiurVLJtZm-VZ3s5X20WfU77pk_T-XLCGn8B7G8JYNGyAN1CpdShBShhWIA1jBFoITrJBfNHxH8WvLG4wxXg2cBLiBJKQDCGojA6HGsQ_CwuU8SCSgfCVaAoW5OIWDmQlU8dGx0Qe0-uRp1ooHtcM6dRhSPUL).
### Explanation
```
YEY#a$+$+a*HV:y+1+Ry
   #a                 Size of the matrix
 EY                   Identity matrix of that size
Y                     Yank into the y variable
                  Ry  y reversed (backward identity matrix)
                1+    Add 1 to each value
              y+      Add to y (identity matrix)
           HV:        Each value halved (rounded down)
                      This creates a matrix where both diagonals are 1's and
                      everything else is 0's
         a*           Multiply the input matrix itemwise by the 1/0 matrix
       $+             Fold on addition (sum each column)
     $+               Fold on addition (sum that list of sums)
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-mx`](https://codegolf.meta.stackexchange.com/a/14339/), 11 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
gV +J´gUhVT
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LW14&code=Z1YgK0q0Z1VoVlQ&input=WwpbMjIsMTIsMTAsMTEsIDFdClsgOCwgOSwgMCwgNSwxN10KWyA1LCA3LDE1LCA0LCAzXQpbIDUsIDMsIDcsIDAsMjVdClsgOSwxNSwxOSwgMywyMV0KXQ)
```
gV +J´gUhVT     :Implicit map of each row U at 0-based index V in implicit input array
gV              :Index V into U
   +            :Add
    J           :  Initially -1
     ´          :  Postfix decrement
      g         :  Index into
       UhV      :    U with the character at index V replaced with
          T     :    0
                :Implicit output of sum of resulting array
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 18 bytes
```
ＩΣＥθΣΦι∨⁼κμ⁼⁺κμ⊖Ｌθ
```
[Try it online!](https://tio.run/##PcjLCsIwEIXhV8lyhCM0qaEWl15WigWXIYtQgy2m1aaprx9TkQ5nMd9fN8bXL@NirHzbB9qbMdBt6uhi3jSAze@pdcF6asGuno7DZNxIT7BuBfZX5aYlHWztbWf7YO90tv0jNDSsltvFqJQSAjwtA@fgGmqLEhkkeJEgUYBLbJD/kCdmEDKhnDsvUxJcax3XH/cF "Charcoal – Try It Online") Link is to verbose version of code. Explanation: Filters out elements not on either main diagonal, then takes the sum.
```
   θ                Input matrix
  Ｅ                 Map over rows
      ι             Current row
     Φ              Filtered where
         κ          Row index
        ⁼           Equals
          μ         Column index
       ∨            Logical Or
             κ      Row index
            ⁺       Plus
              μ     Column index
           ⁼        Equals
                 θ  Input matrix
                Ｌ   Length
               ⊖    Decremented
    Σ               Take the sum
 Σ                  Take the sum
Ｉ                   Cast to string
                    Implicitly print
```
[Answer]
# [Raku](http://raku.org/), 43 bytes
```
{sum .kv.flatmap:{@^v[unique $^k,@v-$k-1]}}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/urg0V0Evu0wvLSexJDexwKraIa4sujQvs7A0VUElLlvHoUxXJVvXMLa29n9xYqVCmka0jbmCmYKhgZ2OjZGBgqGxgokJkGmqAGTbxWr@BwA "Perl 6 – Try It Online")
A function which takes its input in `$_` as a list-of-lists.
* `.kv` returns an interleaved sequence of each row's index and the row, ie: `0`, the first row, `1`, the second row, etc.
* `.flatmap: { ... }` passes the key and value to the brace-delimited anonymous function and flattens the return values. That function takes the index of each row in `$k` and the row itself in `@v`, each argument declared with a "twigil" `$^`/`@^` on its first lexical appearance.
* `$k` and `@v - $k - 1` are the indices of the elements of each row that go into the double trace. `@v`, the row, evaluates to its number of elements in a numerical context (the subtraction operator).
* `unique` selects only the unique indices. This eliminates the double index at the center of the matrix, if it has an odd dimension.
* `@v[...]` is a slice that returns the row elements at the unique indices.
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 18 bytes
```
s.e+@bk&-hyklb@_bk
```
[Try it online!](https://tio.run/##JY3BCsIwEETv/obgxalkN0ljD0IvfoDnUISiUKnYQnvpzxsn8bLMvmHfzts6pP3ldk3L6Xls@/FQDdv47tt7P6YUu12MvsvTwneIDqZsATXEEKiBWDjH6MFcWiWBSrlQh8AiMAqhgkDqogpoIPq/UCYaIUIH4pmVgYeEIg4QT6Utiy1CzfImc2mIlJ@/07y@ps@SqscP "Pyth – Try It Online")
## Explanation
```
s.e+@bk&-hyklb@_bk
 .e                 # Iterate through implicit input with (b = row, k = index)
    @bk             # b[k]
       &-hyklb      # if ((2 * k) + 1) - len(b) != 0
   +          @_bk  #   plus b[::-1][k]
s                   # sum results from the iteration
```
The `((2 * k) + 1) - len(b)` is derived from rearranging `k - (len(b) - (k + 1))`
[Answer]
# JavaScript (ES6), 51 bytes
```
m=>m.reduce((t,r,i)=>t+r[i]+r.reverse(r[i]=0)[i],0)
```
[Try it online!](https://tio.run/##bY7BDoIwEETvfkkbRtJdqJUD/kjDgUA1GBRTkN/HpUfDZTM7M9m3z3Zt5y4On@X8nvqw3evtVd9eeQz9twtKLYgYdH1bsuiHJosSrCHOQe1rbbRMGL1103uexpCP00Pdlfe2abQ@/bsFbANfwhymDheQkQIbUIGyFGkh@rDN0gBTusglnBSdSBKTIQZdEsqhAvHxBZZEiCASBvxVqgYW5BLYgawgirQUCcA7rNp9qsTi9Nn2Aw "JavaScript (Node.js) – Try It Online")
### Commented
```
m =>         // m[] = matrix
m.reduce(    // reduce():
  ( t,       //   t   = sum
    r,       //   r[] = current row
    i        //   i   = row index, used as a column index
  ) =>       //
  t + r[i] + //   add r[i] to t
  r.reverse( //   reverse r[] ...
    r[i] = 0 //     ... but only once r[i] has been cleared
             //     so that it's not counted twice
  )[i],      //   add r[i] again
  0          //   start with t = 0
)            // end of reduce()
```
---
# JavaScript ES2022, 46 bytes
Suggested by [@tsh](https://codegolf.stackexchange.com/users/44718/tsh):
```
m=>m.reduce((t,r,i)=>t+r[i]+r.at(~i,r[i]=0),0)
```
(Doesn't work on TIO.)
]