You are playing some computer game. One of its levels puts you in a maze consisting of n lines, each of which contains m cells. Each cell either is free or is occupied by an obstacle. The starting cell is in the row r and column c. In one step you can move one square up, left, down or right, if the target cell is not occupied by an obstacle. You can't move beyond the boundaries of the labyrinth.

Unfortunately, your keyboard is about to break, so you can move left no more than x times and move right no more than y times. There are no restrictions on the number of moves up and down since the keys used to move up and down are in perfect condition.

Now you would like to determine for each cell whether there exists a sequence of moves that will put you from the starting cell to this particular one. How many cells of the board have this property?


-----Input-----

The first line contains two integers n, m (1 ≤ n, m ≤ 2000) — the number of rows and the number columns in the labyrinth respectively.

The second line contains two integers r, c (1 ≤ r ≤ n, 1 ≤ c ≤ m) — index of the row and index of the column that define the starting cell.

The third line contains two integers x, y (0 ≤ x, y ≤ 10^9) — the maximum allowed number of movements to the left and to the right respectively.

The next n lines describe the labyrinth. Each of them has length of m and consists only of symbols '.' and '*'. The j-th character of the i-th line corresponds to the cell of labyrinth at row i and column j. Symbol '.' denotes the free cell, while symbol '*' denotes the cell with an obstacle.

It is guaranteed, that the starting cell contains no obstacles.


-----Output-----

Print exactly one integer — the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself.


-----Examples-----
Input
4 5
3 2
1 2
.....
.***.
...**
*....

Output
10

Input
4 4
2 2
0 1
....
..*.
....
....

Output
7



-----Note-----

Cells, reachable in the corresponding example, are marked with '+'.

First example:  

+++..

+***.

+++**

*+++.

 

Second example:  

.++.

.+*.

.++.

.++.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Graph algorithms

Shortest paths


Solutions:
from collections import deque
(n, m) = map(int, input().split())
(r, c) = map(int, input().split())
(x, y) = map(int, input().split())
gr = []
vd = [[0] * m for i in range(n)]
r -= 1
c -= 1
ans = 0
for i in range(n):
	gr.append(input())
to_visit = deque()
to_visit.append((r, c, x, y))
while to_visit:
	(ri, ci, xi, yi) = to_visit.popleft()
	ru = ri
	while ru >= 0 and gr[ru][ci] == '.' and (not vd[ru][ci]):
		vd[ru][ci] = 1
		ans += 1
		if xi > 0 and ci - 1 >= 0 and (gr[ru][ci - 1] == '.') and (not vd[ru][ci - 1]):
			to_visit.append((ru, ci - 1, xi - 1, yi))
		if yi > 0 and ci + 1 < m and (gr[ru][ci + 1] == '.') and (not vd[ru][ci + 1]):
			to_visit.append((ru, ci + 1, xi, yi - 1))
		ru -= 1
	rd = ri + 1
	while rd < n and gr[rd][ci] == '.' and (not vd[rd][ci]):
		vd[rd][ci] = 1
		ans += 1
		if xi > 0 and ci - 1 >= 0 and (gr[rd][ci - 1] == '.') and (not vd[rd][ci - 1]):
			to_visit.append((rd, ci - 1, xi - 1, yi))
		if yi > 0 and ci + 1 < m and (gr[rd][ci + 1] == '.') and (not vd[rd][ci + 1]):
			to_visit.append((rd, ci + 1, xi, yi - 1))
		rd += 1
print(ans)


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(n, m) = map(int, input().split())
(r, c) = map(int, input().split())
r -= 1
c -= 1
(x1, y1) = map(int, input().split())
x1 = min(x1, n * m + 1)
y1 = min(y1, n * m + 1)
maze = []
for i in range(n):
	maze.append(list(input()))
left = [[4 * n * m for i in range(m)] for j in range(n)]

def bfszeroone():
	s = deque([])
	s.append((r, c))
	left[r][c] = 0
	while len(s) > 0:
		(x, y) = s.popleft()
		d = left[x][y]
		if x > 0 and left[x - 1][y] > d and (maze[x - 1][y] == '.'):
			left[x - 1][y] = d
			s.appendleft((x - 1, y))
		if x < n - 1 and left[x + 1][y] > d and (maze[x + 1][y] == '.'):
			left[x + 1][y] = d
			s.appendleft((x + 1, y))
		if y > 0 and left[x][y - 1] > d + 1 and (maze[x][y - 1] == '.'):
			left[x][y - 1] = d + 1
			s.append((x, y - 1))
		if y < m - 1 and left[x][y + 1] > d + 1 and (maze[x][y + 1] == '.'):
			left[x][y + 1] = d + 1
			s.append((x, y + 1))
