Digit-only subrectangles
Time limit3sMemory limit1024 MB
Given a grid of digits and asterisks, sum the squared digit totals of all rectangles that contain only digits, modulo 998244353.
- Level
Medium6 of 10
- Topics
- Prefix sum, Math, Implementation
- Solved
- No attempts yet
Statement
The grid has rows and columns of square cells. Each cell holds either a digit or an asterisk (‘*’). Let denote the cell in row from the top and column from the left.
A subrectangle is a set of cells that forms a rectangle. Precisely, a set is a subrectangle if there are integers , , , with , , and . A subrectangle is digit-only if every cell in it holds a digit. The score of a digit-only subrectangle is the square of the sum of its digits.
Compute the sum of scores of all digit-only subrectangles, modulo .
Input
The input is given in the following format.
The first line contains and (, ). Each of the next lines contains characters, where is the character in cell : a digit from 0 to 9 or an asterisk (‘*’). It is guaranteed that at least one digit-only subrectangle exists.
Output
Print the sum of scores of all digit-only subrectangles modulo on one line.
Hint
The first test case has five digit-only subrectangles, shown in the figure below. Their scores sum to .

Figure F.1. Digit-only subrectangles in the first test case