How great is your land? In this challenge, you'll calculate how great your land is.



Write a program or function that calculates the size of your land, given a wall you have built. You're given a non-empty input string containing a set of 4 distinct characters of your choice that represent the four directions "up", "down", "left" and "right" (I'll use ^ v &lt; &gt; in this challenge). It's not possible to take 180 degree turns (&lt;&gt; or ^v), but you may cross your wall.

The way you "capture" land is by encircling it with your wall. The wall itself is also considered part of your land. A few examples will make it more clear. I'll use o for land that has been encircled by the wall, x for the wall itself, and S for the starting point of the wall, just to illustrate how the wall is built. The output should be the total size of your land (the number of o, x and S in the test cases below). 

Input: &gt;&gt;&gt;&gt;
Land: Sxxxx
Output: 5

Input: &lt;&lt;&lt;^^^&gt;&gt;&gt;vv
Land:
xxxx
xoox
xoox
xxxS
Output: 16

Input: &lt;&lt;&lt;^^^&gt;&gt;&gt;v
Land:
xxxx
x  x
x  
xxxS 
Output: 11

Input: &lt;
Land: xS
Output: 2 

Input: &gt;&gt;&gt;&gt;&gt;&gt;vvvvvvvvv&lt;&lt;&lt;&lt;&lt;^^^^&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;vvvvvvvvvv&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;^^^^^^^^^&gt;&gt;&gt;vvvvvv&lt;&lt;&lt;&lt;&lt;
Land:
        Sxxxxxx
              x
              x
              x
              x  
         xxxxxxxxx
  xxxx   xoooox  x
  xoox   xoooox  x
  xoox   xoooox  x
  xoox   xxxxxx  x
  xoox           x
  xoox           x
xxxxxx           x
  x              x
  x              x
  xxxxxxxxxxxxxxxx
Output: 101

Input: &gt;&gt;vvvv&gt;&gt;^^&lt;&lt;&lt;&lt;^
Land:
Sxx
xox
xxxxx
  xox
  xxx
Output: 17

Input: &lt;&lt;^^^&gt;&gt;&gt;vv
Land:
xxxx
x  x
x  x
xxS
Output: 11   &lt;- Note, diagonal edges do not close the "loop"


Clarifications:


You do not need to draw the wall, the output should only be an integer
The input format is optional. You may take a string with &lt;&gt;^v, a list of digits, (1, -1, i, -i), a list of characters ABCD etc.




This is code-golf so the shortest code in each language wins. Remember, explanations are important, even in "regular" languages!
Python 2, 385 345 332 bytes



A,I,R=max,min,range
a=b=0
p=[[a,b]]
for i in input():a+=i%2*(2-i);b+=(1-i%2)*(1-i);p+=[a,b],
k,l=zip(*p)
x=A(k)-I(k)+3
y=A(l)-I(l)+3
o=[[1]*y for _ in' '*x]
def g(m,n):
 if 0&lt;o[m][n]and[m+I(k)-1,n+I(l)-1]not in p:o[m][n]=0;[g(i,j)for i in R(A(0,m-1),I(x,m+2))for j in R(A(0,n-1),I(y,n+2))if(i,j)!=(m,n)]
g(0,0)
print sum(map(sum,o))


Try it online! or Try all test cases

Input is numerical, 0~3, the 0-index of the symbols here: &gt;v&lt;^

#starting position
a,b=0
#new list to hold the wall coordinates
p=[[a,b]]

#iterate over the input calculating
#the next coordinate and storing on p
for i in input():
 a=a+i%2*(2-i)
 b=b+(1-i%2)*(1-i)
 p+=[[a,b]]
#i%2*(2-i) and (1-i%2)*(1-i) generate the increment
#of each symbol from last position 
# &gt;/0 : (0,1)
# v/1 : (1,0)
# &lt;/2 : (0,-1)
# ^/3 : (-1,0)

#transpose the coordinate list
k,l=zip(*p)
#calculate the difference between the max and min values
#to generate the total land size
#adding a border to avoid dead-ends
x=max(k)-min(k)+3
y=max(l)-min(l)+3

#create a matrix of 1's with the total land size
o=[([1]*y) for _ in ' '*x]

#recursive function that sets a cell to 0
#and call itself again on all surrounding cells
def g(m,n):
 #correct the indexes (like negative ones)
 a,b=m+min(k)-1,n+min(l)-1
 #if this cell contains 1 and don't belong to the wall
 if o[m][n]&gt;0 and (a,b) not in p:
  #sets to 0
  o[m][n]=0
  #call again on surrounding cells
  for i in range(max(0,m-1),min(x,m+2)):
   for j in range(max(0,n-1), min(y,n+2)):
    if (i,j)!=(m,n):g(i,j)

#call the recursive function o origin
g(0,0)
#print the sum of the cells
print sum(map(sum,o))


This is the resulting matrix:

[0, 0, 0, 0, 0, 0, 0, 0, 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