bfszeroone()
ans = 0
for i in range(n):
	for j in range(m):
		lt = (left[i][j] + c - j) // 2
		right = (left[i][j] - c + j) // 2
		if right <= y1 and lt <= x1:
			ans += 1
print(ans)


import collections
(n, m) = map(int, input().split())
(r, c) = map(int, input().split())
(x, y) = map(int, input().split())
arr = []
for i in range(n):
	arr.append(input())
que = collections.deque([(r - 1, c - 1)])
dist = [[float('inf')] * m for i in range(n)]
dist[r - 1][c - 1] = 0
while que:
	v = que.popleft()
	if v[0] > 0 and arr[v[0] - 1][v[1]] != '*' and (dist[v[0] - 1][v[1]] > dist[v[0]][v[1]]):
		dist[v[0] - 1][v[1]] = dist[v[0]][v[1]]
		que.appendleft((v[0] - 1, v[1]))
	if v[0] < n - 1 and arr[v[0] + 1][v[1]] != '*' and (dist[v[0] + 1][v[1]] > dist[v[0]][v[1]]):
		dist[v[0] + 1][v[1]] = dist[v[0]][v[1]]
		que.appendleft((v[0] + 1, v[1]))
	if v[1] < m - 1 and arr[v[0]][v[1] + 1] != '*' and (dist[v[0]][v[1] + 1] > dist[v[0]][v[1]]):
		dist[v[0]][v[1] + 1] = dist[v[0]][v[1]]
		que.appendleft((v[0], v[1] + 1))
	if v[1] > 0 and arr[v[0]][v[1] - 1] != '*' and (dist[v[0]][v[1] - 1] > dist[v[0]][v[1]] + 1):
		dist[v[0]][v[1] - 1] = dist[v[0]][v[1]] + 1
		que.append((v[0], v[1] - 1))
count = 0
for i in range(n):
	for e in range(m):
		if dist[i][e] <= x and e - (c - 1) + dist[i][e] <= y:
			count += 1
print(count)


from collections import deque
(n, m) = [int(x) for x in input().split()]
(x, y) = [int(x) for x in input().split()]
(left, right) = [int(x) for x in input().split()]
s = []
ans = 0
d = [None] * 2007
for i in range(0, 2007):
	d[i] = [0 for j in range(0, 2007)]
for i in range(0, n):
	s.append(input())
q = deque()
q.append([x - 1, y - 1, left, right])
while len(q):
	v = q[0]
	q.popleft()
	if v[0] < 0 or v[0] >= n or v[1] < 0 or (v[1] >= m) or (d[v[0]][v[1]] == 1) or (v[2] < 0) or (v[3] < 0) or (s[v[0]][v[1]] == '*'):
		continue
	d[v[0]][v[1]] = 1
	q.appendleft([v[0] + 1, v[1], v[2], v[3]])
	q.appendleft([v[0] - 1, v[1], v[2], v[3]])
	q.append([v[0], v[1] + 1, v[2], v[3] - 1])
	q.append([v[0], v[1] - 1, v[2] - 1, v[3]])
for i in range(0, n):
	for j in range(0, m):
		ans += d[i][j]
print(ans)


from collections import deque

def put():
	return map(int, input().split())

def safe(i, j):
	return i >= 0 and j >= 0 and (i < n) and (j < m) and (vis[i][j] == 0) and (mat[i][j] == '.')
(n, m) = put()
(sx, sy) = put()
(l, r) = put()
(sx, sy) = (sx - 1, sy - 1)
mat = [input() for _ in range(n)]
vis = [[0] * m for _ in range(n)]
q = deque()
move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
ans = 0
if safe(sx, sy):
	vis[sx][sy] = 1
	q.append((sx, sy, 0))
