Loop Detection - not that kind! The goal of this challenge is to find the direction and area enclosed by a loop.
Input:
A rectangular grid consisting entirely of these characters: ^v&lt;&gt;
(Optionally, you may also be given the dimensions of the grid before the grid itself in decimal with a prefix, suffix, and separator character of your choice.)
A loop in the grid is a set of the aforementioned characters such that one points to the next, pointing to the next, eventually pointing back at the first character. For example:
&lt;&gt;&gt;v&gt;     &gt;&gt;v 
^^&lt;&gt;v     ^ &gt;v
&gt;^&lt;&lt;&lt;     ^&lt;&lt;&lt;
&gt;^&lt;v&gt;         

The left grid is the sample input; the right grid is the loop isolated.
The input grid will contain either no loops at all or one loop; you do not have to worry about any cases in which the grid contains more than one loop.
Output:
If the grid contains no loop, output X.
If the grid contains two arrows pointing at each other, output 0.
If the grid contains a counterclockwise loop, count the characters enclosed by the loop, including the border. Output that number.
If the grid contains a clockwise loop, follow the same process for the counterclockwise loop, but output the negative of that number. For instance, the above input grid would have an output of -11: 10 come from the loop itself, and 1 from the character enclosed by the loop.
This is code-golf. Shortest code wins.
Test cases:
&lt;&lt;^
^&gt;v
^v&lt;

Output X.
&lt;&lt;&lt;&lt;
&gt;&lt;&lt;&lt;
&gt;&gt;^&gt;

Output 0.
&lt;&gt;^^&lt;
&gt;&gt;&gt;v&gt;
&lt;^^&gt;v
&lt;^&gt;&gt;v
&gt;^&lt;&lt;&lt;

Output -15.
v&lt;&lt;&lt;&lt;
&gt;v&gt;&gt;^
v&lt;^&lt;&lt;
&gt;&gt;&gt;&gt;^

Output 20.
C#, 604 bytes

Complete program, accepts input (line-delimitated layout, no dimensions) from STDIN, outputs to STDOUT.


using C=System.Console;class P{static void Main(){int w=0,W,i,j,t,k,l,c;string D="",L;for(;(L=C.ReadLine())!=null;D+=L)w=L.Length;var R=new[]{-1,0,1,w,-w};L="X";for(W=i=D.Length;i--&gt;0;){var M=new int[W];for(k=j=i;i&gt;0;){M[j]=++k;t=j+R[c=D[j]%5];if(t&lt;0|t&gt;=W|c&lt;3&amp;t/w!=j/w|c&gt;2&amp;t%w!=j%w)break;j=t;if((l=M[j])&gt;0){var J=new int[W+1];System.Func&lt;int,int&gt;B=null,A=s=&gt;J[s]&lt;0?0:J[k=B(s)]=k==W?k:i;B=x=&gt;J[x]==x?x:B(J[x]);for(i=J[W]=W;i&gt;0;)J[--i]=M[i]&lt;l?i%w&lt;1|i%w&gt;w-2|i&lt;w|i&gt;W-w?W:i:-1;for(;i&lt;W;)if(J[++i]&lt;0)l=D[i]%5/2-1;else{A(i-1);if(i&gt;w)A(i-w);}for(c=W;i--&gt;0;L=""+(c&gt;2?c:0)*l)c-=J[i]&lt;0?0:B(i)/W;}}}C.WriteLine(L);}}


The program works by first reading in the layout, needless to say, and then iterating over every cell. We then run a 'snake' from each cell, which follows the arrows until it runs off the edge, or runs into itself. If it runs into itself, then we know we have found a loop (or one of those ">&lt;" things), and it also knows how much of the snake is in the loop.

