#P6582. Exam Hall Seat Arrangement

    ID: 19794 Type: Default 1000ms 256MiB

Exam Hall Seat Arrangement

Exam Hall Seat Arrangement

You are given an exam hall represented as an n × m grid. Each cell is either 'O' or 'X' where 'O' represents a seat and 'X' represents an empty space. It is guaranteed that there is at least one seat and every seat must be occupied by a student.

The seating arrangement must satisfy the following conditions:

  • The seats in the hall can be partitioned into several bars (i.e. linear segments). A bar is defined as a set of seats forming a simple path on the grid. In a valid bar with more than one seat, exactly two endpoints have exactly one adjacent seat (within the bar) and every other seat has exactly two adjacent seats. A single seat is also considered a valid bar.
  • No two adjacent seats (sharing a common edge) may be occupied by students from the same school. There are students from k different schools and each seat must be assigned a school.

Your task is to determine the total number of valid seating assignments. If the grid cannot form a valid ION 2048 exam hall (i.e. the seats cannot be partitioned into valid bars), output 0. Since the answer might be large, output it modulo $998244353$.

The number of ways to assign schools to a bar of length L is:

\( k \times (k-1)^{L-1} \)

and the total number of valid assignments is the product of the number of ways for each connected component (bar) of seats.

inputFormat

The first line contains three integers n, m and k --- the number of rows, columns, and schools respectively.

Each of the next n lines contains a string of length m made up of characters 'O' and 'X', representing the exam hall.

outputFormat

Output a single integer: the number of valid seating assignments modulo $998244353$.

sample

1 1 3
O
3