while q:
	(x, y, s) = q.popleft()
	R = s + (y - sy)
	L = s - (y - sy)
	if R <= 2 * r and L <= 2 * l:
		ans += 1
	for (dx, dy) in move:
		(i, j) = (x + dx, y + dy)
		if safe(i, j):
			vis[i][j] = 1
			if dx == 0:
				q.append((i, j, s + 1))
			else:
				q.appendleft((i, j, s))
print(ans)


from collections import deque
(n, m) = map(int, input().split())
(r, c) = map(int, input().split())
r -= 1
c -= 1
(x, y) = map(int, input().split())
grid = []
for i in range(n):
	s = input()
	grid.append(s)
visited = []
for i in range(n):
	visited.append([0] * m)
visited[r][c] = 1
stack = deque([(r, c, 0)])
stack2 = []
ans = 0
while stack or stack2:
	if not stack:
		for (i, j, k) in stack2:
			stack.append((i, j, k))
		stack2 = []
	(i, j, val) = stack.popleft()
	if visited[i][j] != val + 1:
		continue
	ans += 1
	visited[i][j] = 1
	if i > 0 and grid[i - 1][j] == '.' and (not visited[i - 1][j] or visited[i - 1][j] > val + 1):
		stack.append((i - 1, j, val))
		visited[i - 1][j] = val + 1
	if i < n - 1 and grid[i + 1][j] == '.' and (not visited[i + 1][j] or visited[i + 1][j] > val + 1):
		stack.append((i + 1, j, val))
		visited[i + 1][j] = val + 1
	if j > 0 and grid[i][j - 1] == '.' and (not (val + 1 > x or j - 1 + val + 1 - c > y)) and (not visited[i][j - 1] or visited[i][j - 1] > val + 2):
		stack2.append((i, j - 1, val + 1))
		visited[i][j - 1] = val + 2
	if j < m - 1 and grid[i][j + 1] == '.' and (not (val > x or j + 1 + val - c > y)) and (not visited[i][j + 1] or visited[i][j + 1] > val + 1):
		stack.append((i, j + 1, val))
		visited[i][j + 1] = val + 1
print(ans)


(n, m) = map(int, input().split())
(r, c) = map(int, input().split())
(r, c) = (r - 1, c - 1)
(X, Y) = map(int, input().split())
S = [input() for i in range(n)]
L = [[-1] * m for i in range(n)]
R = [[-1] * m for i in range(n)]
from collections import deque
q = deque([])
q.append((r, c))
L[r][c] = 0
R[r][c] = 0
while q:
	(y, x) = q.popleft()
	for dy in (-1, 1):
		(ny, x) = (y + dy, x)
		if 0 <= ny < n:
			if S[ny][x] == '*':
				continue
			if L[ny][x] == -1 and R[ny][x] == -1:
				q.appendleft((ny, x))
				L[ny][x] = L[y][x]
				R[ny][x] = R[y][x]
			if L[ny][x] > L[y][x]:
				L[ny][x] = L[y][x]
			if R[ny][x] > R[y][x]:
				R[ny][x] = R[y][x]
	for dx in (-1, 1):
		(y, nx) = (y, x + dx)
		if 0 <= nx < m:
			if S[y][nx] == '*':
				continue
			if L[y][nx] == -1 and R[y][nx] == -1:
				q.append((y, nx))
				if dx == 1:
					R[y][nx] = R[y][x] + 1
					L[y][nx] = L[y][x]
				else:
					L[y][nx] = L[y][x] + 1
					R[y][nx] = R[y][x]
			if L[y][nx] > L[y][x] + 1:
				L[y][nx] = L[y][x] + 1
			if R[y][nx] > R[y][x] + 1:
				R[y][nx] = R[y][x] + 1
ans = 0
for i in range(n):
	for j in range(m):
		if L[i][j] == -1 or R[i][j] == -1:
			continue
		if L[i][j] <= X and R[i][j] <= Y:
			ans += 1
print(ans)


import os, sys
from io import BytesIO, IOBase
from collections import deque

