actGenshinImp

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

요약
서로 다른 13개 칸으로 이루어진 단순 경로 중 글자가 genshinimpact의 순환 이동과 일치하는 경로의 개수를 998244353으로 나눈 나머지를 구한다.
난이도

어려움10점 중 8점

유형
동적 계획법, DFS, 문자열 매칭, 조합론
정답자
아직 제출이 없습니다

문제

In the recent few years, this game has been so popular worldwide, it has even become a meme in the competitive programming community. Why would it be a bad idea to set problems about it?

You are given a grid GG of lowercase latin alphabets. A simple path on this grid is defined as a sequence of k≥1k \ge 1 distinct cells p_1,p_2,⋯ ,p_kp\_1,p\_2,\cdots,p\_k, such that p_i−1p\_{i-1} and p_ip\_i are adjacent either vertically or horizontally. Also, for some simple path dd of mm cells, let f(d)f(d) be the string of length mm such that (f(d))_i(f(d))\_i is the letter written on the cell d_id\_i of the grid GG.

Please find the number of simple paths aa of 1313 cells, such that f(a)f(a) is a cyclic shift of "genshinimpact". As the answer may be very large, you are only required to find the value modulo 998,244,353998 \\, 244 \\, 353.

입력

The first line contains two integers rr and cc --- the number of rows and the number of columns of GG. (1≤r,c≤5001 \le r,c \le 500)

Each of the rr following lines contains a string of length cc consisting of lowercase latin letters. The ii-th of them is the ii-th row of the grid GG.

출력

Output the answer modulo 998,244,353998 \\, 244 \\, 353 on one line.

힌트

The grid in the sample input contains 88 simple paths satisfying the condition. The 88 simple paths are as follows.

예제1

  1. 예제 1

    입력
    3 7
    gshimct
    eninpag
    ppmpact
    
    예상 출력
    8