We are given a grid N rows, M columns. We start at (1,1). We want to go to (N,M) by moving only right or down, staying in grid. At each cell, if there are multiple possible moves (i.e., both right and down are within grid), we choose uniformly at random among them. So we have a random walk from start to target. The process stops when we reach (N,M) (presumably, once we reach it, we stop; but the definition says: "You are initially at cell (1, 1) and want to go to cell (N, M). From any cell you can move to the cell below it or to the cell right to it. You should never go out of the grid. At any point you should consider all the possibilities of movement with equal probability". It implies we take steps until we reach (N,M). At (N,M), since it's the target, maybe we don't move further; also from a cell on last row or last column, only one move is possible (right if not at last column, down if not at last row). But from (N,M) we stop. So the process defines a probability distribution over paths. For each cell (i,j), we need the probability that the random path visits that cell at some time (including start and end). Then sum over all cells.
We need to compute this sum for given N,M up to 1000, T up to 1000. Must be efficient, perhaps O(N+M) per test or even O(1) with precomputation.
We need to compute sum_{i=1..N} sum_{j=1..M} P(i,j). P(i,j) is probability that cell (i,j) is visited during the random walk.
We can try to find a formula.
First, think about the process: It is a directed acyclic graph (grid) with only moves right/down. Starting from (1,1) to (N,M). At each node, we choose among outgoing edges uniformly. This defines a random path (like a random lattice path). The probability of a particular path is product over steps of 1/(number of possible moves at that node). But the probability that a given cell is visited can be computed via probability flow.
Observation: Since the graph is acyclic and we start at source and stop at sink, the probability that a node is visited equals the probability that we reach that node before sink. That's exactly the probability that the random walk goes through that node. For a node (i,j), the probability of visiting it is the probability that the random walk reaches (i,j). But careful: Does visiting include multiple visits? No, since moves are only down/right, we cannot revisit a cell; each node visited at most once. So visitation is equivalent to being on the path.
Thus P(i,j) is the probability that node (i,j) lies on the random path. This is like the "probability of passing through (i,j)". For a random path chosen with the described stepwise random rule, this probability can be computed.
Alternate viewpoint: It is a Markov chain with absorbing state (N,M). For each state (i,j), the probability of ever being in that state (starting from (1,1)) is the probability that the chain reaches that state. Since it's a directed acyclic chain, it's straightforward: The probability to reach (i,j) is the probability that we take a path that goes through (i,j). This probability can be computed recursively: Let f(i,j) = probability that we ever reach (i,j). Then f(1,1)=1. For other nodes: f(i,j) = sum over predecessors that can lead to (i,j) of (probability to reach that predecessor times probability from that predecessor to go to (i,j)). Since moves only right/down, the only predecessors are (i-1,j) (from above) and (i,j-1) (from left). But careful: The predecessor must have had a choice that includes that direction. So the transition probability from a predecessor (a,b) to (i,j) is: if (i,j) = (a+1,b) (move down), then from (a,b) if both down and right are possible, probability 1/2; if only down is possible (i.e., b=M? Actually if a < N and b = M? Let's examine: (a,b) can move down if a < N; move right if b < M. So from (a,b), the number of possible moves is: d = (a<N) + (b<M). The probability of moving down, if a<N, is 1/d. Similarly for right. So the transition probability from (a,b) to a neighbor is 1/(number of available moves). So we can define f recursively:
f(1,1) = 1.
For any (i,j) not (1,1):
f(i,j) = f(i-1,j) * p_down_from(i-1,j) + f(i,j-1) * p_right_from(i,j-1),
where p_down_from(a,b) = 1/ ((a<N) + (b<M)) if a<N (down exists) else 0. But careful: if (a,b) = (i-1,j) and we want the probability that from (i-1,j) we go down to (i,j). That requires that down is available (i-1 < N) so condition i-1 < N => i <= N, always true if i<=N? Actually i ≤ N, so i-1 < N for i ≤ N? If i = N, then i-1 = N-1 < N, so down exists as long as not at bottom? Actually from (N-1, j), down exists (since N-1 < N). However, if j = M, then from (N-1, M), both down and right? Right would be j < M? If j = M, then right not available. So number of moves from (N-1,M) is 1 (down only). So p_down_from(N-1,M) = 1. Similarly, from (i,j-1) with i=N, j-1 something, we need to check.
Thus we can compute f(i,j) by DP over grid. But N,M up to 1000, T up to 1000, total up to 10^6 cells could be computed? That's 10^9 which is too high. We need closed form.
Maybe the sum of f(i,j) simplifies nicely.
Let's explore small cases.
N=1, M arbitrary: Only one row. Moves: Only right moves available at each cell except the last (since down not possible). At each cell (1,j) for j<M, the only move is right, so we always go right until (1,M). So the path is deterministic. So P(1,j)=1 for all j=1..M. Sum = M. Example: N=1, M=6 gives sum 6. Correct.
M=1, similarly sum = N.
N=2, M=2: given sum=3. Let's compute f: (1,1)=1. (1,2): from (1,1) probability to go right = 1/2 => f(1,2)=0.5. (2,1): similarly 0.5. (2,2): either from (1,2) with down (from (1,2) number of moves? At (1,2): row1, col2. Since N=2, M=2: (1,2) has down available (1<2) but right not (2<2? M=2, b=2 => right not available). So only down, so from (1,2) to (2,2) probability 1. Also from (2,1) similarly. So f(2,2)= f(1,2)*1 + f(2,1)*1 = 1.0. Sum=3.
N=2, M=3: Let's compute manually maybe find pattern.
Grid:
(1,1):1.
From (1,1): moves: down and right (both possible since N>1, M>1). So probability 1/2 each.
Compute f(1,2)=0.5; f(2,1)=0.5.
Now (1,3): from (1,2) only? (1,2) at row1 col2: N=2, M=3 => from (1,2): down exists (since row1<N), right exists (since col2<M). So two moves. So probability from (1,2) to (1,3) is 1/2. So f(1,3) = f(1,2) * 1/2 = 0.5 * 0.5 = 0.25.
Also could reach (1,3) from (1,2) only? (0,3) not exist. So only that.
(2,2): can come from (1,2) down or (2,1) right.
From (1,2): probability to go down = 1/2. So contribution = f(1,2)1/2 = 0.50.5=0.25.
From (2,1): probability to go right = from (2,1): (2,1): row2 col1. Since N=2, from (2,1) down not possible (row2=N), right exists (col1<M). So only one move: right. So from (2,1) to (2,2) probability =1. So contribution = f(2,1)*1 = 0.5.
Thus f(2,2)=0.25+0.5=0.75.
(2,3): sink? Actually (2,3) is target (N=2, M=3). Compute: can come from (1,3) down or (2,2) right.
From (1,3): (1,3): row1 col3, N=2, M=3. At (1,3): down exists (since row1<2), right not (col3=M). So only down, prob=1. So contribution = f(1,3)*1 = 0.25.
From (2,2): (2,2): row2 col2. At (2,2): down not (row2=N), right exists (col2<3). So only right, prob=1. Contribution = f(2,2)*1 = 0.75.
Thus f(2,3)=1.0. Good.
Now sum of f: (1,1)=1; (1,2)=0.5; (1,3)=0.25; (2,1)=0.5; (2,2)=0.75; (2,3)=1. Sum = 1+0.5+0.25+0.5+0.75+1 = 4.0. So sum = 4.0.
Let's compute for N=2, M=4 maybe sum = ?
We can attempt to find formula.
Alternatively, maybe the sum equals something like: (N+M-1) + something? For N=2 M=2 sum=3, which is 2+2-1=3? Actually N+M-1=3, matches. For N=2 M=3 sum=4, N+M-1=4, matches. For N=1 M=6 sum=6, N+M-1=6, matches. For N=2 M=2, N+M-1=3 matches. Check N=3 M=3? Let's compute quickly maybe to see if sum equals N+M-1? That would be 5. But is that plausible? Let's test small N=3 M=3.
Compute manually? Could be sum= something else.
We can try compute N=3 M=3 via DP.
We have grid 3x3.
We'll compute f(i,j).
First, (1,1)=1.
From (1,1): moves: both, so 1/2 each.
(1,2): from (1,1) right: 0.5.
(2,1): from (1,1) down: 0.5.
Now (1,3): from (1,2) right. But (1,2) has both moves? (1,2) row1 col2: N=3, M=3 => down exists (row1<3), right exists (col2<3). So 2 moves. So prob right from (1,2) = 1/2. So f(1,3)= f(1,2)1/2 = 0.50.5=0.25.
(2,2): can come from (1,2) down or (2,1) right.
From (1,2): down prob=1/2 => contribution 0.5*0.5=0.25.
From (2,1): right prob: (2,1): row2 col1: down exists (row2<3), right exists (col1<3). So 2 moves, right prob=1/2. So contribution = f(2,1)1/2 = 0.50.5=0.25.
Thus f(2,2)=0.5.
(2,1) already 0.5.
(2,3): from (1,3) down or (2,2) right.
(1,3): row1 col3: down exists (1<3), right not (col3=M). So only down => prob=1. Contribution = f(1,3)*1=0.25.
(2,2): row2 col2: both moves exist? row2<3, col2<3 => 2 moves. So prob right =1/2. Contribution = f(2,2)1/2 = 0.50.5=0.25.
Thus f(2,3)=0.5.
(3,1): from (2,1) down. (2,1): both moves? row2<3, col1<3 => 2 moves, down prob=1/2. So f(3,1)= f(2,1)1/2 = 0.50.5=0.25.
(3,2): from (2,2) down or (3,1) right.
(2,2): down prob=1/2 => contribution 0.5*0.5=0.25.
(3,1): row3 col1: down not (row3=N), right exists (col1<3). So only right => prob=1. Contribution = f(3,1)*1 = 0.25.
Thus f(3,2)=0.5.
(3,3): from (2,3) down or (3,2) right.
(2,3): row2 col3: down exists (2<3), right not (col3=M). So only down => prob=1. Contribution = f(2,3)*1=0.5.
(3,2): row3 col2: right exists (col2<3), down not. So only right => prob=1. Contribution = f(3,2)*1=0.5.
Thus f(3,3)=1.
Now sum all f:
Row1: 1 + 0.5 + 0.25 = 1.75
Row2: 0.5 + 0.5 + 0.5 = 1.5? Wait (2,1)=0.5, (2,2)=0.5, (2,3)=0.5 => sum=1.5
Row3: 0.25 + 0.5 + 1 = 1.75
Total = 1.75+1.5+1.75=5.0. Indeed N+M-1 = 3+3-1=5. So matches.
Check N=2 M=5 maybe sum? According to N+M-1=6. Let's test quickly using pattern? Possibly sum = N+M-1 for all N,M? But need to verify with more varied values.
Try N=3 M=4. N+M-1=6. Let's compute quickly? Let's do DP small to check.
Compute f for 3x4.
We can use combinatorial reasoning: Is it always N+M-1? That would be the number of cells on the minimal path (Manhattan distance) but not total cells. In 2x2 sum=3, but total cells=4. So it's less than total. For 3x3 sum=5, total=9. So not trivial.
But need to test more cases. Let's do N=3 M=2 maybe? Then N+M-1=4. Let's compute manually:
Grid 3x2.
(1,1)=1.
(1,2): from (1,1) right? At (1,1): both moves? N=3>1, M=2>1 => 2 moves. So f(1,2)=0.5.
(2,1): from (1,1) down: 0.5.
(2,2): from (1,2) down or (2,1) right.
(1,2): row1 col2: down exists (1<3), right? M=2, col2=M so right not. So only down => prob=1. Contribution = f(1,2)*1=0.5.
(2,1): row2 col1: down exists (2<3), right exists (col1<2). So 2 moves, right prob=1/2. Contribution = f(2,1)1/2=0.50.5=0.25.
Thus f(2,2)=0.75.
(3,1): from (2,1) down? (2,1) has both? down exists (2<3) and right exists? yes, 2 moves. So down prob=1/2, contribution = f(2,1)1/2=0.50.5=0.25.
Also could come from (something else)? Only (2,1). So f(3,1)=0.25.
(3,2): from (2,2) down or (3,1) right.
(2,2): row2 col2: down exists (2<3), right not (col2=M). So only down => prob=1, contribution = f(2,2)*1 = 0.75.
(3,1): row3 col1: right exists (col1<2), down not. So only right => prob=1, contribution = f(3,1)*1 = 0.25.
Thus f(3,2)=1.0.
Now sum: (1,1)=1, (1,2)=0.5, (2,1)=0.5, (2,2)=0.75, (3,1)=0.25, (3,2)=1. Total = 1+0.5+0.5+0.75+0.25+1 = 4.0. N+M-1=4, matches.
Test N=4 M=1? Then N+M-1=4. Since M=1, only column. Deterministic? For N=4, M=1, only down moves. So each cell visited with probability 1. Sum = 4 = N = N+M-1? Since M=1, N+M-1=N. Yes.
Test N=3 M=5: N+M-1=7. But is sum always N+M-1? Let's try to compute a non-symmetric case like N=3 M=5 using reasoning maybe sum=7? We'll check with DP via code mental or reasoning.
We'll compute f for 3x5.
We could also try to derive formula for f(i,j). Perhaps f(i,j) equals something like number of paths from (1,1) to (i,j) weighted by product of branching probabilities? But maybe simpler: f(i,j) = (choose(i+j-2, i-1) / 2^{i+j-2})? Not exactly, because branching probabilities are not uniform across all paths; the probability of a specific path is product of 1/(#choices at each node). That's not constant across all paths of same length. For example, from (1,1) to (N,M), the probabilities vary.
But maybe there's a known result: For such a random walk on a grid with uniform choices at each step, the probability to visit cell (i,j) is something like: the probability that the walk passes through that cell. This might equal the product of probabilities of reaching the cell from start and then reaching target? In Markov chains, sometimes the probability that a node is visited can be expressed in terms of potential theory. Since it's a directed acyclic graph, the probability to ever visit (i,j) equals (number of ways from start to (i,j) weighted by probabilities?) But maybe there is a simpler combinatorial interpretation.
We can try to compute f(i,j) as:
Let a = i-1, b = j-1. The path from (1,1) to (i,j) uses a down moves and b right moves, total steps a+b. But the probability of each particular sequence depends on the number of choices at each interior node.
Alternatively, we can compute f(i,j) recursively and see if it equals something like:
f(i,j) = C(i+j-2, i-1) / 2^{i+j-2} * something? Let's compute some values:
For (1,2) in 3x3, we got 0.5. i=1,j=2: i+j-2=1, C(1,0)=1, /2^1=0.5 -> matches.
For (1,3): i=1,j=3: i+j-2=2, C(2,0)=1, /2^2=0.25 -> matches.
For (2,2): i=2,j=2: i+j-2=2, C(2,1)=2, /2^2=0.5. But we got 0.5? In 3x3 we got f(2,2)=0.5, matches. In 2x3 we got f(2,2)=0.75, which is not 0.5. Wait 2x3: N=2,M=3, i=2,j=2: i+j-2=2, C(2,1)=2, /4=0.5, but we computed 0.75. So not constant; depends on N,M because the transition probabilities near boundaries differ. Indeed, from (2,1) in 2x3, at (2,1) only one move (right) because down not available. So that increased probability to (2,2). So f depends on boundaries.
Thus sum likely not simply N+M-1? But for cases we tested (2x3 sum=4=2+3-1, 2x4 would be 5? Let's compute 2x4 manually quickly to verify.
We can compute f for 2x4.
N=2, M=4.
We'll compute systematically:
(1,1)=1.
(1,2) = from (1,1) right: 0.5.
(2,1) = from (1,1) down: 0.5.
(1,3): from (1,2) right. (1,2) has both moves? N=2, M=4 => at (1,2) down exists (since row1<2), right exists (col2<4). So 2 moves, right prob=1/2. So f(1,3)=0.5 * 0.5 = 0.25.
(2,2): from (1,2) down or (2,1) right.
- (1,2) down prob=1/2 => 0.5*0.5=0.25.
- (2,1): row2 col1: down not (row2=N), right exists (col1<4) => only move right, prob=1 => contribution 0.5*1=0.5. So f(2,2)=0.75.
(1,4): from (1,3) right. (1,3): row1 col3: down exists (1<2), right exists (col3<4) => 2 moves, right prob=1/2. f(1,3)=0.25, so f(1,4)=0.25*0.5=0.125.
(2,3): from (1,3) down or (2,2) right.
- (1,3) down prob=1/2 => 0.25*0.5=0.125.
- (2,2): row2 col2: down not, right exists (col2<4) => only right, prob=1 => contribution f(2,2)*1=0.75. So f(2,3)=0.125+0.75=0.875.
(2,4): target? Actually (2,4) is (N,M). From (1,4) down or (2,3) right.
- (1,4): row1 col4: down exists, right not (col4=M) => only down, prob=1 => f(1,4)=0.125 => contribution 0.125.
- (2,3): row2 col3: down not, right exists (col3<4) => only right, prob=1 => f(2,3)=0.875 => contribution 0.875.
Thus f(2,4)=1.0.
Now sum all cells:
(1,1)=1
(1,2)=0.5
(1,3)=0.25
(1,4)=0.125
(2,1)=0.5
(2,2)=0.75
(2,3)=0.875
(2,4)=1
Total = 1+0.5+0.25+0.125+0.5+0.75+0.875+1 = Let's add: 1+0.5=1.5, +0.25=1.75, +0.125=1.875, +0.5=2.375, +0.75=3.125, +0.875=4.0, +1=5.0. So sum=5. N+M-1=2+4-1=5. Matches.
So far, for all tested, sum = N+M-1.
Test N=4 M=3. Symmetric, sum should be 4+3-1=6. Let's test one more non-square: N=4 M=4: N+M-1=7. Let's compute quickly using DP? But we suspect it's 7? But total cells=16, sum=7 seems plausible? Let's test via quick DP mental? Could also think about path counts? Possibly the sum is N+M-1.
But wait, for N=1, M=1, sum should be? Grid 1x1. Start at (1,1) which is also target. So we visit that cell probability 1. So sum=1. N+M-1=1+1-1=1. Good.
Thus hypothesis: The sum of visitation probabilities equals N+M-1.
But is this always true? Let's try a more complex example: N=3, M=5. According to hypothesis sum=7. Let's compute DP for 3x5 to verify.
We need to compute f(i,j) for i=1..3, j=1..5.
Let's do DP systematically.
Define grid with rows i=1..3, cols j=1..5.
We'll compute f recursively.
First, f(1,1)=1.
Now we compute row by row, column by column, but need to follow dependencies: f(i,j) depends on f(i-1,j) and f(i,j-1). So we can compute in order increasing i+j.
I'll compute values.
Initialize f(1,1)=1.
We'll compute cells:
(1,2): from (1,1) right. At (1,1): moves: both down and right? N=3>1, M=5>1 => 2 moves. So prob right =1/2. f(1,2)=1 * 1/2 = 0.5.
(2,1): from (1,1) down: 0.5.
(1,3): from (1,2) right. At (1,2): i=1,j=2: down exists (1<3), right exists (2<5) => 2 moves => prob right =1/2. f(1,3)= f(1,2) * 1/2 = 0.5 * 0.5 = 0.25.
(2,2): from (1,2) down or (2,1) right.
- (1,2) down: from (1,2) prob down =1/2. Contribution = 0.5 * 1/2 = 0.25.
- (2,1) right: at (2,1): i=2,j=1: down exists (2<3), right exists (1<5) => 2 moves => prob right =1/2. Contribution = f(2,1) * 1/2 = 0.5 * 0.5 = 0.25.
So f(2,2)=0.5.
(3,1): from (2,1) down. At (2,1): down exists (2<3) => but need to consider #moves: (2,1) has both down and right (since j=1<5) => 2 moves, so prob down =1/2. f(3,1)= f(2,1) * 1/2 = 0.5 * 0.5 = 0.25.
(1,4): from (1,3) right. (1,3): i=1,j=3: down exists (1<3), right exists (3<5) => 2 moves => prob right =1/2. f(1,4)= 0.25 * 1/2 = 0.125.
(2,3): from (1,3) down or (2,2) right.
- (1,3) down: prob down=1/2, contribution = 0.25 * 0.5 = 0.125.
- (2,2) right: at (2,2): i=2,j=2: down exists (2<3), right exists (2<5) => 2 moves => prob right=1/2. Contribution = f(2,2)*0.5 = 0.5 * 0.5 = 0.25.
So f(2,3)=0.125+0.25=0.375.
(3,2): from (2,2) down or (3,1) right.
- (2,2) down: prob down=1/2, contribution = 0.5 * 0.5 = 0.25.
- (3,1) right: at (3,1): i=3,j=1: down not (3=N), right exists (1<5) => only right => prob =1. Contribution = f(3,1)*1 = 0.25.
So f(3,2)=0.25+0.25=0.5.
(1,5): from (1,4) right. (1,4): i=1,j=4: down exists (1<3), right exists (4<5) => 2 moves => prob right=1/2. f(1,5)= f(1,4)*0.5 = 0.125 * 0.5 = 0.0625.
(2,4): from (1,4) down or (2,3) right.
- (1,4) down: prob down=1/2, contribution = 0.125 * 0.5 = 0.0625.
- (2,3) right: at (2,3): i=2,j=3: down exists (2<3), right exists (3<5) => 2 moves => prob right=1/2. Contribution = f(2,3)*0.5 = 0.375 * 0.5 = 0.1875.
So f(2,4)=0.0625+0.1875=0.25.
(3,3): from (2,3) down or (3,2) right.
- (2,3) down: prob down=1/2, contribution = 0.375 * 0.5 = 0.1875.
- (3,2) right: at (3,2): i=3,j=2: right exists (2<5), down not => only right => prob=1. Contribution = f(3,2)*1 = 0.5.
So f(3,3)=0.1875+0.5=0.6875.
(2,5): from (1,5) down or (2,4) right.
- (1,5) down: at (1,5): i=1,j=5: down exists (1<3), right not (5=M) => only down => prob=1. Contribution = f(1,5)*1 = 0.0625.
- (2,4) right: at (2,4): i=2,j=4: down exists (2<3), right exists (4<5) => 2 moves => prob right=1/2. Contribution = f(2,4)*0.5 = 0.25 * 0.5 = 0.125.
So f(2,5)=0.0625+0.125=0.1875.
(3,4): from (2,4) down or (3,3) right.
- (2,4) down: prob down=1/2, contribution = 0.25 * 0.5 = 0.125.
- (3,3) right: at (3,3): i=3,j=3: right exists (3<5), down not => only right => prob=1. Contribution = f(3,3)*1 = 0.6875.
So f(3,4)=0.125+0.6875=0.8125.
(3,5): target from (2,5) down or (3,4) right.
- (2,5) down: at (2,5): i=2,j=5: down exists (2<3), right not => only down => prob=1. Contribution = f(2,5)*1 = 0.1875.
- (3,4) right: at (3,4): i=3,j=4: right exists (4<5), down not => only right => prob=1. Contribution = f(3,4)*1 = 0.8125.
So f(3,5)=1.0.
Now sum all f:
Row1: (1,1)=1; (1,2)=0.5; (1,3)=0.25; (1,4)=0.125; (1,5)=0.0625 => sum row1 = 1+0.5=1.5, +0.25=1.75, +0.125=1.875, +0.0625=1.9375.
Row2: (2,1)=0.5; (2,2)=0.5; (2,3)=0.375; (2,4)=0.25; (2,5)=0.1875 => sum = 0.5+0.5=1.0, +0.375=1.375, +0.25=1.625, +0.1875=1.8125.
Row3: (3,1)=0.25; (3,2)=0.5; (3,3)=0.6875; (3,4)=0.8125; (3,5)=1.0 => sum = 0.25+0.5=0.75, +0.6875=1.4375, +0.8125=2.25, +1=3.25.
Total = 1.9375 + 1.8125 + 3.25 = let's add: 1.9375+1.8125=3.75, +3.25=7.0. Yes total = 7.0 exactly.
So indeed sum = N+M-1 = 7.
Thus seems the sum of visitation probabilities equals N+M-1 for all N,M? Let's test with N=4 M=4 quickly maybe sum=7? Actually 4+4-1=7. But we saw for 3x3 sum=5, 2x4 sum=5, 3x5 sum=7. So it appears to be N+M-1. But need to verify for larger values, maybe boundary cases where both dimensions >1. Let's test N=4 M=2, symmetric to 2x4 we did? 2x4 sum=5, so 4x2 sum=5. 4+2-1=5. Good.
Now test N=3 M=6, sum should be 8. Possibly correct.
But is there any case where it fails? Let's test N=4 M=5, sum=8? Actually 4+5-1=8. But we can try compute a few values to be confident.
We could try to derive the sum analytically.
Observation: The process defines a probability distribution over all monotonic paths from (1,1) to (N,M). The probability of a path is product over nodes of 1/(out-degree). The visitation probability of a cell (i,j) is sum over paths that go through (i,j) of their probability. Then sum_{i,j} P(i,j) = sum_{i,j} sum_{paths through (i,j)} prob(path) = sum_{paths} prob(path) * (number of cells visited on that path). Since each path visits exactly (N+M-1) distinct cells? Actually any monotonic path from (1,1) to (N,M) with steps down/right has length (N-1)+(M-1) = N+M-2 steps, and visits N+M-1 distinct cells (including start and end). Indeed, each path includes exactly one cell in each anti-diagonal? More precisely, number of cells visited = number of steps + 1 = N+M-1. So each path visits exactly N+M-1 cells.
Thus sum_{i,j} P(i,j) = sum_{paths} prob(path) * (N+M-1) = (N+M-1) * sum_{paths} prob(path). But sum over all paths of prob(path) is 1, because it's the total probability of all possible paths (they are disjoint and cover all possibilities). However, careful: The random process defines a probability distribution over paths: each path occurs with some probability, and the set of all monotonic paths is exhaustive (since we always eventually reach (N,M) - no loops). So indeed the sum of probabilities over all paths is 1. Then sum_{i,j} P(i,j) = (N+M-1) * 1 = N+M-1.
That's a clean proof! Let's verify: For each path, the number of visited cells is exactly N+M-1. The probability that a particular cell (i,j) is visited is the sum of probabilities of paths that include that cell. So summing over all cells, we interchange sums: total sum = sum_{paths} prob(path) * (number of distinct cells on that path). Since each path has exactly N+M-1 distinct cells (monotonic path from (1,1) to (N,M) has N+M-1 nodes), the sum = (N+M-1) * sum_{paths} prob(path) = N+M-1. That is it.
We must ensure that the process always terminates and that each path has probability defined, and that all paths have exactly N+M-1 visited cells. Yes, because from start to target you need exactly N-1 down moves and M-1 right moves; any sequence yields a distinct set of visited cells of size (N+M-1) (since you never revisit a cell). So indeed.
Thus answer is simply N + M - 1.
Double-check edge cases: N=1, M=1 -> 1+1-1=1 correct. N=1000, M=1000 -> 1999.
But wait: Is it possible that some path could have fewer than N+M-1 visited cells? Only if you skip some cells? But moves are only to adjacent right or down, so you always move to adjacent cell. Starting at (1,1) after k steps you'll be at some cell (i,j) with i-1 down moves and j-1 right moves, so i+j = k+2. When you reach (N,M) after N+M-2 steps, i+j = N+M. So the visited cells are all distinct, and the count is exactly steps+1 = N+M-1. There's no possibility of revisiting because you never go up or left. So it's fine.
Thus answer is simply N+M-1, as a rational integer, but output as floating with decimal? The example outputs 3.000000 and 6.000000. So we can output as integer with .6f.
But need to be careful: The sum of probabilities may not be exactly integer due to floating? But the reasoning shows it's exactly N+M-1, integer. So we can output integer with decimal digits.
Thus solution is trivial: for each test case, print (N+M-1) with 6 decimal places.
But wait: Are we sure that sum over all paths of probabilities equals 1? Yes, because the process defines a probability measure on the set of all possible infinite sequences? But it terminates at (N,M). At (N,M) we stop, so no further moves. So the set of all paths that start at (1,1) and follow moves until reaching (N,M) forms a partition of the sample space. So total probability = 1.
One might worry about the possibility of infinite loops? Not possible because moves only increase coordinates, so you'll reach (N,M) in finite steps.
Thus answer holds.
But wait: Could there be any path that visits more than N+M-1 cells? No, because you cannot revisit cells, and the total number of distinct cells reachable is limited by the monotonic nature; you always move right or down, so you never go back, and you must go from (1,1) to (N,M). The unique cells visited are those along the path; the path length is exactly N+M-2 moves, so number of nodes = moves+1 = N+M-1. So each path visits exactly that many cells.
Thus sum of visitation probabilities = expected number of distinct visited cells. Since each path visits exactly N+M-1 cells, the expected number is N+M-1. So answer is N+M-1.
Therefore the problem reduces to simply computing N+M-1.
But wait, is there any nuance: The probability of visiting a cell is defined as "probability of visiting cell (i,j)" meaning the probability that the random process ever lands on that cell. That matches our definition. So sum is expected number of visited cells. Because expectation of sum of indicator variables = sum of probabilities. So it's exactly the expected number of distinct cells visited. Since each path visits exactly N+M-1 cells, the expected value is N+M-1.
Thus answer is simply N+M-1.
I'll double-check with example: N=2 M=2 => 3, matches. N=1 M=6 => 6, matches. Good.
Thus solution: read T, for each test case read N M, output N+M-1 with 6 decimal places.
Constraints up to 1000, but that's fine. Complexity O(T).
We should also consider if any pitfalls: The grid indices start at 1,1. N,M at least 1. So answer non-negative.
Thus implement straightforward.
But wait, is there any scenario where the random process might have a different rule at the target? At (N,M) we stop, so no moves considered. That's fine.
Thus answer is trivial.
But hold on, the problem statement says: "You are initially at cell (1, 1) and want to go to cell (N, M). From any cell you can move to the cell below it or to the cell right to it. You should never go out of the grid. At any point you should consider all the possibilities of movement with equal probability. Let P[i][j] be the probability of visiting cell (i, j). You need to calculate the sum of P[i][j] for 1 ≤ i ≤ N, 1 ≤ i ≤ M." The example given matches.
Thus our reasoning is correct.
One might ask: Could there be a possibility that the path might not necessarily reach (N,M) because the random choices could somehow avoid it? But with monotonic moves, you'll eventually hit bottom or right edges and forced moves will eventually bring you to (N,M). Indeed, as long as you keep moving, you'll eventually reach (N,M). Because each move either increases row or column, so after finite steps you'll reach maximum row N and maximum column M. There's no way to get stuck elsewhere. So all paths terminate at (N,M) with probability 1.
Thus sum over paths prob = 1.
Therefore answer is N+M-1.
I'll also consider potential alternative interpretations: Could "probability of visiting cell (i,j)" include possibly multiple visits? But as argued, cannot revisit. So it's fine.
Thus final solution.
But wait: The problem might be trickier: The description says: "From any cell you can move to the cell below it or to the cell right to it. You should never go out of the grid. At any point you should consider all the possibilities of movement with equal probability." This could be interpreted that at each step, you consider all possible moves that keep you within the grid, and choose uniformly among them. That's what we used.
Thus answer is straightforward.
Now, the test input includes T up to 1000, N,M up to 1000. So output (N+M-1) as double with 6 decimal digits.
Implementation: read T; loop; read N M; print("%.6f" % (N+M-1)).
Make sure to use float or double. Since integer, output as e.g., 3.000000.
Edge cases: N=1000, M=1000 => 1999.0.
Thus we are done.
But I'm curious: Could there be any nuance about the probability of visiting (N,M)? It's always 1, as shown. Good.
Let's also test N=2 M=3, we computed sum=4, N+M-1=4. Good.
Thus answer holds.
We can provide solution.
But before finalizing, let's confirm that the expectation argument indeed holds: E[ number of visited cells ] = sum_{cells} P(cell visited) = E[ sum_{cells} I_cell ] = sum_{paths} prob(path) * (number of cells visited on that path) = (N+M-1) * sum_{paths} prob(path) = N+M-1.
Yes.
Thus answer is N+M-1.
Thus the problem is trivial. Probably the intended solution is exactly that observation.
I'll write solution accordingly.
But wait: The problem states "You need to calculate the sum of P[i][j] for 1 ≤ i ≤ N, 1 ≤ i ≤ M." That is the sum of probabilities, which is the expected number of visited cells. And as reasoned, that expectation is constant across all probability distributions over monotonic paths? Actually, any distribution over monotonic paths that always results in a path from start to end would have expected visited cells = average length of path in terms of nodes? But here the distribution is specific (random walk with uniform choices at each node). Yet we found that each path has exactly N+M-1 nodes, so regardless of distribution, as long as we always get a full path from start to target, the expected number is N+M-1. So indeed any distribution over the set of all monotonic paths (that covers all possibilities) would have this property if each path has exactly the same number of nodes. But wait, if the distribution is such that some paths may have probability zero, but those that have positive probability still have exactly N+M-1 nodes. So the expected number would be N+M-1 if the probabilities sum to 1 over the set of all paths that are possible? However, if the process might stop early? No, it must reach target. So it's fine.
Thus answer independent of the specifics of the random choices: any random process that always follows a monotonic path from start to end (and doesn't skip cells) will have expected visited cells = N+M-1. However, the process defined does indeed generate only such paths. So yes.
Thus solution done.
But there is a potential nuance: The random process might sometimes have more than two choices? Only two possible moves at most. So fine.
Thus answer is integer.
Now I'll produce final answer.
I will also include explanation.
But before finalizing, let's try to see if the problem might have a twist: For example, the phrase "At any point you should consider all the possibilities of movement with equal probability" might be interpreted that at each step, you consider all possible moves (including possibly staying in place? No, only moving to below or right). So it's fine.
Thus code is trivial.
Now ensure we handle multiple test cases.
We'll implement in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
T = int(next(it))
out = []
for _ in range(T):
N = int(next(it)); M = int(next(it))
ans = N + M - 1
out.append("{:.6f}".format(ans))
sys.stdout.write("\n".join(out))
But note: For large T, using list and join is fine.
Potential gotcha: Input might have extra spaces or newlines. But reading all tokens is fine.
Now, maybe we need to output as double with exactly 6 decimal places, as per example. So format accordingly.
Thus final solution.
I'll also discuss the reasoning in the answer.
But just to be thorough: Let's test with random values N=5 M=7: sum should be 11. We can simulate mentally? Not necessary.
Thus final.
Now I'm going to produce final answer with explanation. The sum of the visitation probabilities over all cells equals the expected number of distinct cells visited.
Each monotone path from (1,1) to (N,M) (using only right and down moves) contains exactly (N+M-1) cells.
Since the described random process chooses such a path with total probability 1, the expected number of visited cells is exactly (N+M-1).
Hence, for each test case, output (N+M-1) with six decimal places.
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
m = int(next(it))
ans = n + m - 1
out_lines.append(f"{ans:.6f}")
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
solve()