def short(n, m, X, Y, arr, r, c, where):
	dist = [[0] * (m + 1) for _ in range(n + 1)]
	for i in range(n + 1):
		for j in range(m + 1):
			if i == n or m == j or arr[i][j] == '*':
				dist[i][j] = 1
	(dist[r][c], dx, dy, where[r][c]) = (1, [0, 0, -1, 1], [-1, 1, 0, 0], 1)
	curr = deque([(0, 0, r, c)])
	while len(curr):
		(l, r, x, y) = curr.popleft()
		if l > X or r > Y:
			continue
		where[x][y] = 1
		for i in range(4):
			(x1, y1) = (x + dx[i], y + dy[i])
			if dist[x1][y1]:
				continue
			dist[x1][y1] = 1
			if not i or i == 1:
				curr.append((l + (i ^ 1), r + i, x1, y1))
			else:
				curr.appendleft((l, r, x1, y1))

def main():
	(n, m) = map(int, input().split())
	(r, c) = map(lambda xx: int(xx) - 1, input().split())
	(x, y) = map(int, input().split())
	arr = [input().strip() for _ in range(n)]
	where = [[0] * m for _ in range(n)]
	short(n, m, x, y, arr, r, c, where)
	print(sum((i.count(1) for i in where)))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import defaultdict, deque
(n, m) = [int(el) for el in input().split()]
(r, c) = [int(el) for el in input().split()]
(x, y) = [int(el) for el in input().split()]
s = ['*' * (m + 2)]
for i in range(n):
	q = input()
	w = '*' + q + '*'
	s.append(w)
s.append('*' * (m + 2))

def bfs(way):
	ans = 0
	while len(way) != 0:
		(x, y, l, r) = way.pop()
		if visited[x - 1][y] == 0 and s[x - 1][y] == '.':
			way.append([x - 1, y, l, r])
			visited[x - 1][y] = 1
			ans += 1
		if visited[x + 1][y] == 0 and s[x + 1][y] == '.':
			way.append([x + 1, y, l, r])
			visited[x + 1][y] = 1
			ans += 1
		if visited[x][y - 1] == 0 and s[x][y - 1] == '.' and (l > 0):
			way.appendleft([x, y - 1, l - 1, r])
			visited[x][y - 1] = 1
			ans += 1
		if visited[x][y + 1] == 0 and s[x][y + 1] == '.' and (r > 0):
			way.appendleft([x, y + 1, l, r - 1])
			visited[x][y + 1] = 1
			ans += 1
	return ans
way = deque()
way.append((r, c, x, y))
visited = [[0 for i in range(m + 2)] for j in range(n + 2)]
visited[r][c] = 1
ans = bfs(way)
print(ans + 1)


from collections import deque

def move(tx, ty, s, left=False):
	if 0 <= tx < n and 0 <= ty < m:
		if smap[tx][ty] == '.' and mp[tx][ty] == -1:
			mp[tx][ty] = s
			if left:
				q.appendleft((tx, ty))
			else:
				q.append((tx, ty))
[n, m] = map(int, input().split())
[sx, sy] = map(int, input().split())
[ll, rr] = map(int, input().split())
smap = [input() for i in range(n)]
q = deque()
mp = [[-1] * m for i in range(n)]
(sx, sy) = (sx - 1, sy - 1)
mp[sx][sy] = 0
ans = 0
q.append((sx, sy))
while q:
	(px, py) = q.popleft()
	s = mp[px][py]
	ans += s + py - sy <= rr * 2 and s - py + sy <= ll * 2
	move(px + 1, py, s, left=True)
	move(px - 1, py, s, left=True)
	move(px, py - 1, s + 1)
	move(px, py + 1, s + 1)
print(ans)


import sys
from collections import deque
from math import inf

def rint():
	return map(int, sys.stdin.readline().split())

def bfs(sr, sc):
	global ans
	q = deque()
	q.append((x, y, sr, sc))
	visit[sr][sc] = 1
	ans += 1
	while q:
		(ll, rr, r, c) = q.popleft()
		nadj = []
		if r + 1 < n and maze[r + 1][c] == '.':
			nadj.append([r + 1, c])
		if r - 1 >= 0 and maze[r - 1][c] == '.':
			nadj.append([r - 1, c])
		if c + 1 < m and maze[r][c + 1] == '.':
			nadj.append([r, c + 1])
		if c - 1 >= 0 and maze[r][c - 1] == '.':
			nadj.append([r, c - 1])
		for (adr, adc) in nadj:
			if visit[adr][adc] == 1:
				continue
			if c == adc:
				visit[adr][adc] = 1
				ans += 1
				q.appendleft([ll, rr, adr, adc])
			elif adc < c:
				if ll - 1 >= 0:
					visit[adr][adc] = 1
					ans += 1
					q.append([ll - 1, rr, adr, adc])
			elif rr - 1 >= 0:
				visit[adr][adc] = 1
				ans += 1
				q.append([ll, rr - 1, adr, adc])
