#[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(); } let mut dp = vec![vec![vec![vec![0; w]; h]; w]; h]; dp[0][0][h - 1][w - 1] = 1; let m = 1_000_000_007; for i in 0..h { for j in 0..w { for ni in (0..h).rev() { for nj in (0..w).rev() { if ni < i || nj < j { continue; } if i + 1 < ni && s[i + 1][j] == s[ni - 1][nj] { dp[i + 1][j][ni - 1][nj] += dp[i][j][ni][nj]; dp[i + 1][j][ni - 1][nj] %= m; } if i < ni && j < nj && s[i + 1][j] == s[ni][nj - 1] { dp[i + 1][j][ni][nj - 1] += dp[i][j][ni][nj]; dp[i + 1][j][ni][nj - 1] %= m; } if i < ni && j < nj && s[i][j + 1] == s[ni - 1][nj] { dp[i][j + 1][ni - 1][nj] += dp[i][j][ni][nj]; dp[i][j + 1][ni - 1][nj] %= m; } if j + 1 < nj && s[i][j + 1] == s[ni][nj - 1] { dp[i][j + 1][ni][nj - 1] += dp[i][j][ni][nj]; dp[i][j + 1][ni][nj - 1] %= m; } } } } } let mut ans = 0; for i in 0..h { for j in 0..w { ans += dp[i][j][i][j]; ans %= m; } } println!("{}", ans); } fn getline() -> String { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim().to_string() }