How many towers can you see? This question is based on the number-placement puzzle Towers (also known as Skyscrapers), which you can play online. Your goal is to take a solution to the puzzle and determine the clues -- the numbers of towers visible along each row and column. This is code golf, so fewest bytes wins.

How Towers works

The solution to a Towers puzzle is a Latin square -- a n*n grid in which every row and column contains a permutation of the numbers 1 through n. An example for n=5 is:

4 3 5 2 1 
5 4 1 3 2 
1 5 2 4 3 
2 1 3 5 4 
3 2 4 1 5 


Each row and column is labelled with a clue at each end like:

       2 3 1 4 5    
       v v v v v

 2 &gt;   4 3 5 2 1   &lt; 3
 1 &gt;   5 4 1 3 2   &lt; 4
 2 &gt;   1 5 2 4 3   &lt; 3
 3 &gt;   2 1 3 5 4   &lt; 2
 3 &gt;   3 2 4 1 5   &lt; 1 

       ^ ^ ^ ^ ^
       2 2 2 2 1


Each clue is a number from 1 to n that tells you how many towers you "see" looking along the row/column from that direction, if the numbers are treated as towers with that height. Each tower blocks shorter towers behind it. In other words, the towers you can see are the ones that are taller than any tower before them.



For example, let's look at the first row.

 2 &gt;   4 3 5 2 1   &lt; 3


It has a clue of 2 from the left because you can see the 4 and the 5. The 4 blocks the 3 from sight and the 5 blocks everything else. From the right, you can see 3 towers: 1, 2, and 5.

Program requirements

Write a program or function that takes in the grid of numbers and outputs or prints the clues, going clockwise from the top left.

Input

An n*n Latin-square with 2&lt;=n&lt;=9. 

The format is flexible. You can use any data structure that represents a grid or list containing numbers or digit characters. You may require a separator between the rows or no separator at all. Some possibilities are a list, a list of lists, a matrix, a token-separated string like

43521 54132 15243 21354 32415,


or a string without spaces. 

You're not given n as part of the input.

Output

Return or print the clues starting from the top left and going clockwise. So, first the upper clues reading rightwards, then the right clues reading downwards, then the lower clues reading leftwards, the the left clues reading upwards.

This would be 23145 34321 12222 33212 for the previous example

       2 3 1 4 5    
       v v v v v

 2 &gt;   4 3 5 2 1   &lt; 3
 1 &gt;   5 4 1 3 2   &lt; 4
 2 &gt;   1 5 2 4 3   &lt; 3
 3 &gt;   2 1 3 5 4   &lt; 2
 3 &gt;   3 2 4 1 5   &lt; 1 

       ^ ^ ^ ^ ^
       2 2 2 2 1


Just as for the input, you can use a list, string, or any ordered structure. The four "groups" can be separated or not, in a nested or a flat structure. But, the format must be the same for each group.

Example test cases:

(Your input/output format doesn't have to be the same as these.)

&gt;&gt; [[1 2] [2 1]]

[2 1]
[1 2]
[2 1]
[1 2]

&gt;&gt; [[3 1 2] [2 3 1] [1 2 3]]

[1 2 2]
[2 2 1]
[1 2 3]
[3 2 1]

&gt;&gt; [[4 3 5 2 1] [5 4 1 3 2] [1 5 2 4 3] [2 1 3 5 4] [3 2 4 1 5]]

[2 3 1 4 5]
[3 4 3 2 1]
[1 2 2 2 2]
[3 3 2 1 2]

&gt;&gt; [[2 6 4 1 3 7 5 8 9] [7 2 9 6 8 3 1 4 5] [5 9 7 4 6 1 8 2 3] [6 1 8 5 7 2 9 3 4] [1 5 3 9 2 6 4 7 8] [3 7 5 2 4 8 6 9 1] [8 3 1 7 9 4 2 5 6] [9 4 2 8 1 5 3 6 7] [4 8 6 3 5 9 7 1 2]]

[4 2 2 3 3 3 3 2 1]
[1 3 3 2 2 2 2 3 3]
[4 3 2 1 2 3 3 2 2]
[3 1 2 4 3 3 2 2 5]


For you convenience, here are the same test cases in a flat string format.

&gt;&gt; 1221

21
12
21
12

&gt;&gt; 312231123

122
221
123
321

&gt;&gt; 4352154132152432135432415

23145
34321
12222
33212

&gt;&gt; 264137589729683145597461823618572934153926478375248691831794256942815367486359712

422333321
133222233
432123322
312433225
APL 19

≢¨∪/⌈\(⍉⍪⌽⍪⊖∘⌽∘⍉⍪⊖)


(golfed a bit more after ngn's suggestion, thanks)

Explanation:

(⍉⍪⌽⍪⊖∘⌽∘⍉⍪⊖)  rotates matrix 4 times appending results
⌈\ gets maximums for each row up to current column (example: 4 2 3 5 1 gives 4 4 4 5 5)
≢¨∪/ counts unique elements for each row


Try it on tryapl.org