(n, m) = rint()
(sr, sc) = rint()
(x, y) = rint()
sr -= 1
sc -= 1
maze = []
for i in range(n):
	maze.append(input())
visit = [[0 for i in range(m)] for j in range(n)]
ans = 0
bfs(sr, sc)
print(ans)


from collections import deque
(n, m) = map(int, input().split())
(sx, sy) = map(int, input().split())
(tl, tr) = map(int, input().split())
g = [input() for i in range(n)]
sx -= 1
sy -= 1
dis = [[-1 for j in range(m)] for i in range(n)]
q = deque()

def go(x, y, s, left=False):
	if 0 <= x < n and 0 <= y < m:
		if g[x][y] == '.' and dis[x][y] == -1:
			dis[x][y] = s
			if left:
				q.appendleft((x, y))
			else:
				q.append((x, y))
go(sx, sy, 0)
ans = 0
while q:
	(x, y) = q.popleft()
	s = dis[x][y]
	ans += s + y - sy <= tr * 2 and s - y + sy <= tl * 2
	go(x - 1, y, s, left=True)
	go(x + 1, y, s, left=True)
	go(x, y - 1, s + 1)
	go(x, y + 1, s + 1)
print(ans)


from collections import deque
(n, m) = list(map(int, input().split()))
(sx, sy) = list(map(int, input().split()))
(x, y) = list(map(int, input().split()))
smap = [input() for i in range(n)]
mp = [[-1] * m for i in range(n)]
(sx, sy) = (sx - 1, sy - 1)
mp[sx][sy] = 0
ans = 0
q = deque()
q.append((sx, sy))

def move(px, py, s, left):
	if px >= 0 and px < n and (py >= 0) and (py < m):
		if smap[px][py] == '.' and mp[px][py] == -1:
			mp[px][py] = s
			if left:
				q.appendleft((px, py))
			else:
				q.append((px, py))
while q:
	(px, py) = q.popleft()
	s = mp[px][py]
	ans += s + py - sy <= y * 2 and s - py + sy <= x * 2
	move(px + 1, py, s, left=True)
	move(px - 1, py, s, left=True)
	move(px, py + 1, s + 1, left=False)
	move(px, py - 1, s + 1, left=False)
print(ans)


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
import sys
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n // i
	if n > 2:
		l.append(n)
	return list(set(l))

def mapin():
	return map(int, input().split())
(n, m) = mapin()
(r, c) = mapin()
(x, y) = mapin()
dp = []
for i in range(n):
	s = input()
	dp.append(s)
ans = [[0 for i in range(2005)] for j in range(2005)]
q = deque()
q.append([r - 1, c - 1, x, y])
while len(q):
	temp = q[0]
	q.popleft()
	if temp[0] < 0 or temp[0] >= n or temp[1] < 0 or (temp[1] >= m):
		continue
	if ans[temp[0]][temp[1]] == 1:
		continue
	if dp[temp[0]][temp[1]] == '*':
		continue
	if temp[2] < 0 or temp[3] < 0:
		continue
	ans[temp[0]][temp[1]] = 1
	q.appendleft([temp[0] + 1, temp[1], temp[2], temp[3]])
	q.appendleft([temp[0] - 1, temp[1], temp[2], temp[3]])
	q.append([temp[0], temp[1] + 1, temp[2], temp[3] - 1])
	q.append([temp[0], temp[1] - 1, temp[2] - 1, temp[3]])
out = 0
for i in ans:
	out += sum(i)
print(out)


from collections import deque
(n, m) = list(map(int, input().split()))
(start_x, start_y) = list(map(int, input().split()))
(l, r) = list(map(int, input().split()))
maze = []
for _ in range(n):
	maze.append(input())
