[Question]
  [
## Challenge:
Given a matrix input, determine the amount of diagonals and anti-diagonals with duplicated numbers.  
So if we have a matrix like this:
```
[[aa,ab,ac,ad,ae,af],
 [ba,bb,bc,bd,be,bf],
 [ca,cb,cc,cd,ce,cf],
 [da,db,dc,dd,de,df]]
```
All diagonals and anti-diagonals would be:
```
[[aa],[ab,ba],[ac,bb,ca],[ad,bc,cb,da],[ae,bd,cc,db],[af,be,cd,dc],[bf,ce,dd],[cf,de],[df],
 [af],[ae,bf],[ad,be,cf],[ac,bd,ce,df],[ab,bc,cd,de],[aa,bb,cc,dd],[ba,cb,dc],[ca,db],[da]]
```
**Example:**
```
[[1,2,1,2,1,2],
 [1,2,3,4,5,6],
 [6,5,4,3,2,1],
 [2,1,2,1,2,1]]
```
All diagonals and anti-diagonals would be:
```
[[1],[2,1],[1,2,6],[2,3,5,2],[1,4,4,1],[2,5,3,2],[6,2,1],[1,2],[1],
 [2],[1,6],[2,5,1],[1,4,2,1],[2,3,3,2],[1,2,4,1],[1,5,2],[6,1],[2]]
```
Removing all diagonals and anti-diagonals only containing unique numbers:
```
[[2,3,5,2],[1,4,4,1],[2,5,3,2],[1,4,2,1],[2,3,3,2],[1,2,4,1]]
```
So the output is the amount of diagonals and anti-diagonals containing duplicated numbers:
```
6
```
## Challenge rules:
* If the input matrix is empty, contains only 1 number, or contains only unique numbers across the entire matrix, the output is always `0`.
* Input is guaranteed to only contain positive digits `[1,9]` (unless it's completely empty).
* The matrix will always be rectangular (i.e. all the rows are the same length).
* I/O is flexible. Input can be taken as a list of lists of integers, or 2D array of integers, or a Matrix-object, as a string, etc. etc. You are also allowed to take one or both of the dimensions of the matrix as additional input if it would save bytes in your language of choice.
## General rules:
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest answer in bytes wins.  
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
* [Standard rules apply](https://codegolf.meta.stackexchange.com/questions/2419/default-for-code-golf-program-function-or-snippet/2422#2422) for your answer with [default I/O rules](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods/), so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
* [Default Loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
* If possible, please add a link with a test for your code (i.e. [TIO](https://tio.run/#)).
* Also, adding an explanation for your answer is highly recommended.
## Test cases:
```
Input:                     Output:
[[1,2,1,2,1,2],            6
 [1,2,3,4,5,6],
 [6,5,4,3,2,1],
 [2,1,2,1,2,1]]
[[]]                       0
[[1,2],                    0
 [3,4]]
[[1,1],                    2
 [1,1]]
[[9,9,9],                  6
 [9,9,9],
 [9,9,9]]
[[7,7,7,7],                8
 [7,7,7,7],
 [7,7,7,7]]
[[1,1,1],                  1
 [2,3,4],
 [2,5,1]]
[[1,8,4,2,9,4,4,4],        12
 [5,1,2,7,7,4,2,3],
 [1,4,5,2,4,2,3,8],
 [8,5,4,2,3,4,1,5]]
[[1,2,3,4],                4
 [5,6,6,7],
 [8,6,6,9],
 [8,7,6,5]]
```
  
[Answer]
# [R](https://www.r-project.org/), ~~92~~ ~~86~~ ~~82~~ 78 bytes
```
function(m,x=row(m),y=col(m),`|`=split,`^`=Map)sum(max^table^c(m|x-y,m|x+y)>1)
```
[Try it online!](https://tio.run/##rZLNioMwEMfvPkUhl4Sdgonxa9n0DXrrWbSygmBqsZYq9N3tJJRqZSlla4Ykk0z4zST5N0Pxsx6K8yFvy/pANXSqqS9UM@hVXlfGSa@pOh2rsoU0SdU2O7LTWVOddUmb7avfJKf62q17wPGrZxvOhgKjbVN2NKccBEy7BxJ8CNB89LxZlDPcDGDfYw1qx9hq3kjgPODggvvHkf834o5w/i2xHvGilE/gy6IRLkZ4jK/qLQqfvHloP3BJeOQ8qYU/VCJQIaiHTy5D@DM8stgYR2llaFKFaNKm5HZP3FcRmn/3JcZ8I81oUgrhwpkJfRR3iEfNHOMcGrEzm/L9ixA53AA "R – Try It Online")
### Explanation
First, we declare additional variables \$x\$ and \$y\$ that stand for row and column indices, respectively. Then we can delineate the diagonals and anti-diagonals by taking their difference and sum. E.g., for a 4x4 matrix:
\$x-y\$ gives:
`0 -1 -2 -3
 1 0 -1 -2
 2 1 0 -1
 3 2 1 0`
\$x+y\$ gives:
`2 3 4 5
 3 4 5 6
 4 5 6 7
 5 6 7 8`
Now `split(m, x-y)` and `split(m, x+y)` produce the actual lists of diagonals and anti-diagonals, which we join together.
Finally, we count the entries of the resulting list where duplicates are present.
Thanks for bytes saved:
-4 by CriminallyVulgar  
-4 by digEmAll 
[Answer]
# [J](http://jsoftware.com/), ~~21~~ 20 bytes
-1 byte thanks to Jonah!
```
1#.|.,&((~:&#~.)/.)]
```
[Try it online!](https://tio.run/##VVBNC8IwDL37K4Irc4NZ1y5bP2AnwZMn7x5EHOLFPyD76zVtKm482jTvJY@kr7CVuwlGDztooAVPZy/heDmfgirkRzZlVc2@LGZZH2R9DfXmcX@@YYARJkCKAhTo1emI70kZ6EbK1qpigzYZ3Pwy0wTxs2BBr4SE5QAdQYAD4Zi1mcVYHnsM82pRzTY8pqYRs6HSeSWbamxSHd2Y1okdhoCpUyVO58wS@vxG0np2xGyI/6XyvxhqiNFRNPGfwhc "J – Try It Online")
## Explanation:
```
1#.                   find the sum of the  
     ,                concatenation of
       (          )   the result of the verb in the parentheses applied to
                   ]  the input
      &               and
   |.                 the reversed input
        (      )/.    for each diagonal
         ~:&#~.       check if all elements are unique and negate the result 
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 31 bytes
```
ËcUî
ËéEÃÕc¡XéYnÃÕ mf fÊk_eZâÃl
```
[Try all test cases](https://ethproductions.github.io/japt/?v=1.4.6&code=y2NV7grL6UXD1WOhWOlZbsPVIG1mIGbKa19lWuLDbA==&input=W1tbMSwyLDEsMiwxLDJdLAogWzEsMiwzLDQsNSw2XSwKIFs2LDUsNCwzLDIsMV0sCiBbMiwxLDIsMSwyLDFdXQpbW11dCltbMSwyXSwgICAgICAgICAgICAgICAgICAgIAogWzMsNF1dCltbMSwxXSwgICAgICAgICAgICAgICAgICAgIAogWzEsMV1dCltbOSw5LDldLCAgICAgICAgICAgICAgICAgIAogWzksOSw5XSwKIFs5LDksOV1dCltbNyw3LDcsN10sICAgICAgICAgICAgICAgIAogWzcsNyw3LDddLAogWzcsNyw3LDddXQpbWzEsMSwxXSwgICAgICAgICAgICAgICAgICAKIFsyLDMsNF0sCiBbMiw1LDFdXQpbWzEsOCw0LDIsOSw0LDQsNF0sICAgICAgICAKIFs1LDEsMiw3LDcsNCwyLDNdLAogWzEsNCw1LDIsNCwyLDMsOF0sCiBbOCw1LDQsMiwzLDQsMSw1XV0KW1sxLDIsMyw0XSwgICAgICAgICAgICAgICAgCiBbNSw2LDYsN10sCiBbOCw2LDYsOV0sCiBbOCw3LDYsNV1dCl0KLVFt)
Explanation:
```
Ëc                            #Pad each row...
  Uî                          #With a number of 0s equal to the number of rows
ËéEÃÕ                         #Get the anti-diagonals:
ËéEÃ                          # Rotate each row right a number of times equal to the row's index
    Õ                         # Get the resulting columns
     c                        #Add to that...
      ¡XéYnÃÕ                 #The diagonals:
      ¡XéYnÃ                  # Rotate each row left a number of times equal to the row's index
            Õ                 # Get the resulting columns
              mf              #Remove the 0s from each diagonal
                 fÊ           #Remove the all-0 diagonals
                   k_   Ã     #Remove the ones where:
                     eZâ      # The list contains no duplicates
                         l    #Return the number of remaining diagonals
```
I also tried [a version](https://ethproductions.github.io/japt/?v=1.4.6&code=y6NbWVhFXX3DcmMK/F/OLVrMw2NV/F/OK1rMwyCubWcxw2tfZVriw2w=&input=W1sxLDIsMSwyLDEsMl0sICAgICAgICAgICAgCiBbMSwyLDMsNCw1LDZdLAogWzYsNSw0LDMsMiwxXSwKIFsyLDEsMiwxLDIsMV1dCi1R) based on Kirill L.'s Haskell answer, but couldn't find a good way to "group by a function of the X and Y indices" and the alternative I found wasn't good enough.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 [bytes](https://github.com/DennisMitchell/jellylanguage/wiki/Code-page)
```
ŒD;ŒdQƑÐḟL
```
[Try it online!](https://tio.run/##y0rNyan8///oJBfro5NSAo9NPDzh4Y75Pv///4@ONtQx0oHiWB0uBTDfWMdEx1THDMw3A7JMgCJAFWA@XDWQHwsA "Jelly – Try It Online") or **[Check out the test suite!](https://tio.run/##bVE7DsIwDN17ih7AS9qmH7EisbAwR9lgQb0AKwsr9AJcgAN0r8o94CLhxQmmoq1V6cV@79lxjoe2PTk3duvV2O13z9twffX3rRsu7/Nj45wxRlFG8beUypekXMmpIE2lJZxLoAIZcPksOpyRSIyxU4efVcJdlmpwQYeoVt53kaOkQ0OIOQucWBEU@BVx/CvAksoEyxxLk/CN/bTh7lpmUlRjLxm6Fj6@SrA0b8d7@3rOSsUbzUKGas7VvNmwbUVafGO/2Rx4EUQVtR43EVfA0NsP)**
Alternatives:
```
ŒD;ŒdQƑ€¬S
ŒD;ŒdQƑ€ċ0
```
### How it works?
```
ŒD;ŒdQƑÐḟL – Monadic link / Full program.
  ;        – Join:
ŒD           – The diagonals
             with
   Œd        – The anti-diagonals.
       Ðḟ  – Discard the lists that are not:
     QƑ      – Invariant under deduplication.
         L – Length (count them).
```
[Answer]
# JavaScript (ES6), ~~107 105 101~~ 98 bytes
```
f=(m,d=s=1)=>(m+0)[s-=~d/2]?m.some(o=(r,y)=>!r.every((v,x)=>x+d*y+m.length-s?1:o[v]^=1))+f(m,-d):0
```
[Try it online!](https://tio.run/##fVHbboMwDH3vV7C3ZJhLKLdWyvohiElVCe0mIBOpUHnZrzOHkG7SSmVFio@PfXz5PA5Hdeo/vq5eJysxTTUnLVRccUb5G2ndkBbK499VEJWH1leyFURy0sOI4ZfeF4PoR0IGuKF/c6vX0W39RnTn68VTB7aXxVC@Yy3q1ljXq@g@nE6yU7IRfiPPpCaFEwROunGcgkEEyyvBAluIIYHUACl@Y4SQY4B7AgKbktLNg9qhJj6P3hVRbZUZGSazva0rztPsAM1w/33XEnNNyWA2w3/orKWzpbHf7eh5lm/ypGG2zJbjdiPsMNZmEpN5u1pXh7Z2eH2UyECQGzCfj2MuxiBZFYv/nNZqpGiZLaOdnXUydEyt6Qc "JavaScript (Node.js) – Try It Online")
### Note
The way this code is golfed, the anti-diagonal consisting of the sole bottom-left cell is never tested. That's OK because it can't possibly contain duplicated values.
### Commented
```
f = (                    // f = recursive function taking:
  m,                     //   m[] = input matrix
  d =                    //   d   = direction (1 for anti-diagonal or -1 for diagonal)
  s = 1                  //   s   = expected diagonal ID, which is defined as either the sum
) =>                     //         or the difference of x and y + the length of a row
  (m + 0)[               //
    s -= ~d / 2          // increment s if d = -1 or leave it unchanged otherwise
  ] ?                    // if s is less than twice the total number of cells:
    m.some(o =           //   o = object used to store encountered values in this diagonal
    (r, y) =>            //   for each row r[] at position y in m[]:
      !r.every((v, x) => //     for each cell of value v at position x in r[]:
        x + d * y +      //       x + d * y + m.length is the ID of the diagonal
        m.length - s ?   //       if it's not equal to the one we're looking for:
          1              //         yield 1
        :                //       else:
          o[v] ^= 1      //         toggle o[v]; if it's equal to 0, v is a duplicate and
                         //         every() fails which -- in turn -- makes some() succeed
      )                  //     end of every()
    )                    //   end of some()
    + f(m, -d)           //   add the result of a recursive call in the opposite direction
  :                      // else:
    0                    //   stop recursion
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 25 bytes
```
í‚εεygÅ0«NFÁ]€ø`«ʒ0KDÙÊ}g
```
[Try it online!](https://tio.run/##yy9OTMpM/f//8NpHDbPObT23tTL9cKvBodV@bocbYx81rTm8I@HQ6lOTDLxdDs883FWb/v9/dLShjpEOFMfqKIC5xjomOqY6ZiCuGZBhAhQAyoO4cKVAbiwA "05AB1E – Try It Online")
or as a [Test Suite](https://tio.run/##yy9OTMpM/W/p5vL/8NpHDbPObT23tTL9cKvBodV@bocba2trHzWtObwj4dDqU5MMvF0OzzzcVZv@X@d/dLShjpEOFMfqKIC5xjomOqY6ZiCuGZBhAhQAyoO4cKVAbixXdDSYgOoE6oJyDSEGQZRY6gAhSACNAZIz1wFDkCAWJtQwmM0g88EMU6jJhjoWQLcZAU0zAUGQpCnYbSDtIAljiDtAnjGCCOhYgIQswJ6C@NNQxxTmCZgNQK8DoTlEJYhpCWGaA5lAxQA)
**Explanation**
```
í                          # reverse each row in input
 ‚                         # and pair with the input
  ε                        # for each matrix
   ε                       # for each row in the matrix
    ygÅ0«                  # append len(row) zeroes
         NFÁ               # and rotate it index(row) elements to the right
            ]              # end loops
             €ø            # transpose each matrix
               `«          # append them together
                 ʒ     }   # filter, keep only rows that
                  0K       # when zeroes are removed
                    DÙÊ    # are not equal to themselves without duplicate values                           
                        g  # push length of the result
```
I feel like I've missed something here.  
Need to try and golf this more later.
[Answer]
# [Python 2](https://docs.python.org/2/), ~~144~~ 136 bytes
```
lambda m:sum(l(set(d))<l(d)for d in[[r[i*x+o]for i,r in enumerate(m)if-1<i*x+o<l(r)]for o in range(-l(`m`),l(`m`))for x in[-1,1]])
l=len
```
[Try it online!](https://tio.run/##ZZDRboMwDEXf@Yo8JpuRlhQKndovYUhlImxISahSKnVfzxyHAt2wAOfm@Mbx5Wf8HpyautPHZBr72TbMvl9vlht@1SNvhTga/HaDZy3rXVX5qn@5vw51UHrA1zHtblb7ZtTcir5L5ZEIrPOCsCFAvnFfmqeGn@1ZQPyR7T3YphJkXYvEnIx208X3bmQdryoJCua3BrY@CaOtHWSQw74GXO8xy1BBmNZLITkz2CfJ6hsUxuBtq81nYCnaRuA/Itc2qAf5AAOrtuwBMB40svN6yWLdc1vot3QfmohZvp4C8hkv8coK7bIQhOd05QIj7OxIkzQmFRUoSStpXHGEEvJ5IEyqPyOJfWzujPPGKGaTkB/mvMCcjCCbfgE "Python 2 – Try It Online")
[Answer]
# [Octave](https://www.gnu.org/software/octave/), 98 bytes
```
@(A)nnz([(q=@(Q)arrayfun(@(n)nnz(z=diag(Q,n))-nnz(unique(z)),-([m,n]=size(Q)):n))(A),q(rot90(A))])
```
[Try it online!](https://tio.run/##bVFNb4MwDL3zK7hMciQXEcrnJCT2E3pGHNBGJw5NBSuTxp9ntpPRbGsspBf7vWfHXF9v/eewnesoirYGXpQxK7Qw1Q2cVD/P/dd5MdCAkcJav439O5zQKHXgxGLGaRlgVQoP0F7QdPXHuA6kVc/EIT@cYL7eqpig6tR2hrbVmKD7OgyfQu/kQSjlI6aYYd4h3XNCKWVIIPddTPdOBQFbEvht5J3YUmy3R6yYTKnhbqa50UNmIuPd21ZIwdzwL50f4qo7@lEVKOF0vrIk7l71sDeZne1@nFjLYvgVdkWZN6XGkhaY0Awph9WLTvODMtklN2LSUQy07D@xGSwlV8p/sP9GY@bZu8b/xkrFPaconAPjyuGCMLts3w "Octave – Try It Online")
[Answer]
## Haskell, ~~118~~ 112 bytes
```
import Data.List
r#(a:b)=sum[1|(/=)=<<nub$[h|h:_<-a:r]]+[t|_:t<-a:r]#b
[]#_=0
a#_=a#[[]]
h x=[]#x+[]#(reverse x)
```
[Try it online!](https://tio.run/##dVHLboMwELz7K1aiB1CcNCa8guDWY/sFloUcCQnUQBGQKof8O10/eKhNvbLlHc3OrNeVHD7L63Wa6qb76kd4k6M8vNfDSHrHlenFy4dbw9nDfc29PMva2@WFV48qLbK9THshdnx8FOloMudCuHCK/EgkntLhXAhSwT1H9L7Dw@3L77IfSrh7UyPrFnJoZPdRgNv1dTvCASoPOAHOGfWp3YLCvPZ7dUYEQBNONKAhjQRVQITXACGsMcAigAAiSlVstDZLyx6J9X3GsQxURc9ZjSmnf7i@6XFxPlOMv2zN1e@xhPVqC2Oq43epLkwUeyFsk7XDZz3qYmZmpN5jxxWu7TKa4DB9bCRQMUuoQqafFurBKjNFOxkJpv/DNxBNDJjofzGfxWi4OljvJ60FxiHCiGcRlZznJMYElcT0Aw "Haskell – Try It Online")
```
r#(a:b)                      -- function '#' calculates the ant-diagonals of a matrix
                             -- where 'a' is the first row and 'b' all the others
                             -- as we recursively walk down the rows of the matrix,
                             -- 'r' holds the rows from before with the respective
                             -- head dropped
                             --
          [h|h:_<-a:r]       -- if the heads of the the current row and the rows
                             -- before
       (/=)=<<nub$           -- contain duplicates
    [1|                ]     -- make a singleton list [1] (else the empty list)
 sum                         -- and take the sum thereof
      +                      -- and add
             #               -- a recursive call with
 [t|_:t<-a:r]                -- the tails of the current row and the rows before
              b              -- and the rows below
                             --
[]#_=0                       -- base case if there aren't any tails anymore, return 0
a#_=a#[[]]                   -- if there are tails, but no further rows below,
                             -- continue with tails
h x=[]#x+[]#(reverse x)      -- main function, call '#' with input matrix 'x'
                             -- and the reverse of it to get the number of diagonals
                             -- and anti-diagonals. Recursion starts with no
                             -- rows before the 1st row.
-- example trace of function '#'
-- input matrix:
--   [[1,2,3,4],
--    [5,6,7,8],
--    [9,9,9,9]]
--
--  | r         a          b              a:r          heads   tails (r of next call)
-- -+----------------------------------------------------------------------------------
-- 1| []        [1,2,3,4]  [[5,6,7,8],    [[1,2,3,4]]  [1]     [[2,3,4]]
--  |                       [9,9,9,9]]
--  | 
-- 2| [[2,3,4]]  [5,6,7,8]  [[9,9,9,9]]   [[5,6,7,8],  [5,2]   [[6,7,8],
--  |                                      [2,3,4  ]]           [3,4  ]]
--  |
-- 3| [[6,7,8],  [9,9,9,9]  []            [[9,9,9,9],  [9,6,3] [[9,9,9],
--  |  [3,4  ]]                            [6,7,8  ],           [7,8  ]
--  |                                      [3,4    ],           [4    ]
--  |
--  | ....
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 9 bytes
```
#S≠uṁ∂Se↔
```
[Try it online!](https://tio.run/##yygtzv7/Xzn4UeeC0oc7Gx91NAWnPmqb8v///@hoQx0jHSiO1QHzjHVMdEx1zIA8MyBtAuQDZYE8uDogLxYA "Husk – Try It Online")
## Explanation
```
#S≠uṁ∂Se↔
      S   hook: S f g x = f x (g x)
       e  pair the matrix
        ↔ with its reverse
    ṁ∂    get the antidiagonals of each, in a single list
#         count all diagonals which satisfy the following:
 S        hook: S f g x = f x (g x)
  ≠       diagonal does not equal
   u      itself uniquified?
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~61~~ ~~56~~ 53 bytes
```
Ｆ²ＦＬθＦＬ§θ⁰Ｆ⟦⁻κ×⊖⊗ιλ⟧⊞υ⊞Ｏ⎇∧λ﹪⁺μιＬθ⊟υ⟦⟧§§θμλＩＬΦυ⊙ι‹⌕ιλμ
```
[Try it online!](https://tio.run/##VU9NS8QwEL3vr8hxAiPYdYuVPRUXQXCxh72FHGIbbTBNdvMh7q@v05aiZmAe7w1v5qXtVWi9suP47gODLWczvmj3kXq48P@8Ts@u099wQXbL15k4GpcjfCI7mUFHOOg26EG7pDs4@PxmCQ3nyCyXnDU59pBxxtezDir5ACcdnApXqF0HFtnRd9l6aCytHZAZ8v4GItL4M2RCIamtmf5kG@ZjnO83TTAuwaOKaf3Ck7FJhylB7a5gps0xkkqXzeSa3PPbj6MQosAKd7jFB@pUEjeMiRILku6pptHdIhZESqKzhNUiViQtwo48pZRyvPmyPw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｆ²
```
Loop over forward and reverse diagonals; `i=0` represents forward diagonals while `i=1` represents reverse diagonals.
```
ＦＬθ
```
Loop over each row index. This represents the index of the start of the diagonal.
```
ＦＬ§θ⁰«
```
Loop over each column index.
```
Ｆ⟦⁻κ×⊖⊗ιλ⟧
```
Calculate the row index of the diagonal at this column index. I use a `for` loop over a single-element array instead of an assignment as this avoids having to wrap the assignment into a block with the following statement, thus saving a byte.
```
⎇∧λ﹪⁺μιＬθ
```
Check whether this is the first column or the diagonal is about to wrap around between bottom and top.
```
⊟υ
```
If it isn't then pop the last list from the list of lists.
```
⟦⟧
```
if it is then start a new empty list.
```
⊞Ｏ...§§θμλ
```
Add the current diagonal entry to that list.
```
⊞υ
```
And push that list (back) to the list of lists.
```
ＩＬΦυ⊙ι‹⌕ιλμ
```
Count the number of lists that contain duplicates.
Let's take an example when `i=0` and `k=1`. This means that we've already collected two diagonals, `[[1,1,5,2],[9,4,3,5]]`. Here's our input:
```
 1 8 4 2 9 4 4 4
[5]1 2 7 7 4 2 3
 1 4 5 2 4 2 3 8
 8 5 4 2 3 4 1 5
```
We then loop `l` from `0` to `7`. This advances both the row and column by 1 each time:
```
 1 8 4 2 9 4 4 4
[5]1 2 7 7 4 2 3
 1[4]5 2 4 2 3 8
 8 5[4]2 3 4 1 5
```
The list is now `[[1,1,5,2],[9,4,3,5],[5,4,4]]`. However when `l` is `3`, we have `k+l=4`, a multiple of the height of the array. This means that we need to start a new list: `[[1,1,5,2],[9,4,3,5],[5,4,4],[]]`. We then continue to collect diagonal elements:
```
 1 8 4[2]9 4 4 4
[5]1 2 7[7]4 2 3
 1[4]5 2 4[2]3 8
 8 5[4]2 3 4[1]5
```
The list is now `[[1,1,5,2],[9,4,3,5],[5,4,4],[2,7,2,1]]`. Now when `l` is `7`, we have `k+l=8`, another multiple of the height of the array. This means that we need to start a new list, which ends up with the last element of that diagonal: `[[1,1,5,2],[9,4,3,5],[5,4,4],[2,7,2,1],[4]]`.
```
 1 8 4[2]9 4 4[4]
[5]1 2 7[7]4 2 3
 1[4]5 2 4[2]3 8
 8 5[4]2 3 4[1]5
```
By collecting wrapping diagonals starting at the first element of each row we eventually accumulate all of the diagonals of the array.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), ~~99 98 96 94~~ 83 bytes
```
Count[DuplicateFreeQ@Diagonal[#,i]~Table~{i,-t,t=#~Total~2}&/@{#,Reverse@#},1<0,2]&
```
[Try it online!](https://tio.run/##bVJRS8MwEH7Pr4gUisKNrt26dWgw4NiDT7rNp1IkjmwGtlRq5ktp/3q9pF1EMUfpd99dvrtLchLmXZ6EUTvRCUEZrQmp6xgSGL4GCHX@BKaQwsz5M0RTZDDD@T4bfSRQYfhdBHCzZ@JB8pK6ADTH/UV9fA7OHP8f9rq@GVutR6mvEkOGLSeoO7Xm4qlr2arYyGToy46Z9AxkjsvcuP0RxJD@zOYr4cGgzYdsixcDniPGHc0tIRTXQ3nWJneQ0sdSaco5XZ31zqhS5wKoaJdKHEotjm0Q0ojTtdAHmRs2CtptaZBOgI5MUdhYHcBafsnqU/LA1nPr9f56U1aGB1fsRaMqD8IbFypCQvas61tYnj@OeOlGriopn/mlaB6AKtqteDvKtlYwMmCYL9yEEf9dMr4bQ1KE3T7i9vUwZt/GGC3BsTN7JXiSDY0i@lQpbbpv "Wolfram Language (Mathematica) – Try It Online")
* `Function[a,a~Diagonal~#&/@Range[t=-#~Total~2,-t]]` gets all diagonals of `a`-- which works because `#~Total~2` is larger than any dimension of `a`.
[Answer]
# APL+WIN, 69 bytes
Prompts for a 2d matrix of the form 4 6⍴1 2 1 2 1 2 1 2 3 4 5 6 6 5 4 3 2 1 2 1 2 1 2 1
This yields:
```
1 2 1 2 1 2
1 2 3 4 5 6
6 5 4 3 2 1
2 1 2 1 2 1
+/~(v⍳¨v)≡¨⍳¨⍴¨v←(v←⊂[1](⌽0,⍳1↓n)⌽(n⍴0),m,((n←0 ¯1+↑⍴m)⍴0),⌽m←⎕)~¨0
```
[Try it online! Courtesy of Dyalog Classic](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob6qvz6O2CcZcjzra0/5r69dplD3q3XxoRZnmo86Fh1aA2Y96twAFgKo0QMSjrqZow1iNRz17DXSA0oaP2ibnaQJ5GnlAdQaaOrk6GkBm2wQDhUPrDbUftU0ECudqQuSAynJBRvRN1aw7tMLgP9DS/2lcJgpmQGlDBSMFZGysYKJgqmAGhKZAljGarCEA "APL (Dyalog Classic) – Try It Online")
Explanation:
```
(⌽0,⍳1↓n)⌽(n⍴0),m pad m with zeros to isolate diagonals
((n←0 ¯1+↑⍴m)⍴0),⌽m pad rotated m with zeros to isolate anti-diagonals
```
Yields:
```
1 2 1 2 1 2 0 0 0 2 1 2 1 2 1 0 0 0
0 1 2 3 4 5 6 0 0 0 6 5 4 3 2 1 0 0
0 0 6 5 4 3 2 1 0 0 0 1 2 3 4 5 6 0
0 0 0 2 1 2 1 2 1 0 0 0 1 2 1 2 1 2
v←(v←⊂[1](.....)~¨0 enclose the diagonals as a nested vector with padded zeros removed
+/~(v⍳¨v)≡¨⍳¨⍴¨v identify diagnols with duplicate entries and sum
```
[Answer]
# Perl 5, ~~89~~ 82 bytes
```
map{$i=0;map{$a[$x+$i].=$_;$b[@F-$x+$i++].=$_}/\d/g;$x++}@F;$_=grep/(.).*\1/,@a,@b
```
[TIO](https://tio.run/##K0gtyjH9/z83saBaJdPWwBrMSIxWqdBWyYzVs1WJt1ZJinZw0wULaGuDhWr1Y1L0062BQtq1Dm7WKvG26UWpBfoaepp6WjGG@joOiToOSf//WyoAIRcS@S@/oCQzP6/4v25iToGBgZt@TJ7@f11fUz0DQwA)
[Answer]
# TSQL, ~~140~~ 128 bytes
Found a way to golf 12 characters. This is no longer the longest solution.
Golfed:
```
SELECT sum(iif(y+x=j+i,1,0)+iif(y-x=j-i,1,0))FROM
@,(SELECT x i,y j,max(y)over()m,v w
FROM @)d WHERE(x*y=0or m=y)and v=w and x<i
```
Ungolfed:
```
DECLARE @ table(v int,x int,y int)
-- v = value
-- x = row 
-- y = column
INSERT @ values
(1,0,0),(2,0,1),(1,0,2),(2,0,3),(1,0,4),(2,0,5),
(1,1,0),(2,1,1),(3,1,2),(4,1,3),(5,1,4),(6,1,5),
(6,2,0),(5,2,1),(4,2,2),(3,2,3),(2,2,4),(1,2,5),
(2,3,0),(1,3,1),(2,3,2),(1,3,3),(2,3,4),(1,3,5)
SELECT sum(iif(y+x=j+i,1,0)+iif(y-x=j-i,1,0))
FROM @,(SELECT x i,y j,max(y)over()m,v w FROM @)d
WHERE
  (x*y=0or m=y)
  and v=w
  and x<i
```
**[Try it out](https://data.stackexchange.com/stackoverflow/query/973009/spot-all-antidiagonals-with-duplicated-values)**
]