01Counting paths: add the two cells you came from
How many ways can you walk from the top left to the bottom right corner of a grid, moving only right or down?
Every path into cell (i, j) arrives either from above or from the left, and those two groups do not overlap. So
ways[i][j] = ways[i−1][j] + ways[i][j−1]
The top row and left column have exactly one way each (a straight line), which gives the base cases. Fill row by row, left to right, and both neighbours are always ready.
On a 4 × 5 grid the corner gets 35. You may recognise Pascal’s triangle: the answer is the binomial coefficient C(m+n−2, m−1). But the DP is what survives when the problem changes, for example when some cells are blocked.