```
class Solution(object):
def findPaths(self, m, n, N, i, j):
"""
:type m: int
:type n: int
:type N: int
:type i: int
:type j: int
:rtype: int
"""
if not N: return 0
q=[(i,j,N)]
count=0
for i,j,total in q:
#print i,j,total
total-=1
if total<0:
continue
for I,J in (i+1,j),(i-1,j),(i,j+1),(i,j-1):
if I<0 or I>=m or J<0 or J>=n:
count+=1
else:
q.append((I,J,total))
return count
```