#P1141. Alternating Maze Traversal

    ID: 13487 Type: Default 1000ms 256MiB

Alternating Maze Traversal

Alternating Maze Traversal

You are given an n×nn\times n maze consisting only of the digits 00 and 11. If you are in a cell with value 00, you can move to any one of its 44 adjacent cells (up, down, left, right) that contains a 11. Similarly, if you are in a cell with value 11, you can move to any one of its 44 adjacent cells that contains a 00.

Your task is to determine how many cells can be reached (including the starting cell) if you start from a specified cell.

inputFormat

The input begins with an integer nn, denoting the size of the maze. The next line contains two integers rr and cc (0-indexed) which represent the starting cell. This is followed by nn lines, each containing nn space-separated integers (each either 00 or 11) representing the maze.

outputFormat

Output a single integer indicating the total number of cells reachable from the starting cell (including the starting cell itself) according to the alternating move rules.

sample

3
0 0
0 1 0
1 0 1
0 1 0
9