1200 - Circuits   

Description

Given a grid which represents a circuit, the current passing each grid point has its direction which can either 
be north, east, south, or west. According to the right hand rule, if the current formed a closed loop then
a magnetic field is generated inside the loop. A closed loop is defined as follow:

If you started at some point and keep follow the arrow and finally go back to the point, the path you traversed
is a closed loop. In addition, the loop must consists of at least four arrows. Therefore, a right followed by a left
does not account for a closed loop. Note that it is not guranteed that every point lies on a closed loop.

The direction of the magnetic field depends on whether the current loop is clockwise or counter-clockwise.
If the loop is clockwise, then magnitude of -1 is generated. If it is counter-clockwise, +1 is generated.
In addition, the magnitudes are cumalaitve. For example, if there is a clockwise loop A and a counter-clockwise
loop B. If B is inside A, then the magnetic field inside B has magnitude of 0.

In this problem, you have to find the greatest absolute value of the magnetic field magnitude in the circuit.

Input

The input may consist of many cases. In each case, the first line are two integers n, m (1<=n, m<=1000) indicating
the size of the grid circuit. The followings are n lines describing the circuit, and in each line there are m integers.
The integers can be 0, 1, 2, 3 representing north, east, south, west respectively.

Output

In each case, you should output exactly one integer in one line which indicates the greatest absolute value
of magnitude induced by the circuit.

Sample Input  Download

Sample Output  Download

Tags




Discuss