### Problem Statement

A rider is a fantasy chess piece that can jump like a knight several times in a single move. (See notes for a description of how a knight jumps.) A rider that can perform a maximum of K jumps during a single move is denoted as a K-rider. For example, a 2-rider can jump once or twice during a single move, and a 1-rider is a traditional knight.

There are some riders of different types on a chessboard. You are given a String[] board representing the layout of the pieces. The j-th character of the i-th element of board is the content of the square at row i, column j. If the character is a digit K between '1' and '9', the square contains a K-rider. Otherwise, if the character is a '.', the square is empty. Return the minimal total number of moves necessary to move all the riders to the same square. Only one piece can move during each move. Multiple riders can share the same squares at all times during the process. Return -1 if it is impossible.

### Definition

 Class: CollectingRiders Method: minimalMoves Parameters: String[] Returns: int Method signature: int minimalMoves(String[] board) (be sure your method is public)

### Notes

-A traditional knight has up to 8 moves from a square with coordinates (x,y) to squares (x+1,y+2), (x+1,y-2), (x+2,y+1), (x+2,y-1), (x-1,y+2), (x-1,y-2), (x-2,y+1), (x-2,y-1), and can not move outside the chessboard.

### Constraints

-board will contain between 1 and 10 elements, inclusive.
-Each element of board will contain between 1 and 10 characters, inclusive.
-All elements of board will have the same length.
-board will contain only positive digits ('1'-'9') and '.' characters.
-board will contain at least one digit.

### Examples

0)

 ```{"...1", "....", "2..."}```
`Returns: 2`
 The 2-rider can jump from (2,0) to (0,1) in the first move, and then from (0,1) to (2,2) to (0,3) in the second.
1)

 ```{"........", ".1......", "........", "....3...", "........", "........", ".7......", "........"}```
`Returns: 2`
 In 2 moves, we can move all the pieces to the cell initially occupied by the 1-rider.
2)

 ```{"..", "2.", ".."}```
`Returns: 0`
 No moves are necessary.
3)

 `{".1....1."}`
`Returns: -1`
4)

 ```{"9133632343", "5286698232", "8329333369", "5425579782", "4465864375", "8192124686", "3191624314", "5198496853", "1638163997", "6457337215"}```
`Returns: 121`
 Kind of maximal test.

#### Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8319

#### Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10805&pm=8319

dkorduban

#### Testers:

PabloGilberto , Olexiy , marek.cygan , ivan_metelsky

Graph Theory