The cells of a rectangular $$m \times n$$ grid with $$m$$ rows and $$n$$ columns each contain a number between 0 and 9. Connected cells containing the same number form a group, where cells are connected if they share a common side (horizontally or vertically). For example, the following $$4 \times 4$$ grid has five groups, marked with a distinct color.

fuse
What is the minimum number of moves needed to fuse all cells of this $$4 \times 4$$ grid into a single group?

A move increments or decrements all cells in a group by 1, possibly fusing it with other groups. In doing so, the group number may not fall below 0 or exceed 9. The challenge is to fuse the entire grid into a single group using a minimum number of moves. The above example takes just two moves:

Assignment

The list representation of a sequence of digits arranged in a rectangular $$m \times n$$ grid consists of a list containing the $$m$$ rows of the grid (top to bottom), where each row is itself represented as a list containing the $$n$$ numbers (int) in the row (from left to right).

The string representation of a sequence of digits arranged in a rectangular $$m \times n$$ grid consists of a tuple containing three elements: i) a string (str) containing the digits as read from left to right and from top to bottom from the grid, ii) the number of rows (int) in the grid and iii) the number of columns (int) in the grid.

We number the rows of the grid from top to bottom and the columns from left to right, each time starting from zero. This way, the position of a cell in the grid can be represented as a tuple $$(r, k)$$, where $$r$$ and $$k$$ indicate the number of the row and the column where the cell is located in the grid.

Your task:

The functions string_representation, is_solved, group and is_solution may not modify the given grid. On the other hand, the function move must modify the numbers in the given grid itself and return a reference to the given grid.

Remark

All functions may assume that all arguments passed and all actions they need to perform are valid, without the need to check this explicitly.

Example

>>> grid = list_representation('1221133113322222', 4)
>>> grid
[[1, 2, 2, 1], [1, 3, 3, 1], [1, 3, 3, 2], [2, 2, 2, 2]]
>>> string_representation(grid)
('1221133113322222', 4, 4)
>>> move(-1, {(1, 2), (1, 1), (2, 1), (2, 2)}, grid)
[[1, 2, 2, 1], [1, 2, 2, 1], [1, 2, 2, 2], [2, 2, 2, 2]]
>>> grid
[[1, 2, 2, 1], [1, 2, 2, 1], [1, 2, 2, 2], [2, 2, 2, 2]]
>>> move(+1, {(0, 3), (1, 3)}, grid)
[[1, 2, 2, 2], [1, 2, 2, 2], [1, 2, 2, 2], [2, 2, 2, 2]]
>>> grid
[[1, 2, 2, 2], [1, 2, 2, 2], [1, 2, 2, 2], [2, 2, 2, 2]]
>>> move(+1, {(2, 0), (1, 0), (0, 0)}, grid)
[[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]
>>> is_solved(grid)
True

>>> grid = list_representation('1221133113322222', 4)
>>> group((2, 1), grid)
{(1, 2), (1, 1), (2, 1), (2, 2)}
>>> group((0, 3), grid)
{(0, 3), (1, 3)}
>>> group((1, 0), grid)
{(2, 0), (1, 0), (0, 0)}
>>> is_solution([(1, 1, False), (3, 2, False)], grid)
True
>>> is_solution([(1, 3, True), (3, 2, False), (0, 1, True)], grid)
False
>>> grid
[[1, 2, 2, 1], [1, 3, 3, 1], [1, 3, 3, 2], [2, 2, 2, 2]]