costs = [[float('inf')] * m for i in range(n)]
costs[start_x - 1][start_y - 1] = 0
q = deque([(start_x - 1, start_y - 1)])
while q:
	(x, y) = q.popleft()
	if x > 0 and maze[x - 1][y] == '.' and (costs[x][y] < costs[x - 1][y]):
		q.append((x - 1, y))
		costs[x - 1][y] = costs[x][y]
	if x < n - 1 and maze[x + 1][y] == '.' and (costs[x][y] < costs[x + 1][y]):
		q.append((x + 1, y))
		costs[x + 1][y] = costs[x][y]
	if l > 0 and y > 0 and (maze[x][y - 1] == '.') and (costs[x][y - 1] > costs[x][y]):
		q.append((x, y - 1))
		costs[x][y - 1] = costs[x][y]
	if r > 0 and y < m - 1 and (maze[x][y + 1] == '.') and (costs[x][y + 1] > costs[x][y] + 1) and (costs[x][y] < r):
		q.append((x, y + 1))
		costs[x][y + 1] = costs[x][y] + 1
total = 0
for i in range(n):
	for j in range(m):
		if costs[i][j] + (start_y - 1) - j <= l:
			total += 1
print(total)


from collections import deque
I = lambda : map(int, input().split())
(n, m) = I()
(xfinal, yfinal) = I()
xfinal -= 1
yfinal -= 1
(left, right) = I()
arr = [[0 for i in range(m)] for j in range(n)]
visit = [[0 for i in range(m)] for j in range(n)]
stack = deque()
global answer

def floodfill(xini, yini, l, r):
	if arr[xini][yini] != '*':
		stack.append([xini, yini, l, r])
		visit[xini][yini] = 1
		answer = 1
		while stack:
			t = stack.pop()
			p1 = t[0]
			p2 = t[1]
			p3 = t[2]
			p4 = t[3]
			if p2 + 1 < m and p4 > 0 and (visit[p1][p2 + 1] == 0) and (arr[p1][p2 + 1] == '.'):
				stack.appendleft([p1, p2 + 1, p3, p4 - 1])
				visit[p1][p2 + 1] = 1
				answer += 1
			if p2 - 1 >= 0 and p3 > 0 and (visit[p1][p2 - 1] == 0) and (arr[p1][p2 - 1] == '.'):
				stack.appendleft([p1, p2 - 1, p3 - 1, p4])
				visit[p1][p2 - 1] = 1
				answer += 1
			if p1 + 1 < n and visit[p1 + 1][p2] == 0 and (arr[p1 + 1][p2] == '.'):
				stack.append([p1 + 1, p2, p3, p4])
				visit[p1 + 1][p2] = 1
				answer += 1
			if p1 - 1 >= 0 and visit[p1 - 1][p2] == 0 and (arr[p1 - 1][p2] == '.'):
				stack.append([p1 - 1, p2, p3, p4])
				visit[p1 - 1][p2] = 1
				answer += 1
		print(answer)
for i in range(n):
	li = input()
	li = list(li)
	for j in range(m):
		arr[i][j] = li[j]
floodfill(xfinal, yfinal, left, right)


from collections import deque
(n, m) = map(int, input().split())
(r, c) = map(int, input().split())
r -= 1
c -= 1
(x, y) = map(int, input().split())
import sys
a = [[k for k in l.strip()] for l in sys.stdin]
able = [[-1] * m for i in range(n)]
able[r][c] = x + y
q = deque()

def dfs(x, y, e, f):
	i = 1
	k = []
	kb = []
	if x + i <= n - 1 and a[x + i][y] == '.':
		if e + f > able[x + i][y]:
			able[x + i][y] = e + f
			q.append([x + i, y, e, f])
	if x - i >= 0 and a[x - i][y] == '.':
		if e + f > able[x - i][y]:
			able[x - i][y] = e + f
			q.append([x - i, y, e, f])
	if y + 1 <= m - 1 and f >= 1 and (a[x][y + 1] == '.'):
		if e + f - 1 > able[x][y + 1]:
			able[x][y + 1] = e + f - 1
			q.append([x, y + 1, e, f - 1])
	if y - 1 >= 0 and e >= 1 and (a[x][y - 1] == '.'):
		if e + f - 1 > able[x][y - 1]:
			able[x][y - 1] = e + f - 1
			q.append([x, y - 1, e - 1, f])
dfs(r, c, x, y)
while q:
	way = q.popleft()
	dfs(way[0], way[1], way[2], way[3])
ans = 0
for i in able:
	for j in i:
		if j != -1:
			ans += 1
print(ans)


