Seam carving is a novel algorithm for resizing images while maintaining as much information as possible from the source image.
Now, Rikka is going to use seam carving method to deal with an $n \times m$ black and white picture. We can abstract this picture into an $n \times m$ 01 matrix $A$.
A $K$-seam of this picture is an integer sequence $a$ which satisfies the following conditions:
1. $|a| = n$, $a_i \in [1, m]$.
2. $|a_i - a_{i+1}| \leq K$, $\forall i \in [1,n)$.
After choosing a $K$-seam $a$, Rikka reduces the size of the picture by deleting pixels $(i,a_i)$, and then she gets a matrix $A'$ of size $n \times (m - 1)$.
For example, if the chosen seam is $[1,2,3]$ and the picture is \begin{align*} \begin{bmatrix} 1 &0 &0 \\ 1& 1& 1 \\ 0 & 0& 0\end{bmatrix}\end{align*} the result matrix will be \begin{align*} \begin{bmatrix} 0 &0 \\ 1& 1 \\ 0 & 0\end{bmatrix}\end{align*}
Rikka finds that deleting different seams may get the same result. In the previous example, seam $[1,2,3],[1,2,1],[1,2,2],[1,1,1]$ are equivalent.
Now Rikka wants to calculate the number of different matrixes she can get by deleting exactly one $K$-seam.