The size of the input 2D-array will be between 3 and 1000.
Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.

we can use just map/unordered_map to find id of each edge fast in input array, and find cycle in (N+M) (DFS) after that find in O(N) max of indexes. So, first solution + maps should work total in O(N) or O(NlogN).