#[allow(clippy::many_single_char_names)] fn main() { let s = getline(); let a = s.split(' ').collect::>(); let h = a[0].parse::().unwrap(); let w = a[1].parse::().unwrap(); let mut s = vec![vec!['a'; w]; h]; for i in 0..h { s[i] = getline().chars().collect(); } if s[0][0] != s[h - 1][w - 1] { println!("0"); return; } let mut dp = vec![vec![vec![0; h]; w]; h]; dp[0][0][h - 1] = 1; let v = (h + w - 2) / 2; let m = 1_000_000_007; for i in 0..h { for j in 0..w { if i + j >= v { continue; } for ni in (0..h).rev() { // i + j = (h-1-ni) + (w-1-nj) let nj = w + h - (i + j) - 2 - ni; if nj >= w || i > ni || j > nj { continue; } if i + 1 < ni && s[i + 1][j] == s[ni - 1][nj] { dp[i + 1][j][ni - 1] += dp[i][j][ni]; dp[i + 1][j][ni - 1] %= m; } if i < ni && j < nj && s[i + 1][j] == s[ni][nj - 1] { dp[i + 1][j][ni] += dp[i][j][ni]; dp[i + 1][j][ni] %= m; } if i < ni && j < nj && s[i][j + 1] == s[ni - 1][nj] { dp[i][j + 1][ni - 1] += dp[i][j][ni]; dp[i][j + 1][ni - 1] %= m; } if j + 1 < nj && s[i][j + 1] == s[ni][nj - 1] { dp[i][j + 1][ni] += dp[i][j][ni]; dp[i][j + 1][ni] %= m; } } } } let mut ans = 0; for i in 0..h { for j in 0..w { for k in 0..h { if i + j == v { ans += dp[i][j][k]; ans %= m; } } } } println!("{}", ans); } fn getline() -> String { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim().to_string() }