fdo#78976: Correct way to determine the end position for matrix check.

Change-Id: Ifd6acd85a9bf33742cb62327a3b8a4320f35d0f3
(cherry picked from commit 054213e86b8f3b1084474b0447cc7ffaf82178c5)
Reviewed-on: https://gerrit.libreoffice.org/9512
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Eike Rathke <erack@redhat.com>
(cherry picked from commit 20857b5d7536b3fcafa16ca6527ba459032933d7)
Reviewed-on: https://gerrit.libreoffice.org/9528
Reviewed-by: Markus Mohrhard <markus.mohrhard@googlemail.com>
Reviewed-by: Caolán McNamara <caolanm@redhat.com>