#pragma GCC target("avx2") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define ALL(x) (x).begin(), (x).end() #define PC(x) __builtin_popcount(x) #define PCL(x) __builtin_popcountll(x) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef pair pll; struct edge { int to, cost, id; }; const double PI = 3.14159265358979323846; const double PI2 = PI * 2.0; const double EPS = 1E-09; const ll MOD = 1E+09 + 7; // =998244353; const ll INFL = 1E18; const int INFI = 1E09; const int MAX_N = 2E+02; ll dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 }; int H, W; char A[MAX_N + 2][MAX_N + 2]; ll dp[MAX_N + 3][MAX_N + 3][MAX_N + 3]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> H >> W; for (int i = 1; i <= H; ++i) { string s; cin >> s; for (int j = 1; j <= W; ++j) { A[i][j] = s[j - 1]; } } if (A[1][1] == A[H][W]) dp[1][1][H] = 1; for (int i = 1; i <= H; ++i) { for (int j = 1; j <= W; ++j) { // if (i + j >= (H + W - 1) / 2) // break; for (int k = H; k >= 1; k--) { int l = H + W - k - (i + j - 2); if (l < 1 || l > W) continue; char x = A[i + 1][j]; char y = A[i][j + 1]; char z = A[k][l - 1]; char w = A[k - 1][l]; if (x == z) { dp[i + 1][j][k] += dp[i][j][k]; dp[i + 1][j][k] %= MOD; } if (x == w) { dp[i + 1][j][k - 1] += dp[i][j][k]; dp[i + 1][j][k - 1] %= MOD; } if (y == z) { dp[i][j + 1][k] += dp[i][j][k]; dp[i][j + 1][k] %= MOD; } if (y == w) { dp[i][j + 1][k - 1] += dp[i][j][k]; dp[i][j + 1][k - 1] %= MOD; } } } } ll ans = 0; int N = H + W - 1; if (N % 2 == 0) { for (int i = 1; i <= H; ++i) { int j = N / 2 - i + 1; if (j < 1 || j > W) continue; ans += dp[i][j][i + 1] + dp[i][j][i]; ans %= MOD; } } else { if (H == 1 && W == 1) { ans = 1; } else { for (int i = 1; i <= H; ++i) { int j = N / 2 - i + 1; if (j < 1 || j > W) continue; ans += dp[i][j][i + 1] * 2 + dp[i][j][i] + dp[i][j][i + 2]; ans %= MOD; } } } // for (int i = 0; i < N; i++) { // for (int j = 0; j < N; j++) { // cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n"; // } // } cout << ans << "\n"; return 0; }