def main():
	(n, m) = list(map(int, input().split()))
	(r, c) = list(map(int, input().split()))
	(L, R) = list(map(int, input().split()))
	lab = [input() for _ in range(n)]
	r -= 1
	c -= 1
	inf = n * m + 1
	dist = [[inf] * m for _ in range(n)]
	dist[r][c] = 0
	Q = [(r, c)]
	for (x, y) in Q:
		d = dist[x][y]
		if x < n - 1 and dist[x + 1][y] > d and (lab[x + 1][y] == '.'):
			dist[x + 1][y] = d
			Q.append((x + 1, y))
		if 0 < x and dist[x - 1][y] > d and (lab[x - 1][y] == '.'):
			dist[x - 1][y] = d
			Q.append((x - 1, y))
		if 0 < y and dist[x][y - 1] > d and (lab[x][y - 1] == '.'):
			dist[x][y - 1] = d + 1
			Q.append((x, y - 1))
		if y < m - 1 and dist[x][y + 1] > d and (lab[x][y + 1] == '.'):
			dist[x][y + 1] = d + 1
			Q.append((x, y + 1))
	ans = 0
	for i in range(n):
		for j in range(m):
			d = dist[i][j]
			if d < inf:
				right = (d + (j - c)) // 2
				left = (d - (j - c)) // 2
				if right <= R and left <= L:
					ans += 1
	print(ans)
main()


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict, deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, m) = map(int, input().split())
(r, c) = map(int, input().split())
r -= 1
c -= 1
(x1, y1) = map(int, input().split())
maze = [input() for i in range(n)]
left = [[n * m + 1 for i in range(m)] for j in range(n)]
s = []
s.append((r, c))
left[r][c] = 0
for (x, y) in s:
	d = left[x][y]
	if x > 0 and left[x - 1][y] > d and (maze[x - 1][y] == '.'):
		left[x - 1][y] = d
		s.append((x - 1, y))
	if x < n - 1 and left[x + 1][y] > d and (maze[x + 1][y] == '.'):
		left[x + 1][y] = d
		s.append((x + 1, y))
	if y > 0 and left[x][y - 1] > d + 1 and (maze[x][y - 1] == '.'):
		left[x][y - 1] = d + 1
		s.append((x, y - 1))
	if y < m - 1 and left[x][y + 1] > d + 1 and (maze[x][y + 1] == '.'):
		left[x][y + 1] = d + 1
		s.append((x, y + 1))
ans = 0
for i in range(n):
	for j in range(m):
		lt = (left[i][j] + c - j) // 2
		right = (left[i][j] - c + j) // 2
		if right <= y1 and lt <= x1 and (left[i][j] < n * m + 1):
			ans += 1
print(ans)


import sys
from collections import deque
input = sys.stdin.readline
(n, m) = map(int, input().split())
(sy, sx) = map(int, input().split())
sy -= 1
sx -= 1
(can_l, can_r) = map(int, input().split())
a = [input().rstrip() for i in range(n)]
d = [[-1] * m for i in range(n)]
dq = deque()
dq.append([sy, sx])
d[sy][sx] = 0
ans = 0
while dq:
	(y, x) = dq.popleft()
	s = d[y][x]
	ans += s + x - sx <= can_r * 2 and s - x + sx <= can_l * 2
	if x + 1 < m and a[y][x + 1] == '.' and (d[y][x + 1] == -1):
		d[y][x + 1] = s + 1
		dq.append([y, x + 1])
	if x - 1 >= 0 and a[y][x - 1] == '.' and (d[y][x - 1] == -1):
		d[y][x - 1] = s + 1
		dq.append([y, x - 1])
	if y + 1 < n and a[y + 1][x] == '.' and (d[y + 1][x] == -1):
		d[y + 1][x] = s
		dq.appendleft([y + 1, x])
	if y - 1 >= 0 and a[y - 1][x] == '.' and (d[y - 1][x] == -1):
		d[y - 1][x] = s
		dq.appendleft([y - 1, x])
print(ans)


input = __import__('sys').stdin.readline
print = __import__('sys').stdout.write
from collections import deque
(n, m) = map(int, input().split())
(sx, sy) = map(int, input().split())
(l, r) = map(int, input().split())
sx -= 1
sy -= 1
lab = [input() for _ in range(n)]
visited = [[-1 for j in range(m)] for i in range(n)]
q = deque()

