You are given a 2D matrix containing N rows and M columns. You are currently standing at cell (1, 1) and you want to reach cell (n, m).
You can only move right or down from your current cell, that is, if you are currently in cell (i, j), then you can either move to cell (i+1, j) or cell (i, j+1). You cannot move out of the matrix.
Each cell of the grid has some cost associated with it which you have to pay when you land at that cell.
Each cell has a number K associated with it. Now, the cost of that cell is defined as the maximum number of times you can divide K by a positive integer X that is greater than 1 until K becomes 1. Here, K must be divisible by X.
The cost of a path is defined as the sum of the costs of all the cells in the path.
Note: You also have to pay the cost at cell (1, 1) as you are standing on it.
You are required to determine the minimum cost to reach from cell (1, 1) to cell (n, m).
Input format
Output format
For each test case, print a single line containing the minimum cost.
Constraints
1≤T≤100001≤N, M≤10001≤grid[i][j]≤100000Sum of (N∗M) over all test cases≤1000000
In the first Matrix the optimal path will be-(1,1)-(1,2)-(2,2) with a total cost of 0+1+1=2
Note: There is also another optimal path (1,1)-(2,1)-(2,2) with a total cost of 0+1+1=2
In the second Matrix the optimal path would be (1,1)-(1,2)-(1,3)-(2,3)-(3,3) with a total cost of 0+1+1+1+1=4