Daisies on a Grid

시간 제한2초메모리 제한2048 MB

요약
작은 격자의 빈 칸을 0, 1, 2 색으로 채워 이 자동자가 결국 모든 칸을 같은 색으로 만들도록 하고, 그런 모든 채우기에서 왼쪽 위 칸의 안정 초를 모두 더한다.
난이도

어려움10점 중 9점

유형
동적 계획법, 구현, 조합론, 시뮬레이션
정답자
아직 제출이 없습니다

문제

You are given a grid consisting of nn rows and mm columns where each cell is a place for a magical daisy. Each daisy can have one of the three possible colors, represented by 00, 11, and 22.

Each daisy gazes at its surrounding flowers and desires to transform into the appearance of other daisies. If at the start of some second, a daisy of color cc has at least one neighboring flower above, below, to the left, or to the right with color c−1c - 1, then in the next second, this daisy will turn into color c−1c - 1; otherwise, its color remains cc in the next second (we consider the colors modulo 33).

Consider an initial arrangement of daisies on the grid. The arrangement is beautiful if, after a finite number of seconds, all daisies become the same color.

It is easy to see that, for a beautiful daisy arrangement, each flower has an earliest second tt such that its color remains unchanged after second tt. We call this second the flower's stable second. We start counting from second 00, so, if a flower never changes color, its stable second is 00.

Now, some daisies are already placed in some cells of the grid, and the other cells are empty. How many ways are there to place daisies in the remaining cells so that the arrangement is beautiful? Also, for all these beautiful arrangements, what is the total sum of stable seconds for the daisy in the top left cell (the cell on the intersection of the first row and the first column)?

As both numbers may be very large, find them modulo 998,244,353998\\,244\\,353.

입력

The first line of the input contains two integers nn and mm (2≤n≤52 \le n \le 5, 2≤m≤502 \le m \le 50).

Then the description of the initial state of the grid follows: the following nn lines contain mm integers each. The jj-th integer in the ii-th line, a_i,j∈0,1,2,3a\_{i,j} \in \\{0, 1, 2, 3\\}, represents the state of the corresponding cell. Here, a_i,j∈0,1,2a\_{i,j} \in \\{0, 1, 2\\} indicates a daisy of the respective color, and a_i,j=3a\_{i,j} = 3 indicates that the cell contains no daisy.

출력

Print a line with two integers: the number of beautiful arrangements and the total sum of stable seconds for the daisies in the top left cell in those arrangements, both modulo 998,244,353998\\,244\\,353.

예제2

  1. 예제 1

    입력
    2 2
    1 0
    3 2
    
    예상 출력
    1 2
    
  2. 예제 2

    입력
    5 5
    3 3 3 3 2
    2 3 3 3 1
    1 3 3 3 3
    3 3 3 3 3
    3 3 3 3 3
    
    예상 출력
    50830224 170059345