Once we know we have a loop, we know which cells are on the loop, and we create a map from each cell (+1, for reasons) to either itself, -1 (means it's on the loop), or W (the whole width) if it's on the edge (or the +1 (which is at index W) to simplify things further one).

While we do this, we also find the direction that the 'last' element of the loop has (that is, the last element of the loop on the last row that has elements from the loop on it). This element must be a "&lt;" or an "^", and this tells us the clockness (CW/CCW) of the loop (translated to -1/+1).

We then perform a disjoin set pass, which assigns all the elements that are outside the loop to the W set. We then subtract how many of these there are from W to get the number contained on and in the loop. If this number is less than 3, we replace it with 0. We multiply this by the clockness, set it as the result, and somehow escape from the for loops, where the result is output.

If, however, most of the above never happens (because no snake ever finds itself), then the result remains as "X", and that is outputted.

using C=System.Console;

class P
{
    static void Main()
    {
        int w=0, // width
        W, // full length
        i, // used for iterating over all the cells
        j, // keeps track of where the snake as got to
        t, // t is next j
        k, // how far along the snake we are, kind of
        // later on, k is used as temp for A
        l, // stores a threshold for how far along the snake the loop starts
        // later on, l stores the last seen pointer - this tells us the clockness
        c; // the translated direction
        // later on, c is a backwards-count

        string D="", // D is the map
        L; // used for reading lines, and then storing the result

        // might not be the best yay of doing this
        for(;(L=C.ReadLine())!=null; // read a line, while we can
            D+=L) // add the line to the map
            w=L.Length; // record the width

        var R=new[]{-1,0,1,w,-w}; // direction table (char%5) - might be able to replace this array with some bit bashing/ternary

        L="X"; // can't seem to fit this in anywhere... (don't strictly need to re-use L)
        for(W=i=D.Length;i--&gt;0;) // for each cell, we send a 'snake' to try to find the loop from that cell
        {
            var M=new int[W]; // stores how far along the snake this point is

            for(k=j=i; // k's value doesn't really matter, as long as it's not stupidly big
                i&gt;0;) // the i&gt;0 check is just for when we return (see comment at the end of the code)
            {
                M[j]=++k; // store snake point and advance distance

                t=j+R[c=D[j]%5]; // t is position after move (translate &lt;&gt;v^ to 0234 (c is direction))
                //c=D[j]%5; // translate &lt;&gt;v^ to 0234 (c is direction)
                //t=j+R[c]; // t is position after move
                if(t&lt;0|t&gt;=W|c&lt;3&amp;t/w!=j/w|c&gt;2&amp;t%w!=j%w)
                    break; // hit an edge - will always happen if we don't find a loop - give up on this snake
                j=t; // move to new position

                if((l=M[j])&gt;0) // we've been here before...
                {
                    // disjoint sets (assign all the edges to one set, assign all the ones on the line to another set, do adjacent disjoint, return size-outteredge (minus if necessary)
                    var J=new int[W+1]; // looks like we can reuse M for this

                    System.Func&lt;int,int&gt;B=null,
                    // whatever s points at should point to i, unless s points to W, in which case it should keep point to W
                    A=s=&gt;J[s]&lt;0?0:J[k=B(s)]=k==W?k:i;
                    // read the value this points to
                    B=x=&gt;J[x]==x?x:B(J[x]);

                    for(i=J[W]=W;i&gt;0;)
                        J[--i]=M[i]&lt;l? // if we are not part of the loop
                            i%w&lt;1|i%w&gt;w-2|i&lt;w|i&gt;W-w? // if we are on the edge
                                W: // on the edge
                                i: // not on the edge
                             -1; // this is on the loop

                    // now fill in
                    // we don't have to worry about wrapping, the important bit being an un-wrapping closed loop
                    // i = 0
                    for(;i&lt;W;)
                        if(J[++i]&lt;0) // we are on the loop
                            l=D[i]%5/2-1; // last one must be ^(4) or &lt;(0)
                        else{ // can probably crush this into an l returning l assigning term (with if above)
                            A(i-1);
                            if(i&gt;w)
                                A(i-w);
                        }

                    // now count the number of non-edges
                    for(c=W; // assume everything is a non-edge
                        i--&gt;0;
                        L=""+(c&gt;2?c:0)*l) // set output to be number of non-edges * clockness (or 0 if too few)
                        c-=J[i]&lt;0?0:B(i)/W; // subtract 1 if an edge (B(i) is W), othewise 0

                    // at this point, i is 0, so we will fall out of all the loops
                }
            }
        }

        C.WriteLine(L); // output result
    }
}