def go(x, y, s, lf=False):
	if 0 <= x < n and 0 <= y < m and (lab[x][y] == '.') and (visited[x][y] == -1):
		visited[x][y] = s
		if lf:
			q.appendleft((x, y))
		else:
			q.append((x, y))
ans = 0
go(sx, sy, 0)
while q:
	(n_x, n_y) = q.popleft()
	s = visited[n_x][n_y]
	ans += s + n_y - sy <= r * 2 and s - n_y + sy <= l * 2
	go(n_x - 1, n_y, s, True)
	go(n_x + 1, n_y, s, True)
	go(n_x, n_y - 1, s + 1)
	go(n_x, n_y + 1, s + 1)
print(str(ans))


import queue
import collections
(n, m) = list(map(int, input().split()))
(x, y) = list(map(int, input().split()))
(l, r) = list(map(int, input().split()))
maze = []
for _ in range(n):
	maze.append(list(input()))
cost = [[float('inf')] * m for _ in range(n)]

def bfs(start_x, start_y):
	cost[start_x][start_y] = 0
	q = collections.deque([(start_x, start_y)])
	while q:
		(x, y) = q.popleft()
		if 0 < x and maze[x - 1][y] == '.' and (cost[x][y] < cost[x - 1][y]):
			q.append((x - 1, y))
			cost[x - 1][y] = cost[x][y]
		if x < n - 1 and maze[x + 1][y] == '.' and (cost[x][y] < cost[x + 1][y]):
			q.append((x + 1, y))
			cost[x + 1][y] = cost[x][y]
		if l > 0 and 0 < y and (maze[x][y - 1] == '.') and (cost[x][y] < cost[x][y - 1]):
			q.append((x, y - 1))
			cost[x][y - 1] = cost[x][y]
		if r > 0 and y < m - 1 and (maze[x][y + 1] == '.') and (cost[x][y] + 1 < cost[x][y + 1]) and (cost[x][y] < r):
			q.append((x, y + 1))
			cost[x][y + 1] = cost[x][y] + 1
bfs(x - 1, y - 1)
v = 0
for i in range(n):
	for j in range(m):
		if cost[i][j] + (y - 1) - j <= l:
			v += 1
print(v)


import sys
import math
from collections import defaultdict
from collections import deque
MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65

def getInt():
	return int(sys.stdin.readline().rstrip())

def getInts():
	return map(int, sys.stdin.readline().rstrip().split(' '))

def getString():
	return sys.stdin.readline().rstrip()

def printOutput(ans):
	sys.stdout.write()
	pass

def successors(grid, curPos, moves):
	(r, c) = curPos
	(x, y) = moves
	succStates = []
	for (a, b) in [(1, 0), (-1, 0)]:
		if 0 <= r + a < len(grid) and 0 <= c + b <= len(grid[0]) and (grid[r + a][c + b] != '*'):
			succStates.append(((r + a, c + b), moves))
	if x > 0 and 0 <= c - 1 and (grid[r][c - 1] != '*'):
		succStates.append(((r, c - 1), (moves[0] - 1, moves[1])))
	if y > 0 and len(grid[0]) > c + 1 and (grid[r][c + 1] != '*'):
		succStates.append(((r, c + 1), (moves[0], moves[1] - 1)))
	return succStates

def solve(grid, start, moves):
	visited = [[0 for _ in range(len(grid[0]))] for _ in range(len(grid))]
	comp = lambda x: x[0] + x[1]
	total = 0
	frontier = deque()
	frontier.append((start, moves))
	while frontier:
		(path, moves) = frontier.popleft()
		if visited[path[0]][path[1]]:
			continue
		visited[path[0]][path[1]] = 1
		for (nextPath, nextMoves) in successors(grid, path, moves):
			(r2, c2) = nextPath
			if moves == nextMoves:
				frontier.appendleft((nextPath, nextMoves))
			else:
				frontier.append((nextPath, nextMoves))
	for line in visited:
		total += sum(line)
	print(total)

def readinput():
	(n, m) = getInts()
	(r, c) = getInts()
	(r, c) = (r - 1, c - 1)
	(x, y) = getInts()
	grid = []
	for _ in range(n):
		grid.append(getString())
	solve(grid, (r, c), (x, y))
readinput()