// -*- coding:utf-8-unix -*- // #![feature(map_first_last)] #![allow(dead_code)] #![allow(unused_imports)] #![allow(unused_macros)] use std::any::Any; use std::cmp::Ordering::*; use std::collections::*; use std::convert::*; use std::convert::{From, Into}; use std::error::Error; use std::f64::consts::PI; use std::fmt::Debug; use std::fmt::Display; use std::fs::File; use std::hash::Hash; use std::io::prelude::*; use std::io::*; use std::iter::Filter; use std::marker::Copy; use std::mem::*; use std::ops::Bound::*; use std::ops::RangeBounds; use std::ops::{Add, Mul, Neg, Sub}; use std::process; use std::slice::from_raw_parts; use std::str; use std::vec; const INF: i64 = 1223372036854775807; const UINF: usize = INF as usize; // const FINF: f64 = 122337203685.0; const LINF: i64 = 2147483647; const FINF: f64 = LINF as f64; const INF128: i128 = 1223372036854775807000000000000; const MOD: i64 = 1000000007; // const MOD: i64 = 998244353; const UMOD: usize = MOD as usize; const MPI: f64 = 3.14159265358979323846264338327950288f64; // const MOD: i64 = INF; use std::cmp::*; use std::collections::*; use std::io::stdin; use std::io::stdout; use std::io::Write; macro_rules! p { ($x:expr) => { println!("{}", $x); }; } macro_rules! d { ($x:expr) => { println!("{:?}", $x); }; } // use str::Chars; macro_rules! M { (a :expr ) => { M::new({ a }) }; } #[derive(Copy, Clone, Debug)] pub struct M(i64); impl M { fn new(x: i64) -> Self { M(x.rem_euclid(MOD)) } fn pow(self, n: usize) -> Self { match n { 0 => M::new(1), _ => { let mut a = self.pow(n >> 1); a *= a; if n & 1 == 1 { a *= self; } a } } } fn inv(self) -> Self { self.pow((MOD - 2) as usize) } } impl std::ops::Neg for M { type Output = M; fn neg(self) -> Self::Output { Self::new(-self.0) } } impl std::ops::AddAssign for M { fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; self.0 %= MOD; } } impl std::ops::AddAssign for M { fn add_assign(&mut self, rhs: i64) { *self += M::new(rhs); } } impl std::ops::AddAssign for M { fn add_assign(&mut self, rhs: usize) { *self += M::new(rhs as i64); } } impl std::ops::Add for M where M: std::ops::AddAssign, { type Output = Self; fn add(self, other: T) -> Self { let mut res = self; res += other; res } } impl std::ops::SubAssign for M { fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0; if self.0 < 0 { self.0 += MOD; } } } impl std::ops::SubAssign for M { fn sub_assign(&mut self, rhs: i64) { *self -= M::new(rhs); if (*self).0 < 0 { self.0 += MOD; } } } impl std::ops::SubAssign for M { fn sub_assign(&mut self, rhs: usize) { *self -= M::new(rhs as i64); if (*self).0 < 0 { self.0 += MOD; } } } impl std::ops::Sub for M where M: std::ops::SubAssign, { type Output = Self; fn sub(self, other: T) -> Self { let mut res = self; res -= other; res } } impl std::ops::MulAssign for M { fn mul_assign(&mut self, rhs: Self) { self.0 %= MOD; self.0 *= (rhs.0 % MOD); self.0 %= MOD; } } impl std::ops::MulAssign for M { fn mul_assign(&mut self, rhs: i64) { *self *= M::new(rhs); } } impl std::ops::MulAssign for M { fn mul_assign(&mut self, rhs: usize) { *self *= M::new(rhs as i64); } } impl std::ops::Mul for M where M: std::ops::MulAssign, { type Output = Self; fn mul(self, other: T) -> Self { let mut res = self; res *= other; res } } impl std::ops::DivAssign for M { fn div_assign(&mut self, rhs: Self) { *self *= rhs.inv(); } } impl std::ops::DivAssign for M { fn div_assign(&mut self, rhs: i64) { *self /= M::new(rhs); } } impl std::ops::DivAssign for M { fn div_assign(&mut self, rhs: usize) { *self /= M::new(rhs as i64); } } impl std::ops::Div for M where M: std::ops::DivAssign, { type Output = Self; fn div(self, other: T) -> Self { let mut res = self; res /= other; res } } impl std::fmt::Display for M { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl std::ops::Deref for M { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 } } impl std::ops::DerefMut for M { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } #[allow(dead_code)] pub fn gcd(a: usize, b: usize) -> usize { if b == 0 { a } else { gcd(b, a % b) } } #[allow(dead_code)] pub fn lcm(a: usize, b: usize) -> usize { a / gcd(a, b) * b } #[allow(dead_code)] /// (gcd, x, y) pub fn extgcd(a: i64, b: i64) -> (i64, i64, i64) { if b == 0 { (a, 1, 0) } else { let (gcd, x, y) = extgcd(b, a % b); (gcd, y, x - (a / b) * y) } } #[allow(dead_code)] /// x ^ n % m pub fn mod_pow(x: usize, n: usize, m: usize) -> usize { let mut res = 1; let mut x = x % m; let mut n = n; while n > 0 { if n & 1 == 1 { res = (res * x) % m; } x = (x * x) % m; n >>= 1; } res } pub struct Combination { m: usize, f_table: Vec, moi: Vec, } impl Combination { // 0 <= size <= 10^8 is constrained. pub fn new(mod_num: usize, table_size: usize) -> Self { Self { m: mod_num, f_table: vec![0; table_size], moi: vec![0; 0], } } pub fn build(&mut self) { let size = self.f_table.len(); self.f_table = self.fact_table(size, self.m); self.moi = self.fact_inv_table(size, self.m); } fn fact_table(&mut self, len: usize, m: usize) -> Vec { let mut res = vec![1; len + 1]; for i in 1..len + 1 { res[i] = (i as usize * res[i - 1]) % m; } res } fn fact_inv_table(&mut self, len: usize, m: usize) -> Vec { let mut res = vec![1; len + 1]; for i in 1..len + 1 { res[i] = (res[i - 1] * self.mod_inverse(i, m)) % m; } res } pub fn p(&mut self, n: usize, k: usize) -> i64 { let p = MOD as usize; if k == 0 { return 1; } if n < k { 0 } else { let (a1, e1) = self.mod_fact(n, p); let (a2, e2) = self.mod_fact(k, p); let (a3, e3) = self.mod_fact(n - k, p); if e1 > e2 + e3 { 0 } else { let moi = self.mod_inverse(a3 % p, p); (a1 * self.mod_inverse(a3 % p, p) % p) as i64 } } } pub fn c(&mut self, n: usize, k: usize) -> i64 { let p = MOD as usize; if n == 0 && k == 0 { return 1; } if n == 0 { return 0; } if k == 0 { return 1; } if n < k { 0 } else { let (a1, e1) = self.mod_fact(n, p); let (a2, e2) = self.mod_fact(k, p); let (a3, e3) = self.mod_fact(n - k, p); if e1 > e2 + e3 { 0 } else { (((a1 * &self.moi[k]) % p * &self.moi[n - k]) % p) as i64 } } } pub fn h(&mut self, n: usize, k: usize) -> i64 { return self.c(n + k - 1, k); } pub fn factorial(&mut self, n: usize) -> i64 { return self.p(n, n); } fn extgcd(&mut self, a: i64, b: i64) -> (i64, i64, i64) { if b == 0 { (a, 1, 0) } else { let (gcd, x, y) = extgcd(b, a % b); (gcd, y, x - (a / b) * y) } } fn mod_inverse(&mut self, a: usize, m: usize) -> usize { let (_, x, _) = self.extgcd(a as i64, m as i64); ((m as i64 + x) as usize % m) % m } fn mod_fact(&mut self, n: usize, p: usize) -> (usize, usize) { if n == 0 { (1, 0) } else { let (a, b) = self.mod_fact(n / p, p); let pow = b + n / p; if n / p % 2 != 0 { (a * (p - self.f_table[(n % p) as usize]) % p, pow) } else { (a * self.f_table[(n % p) as usize] % p, pow) } } } } fn main() { solve(); } // use str::Chars; #[allow(dead_code)] fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } #[allow(dead_code)] fn readi() -> (i64) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); iter.next().unwrap().parse::().unwrap() } #[allow(dead_code)] fn read_vec() -> Vec { read::() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } #[allow(dead_code)] fn read_mat(n: u32) -> Vec> { (0..n).map(|_| read_vec()).collect() } #[allow(dead_code)] fn readii() -> (i64, i64) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) } fn readff() -> (f64, f64) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) } #[allow(dead_code)] fn readiii() -> (i64, i64, i64) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) } #[allow(dead_code)] fn readuu() -> (usize, usize) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) } fn readcc() -> (char, char) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) } fn readuuu() -> (usize, usize, usize) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) } #[allow(dead_code)] fn readiiii() -> (i64, i64, i64, i64) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) } #[allow(dead_code)] fn readuuuu() -> (usize, usize, usize, usize) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) } fn solve() { let (h, w) = readuu(); let mut vv: Vec> = vec![vec!['0' as char; (0) as usize]; (h) as usize]; for i in 0..h { let s: String = read(); let vvv = s.chars().collect(); vv[i] = vvv; } let mut used = BTreeSet::new(); let mut data = BTreeMap::new(); let mut st = VecDeque::new(); st.push_front((0, h * w - 1)); data.insert((0, h * w - 1), 1 as usize); let mut res = 0; while !st.is_empty() { let (x, y) = st.pop_back().unwrap(); if used.contains(&(x, y)) { continue; } used.insert((x, y)); let cnt = *data.entry((x, y)).or_insert(0); let xi = (x / w) as i64; let xj = (x % w) as i64; let yi = (y / w) as i64; let yj = (y % w) as i64; if vv[xi as usize][xj as usize] != vv[yi as usize][yj as usize] { continue; } let dxi = vec![1, 1, 0, 0]; let dxj = vec![0, 0, 1, 1]; let dyi = vec![0, -1, 0, -1]; let dyj = vec![-1, 0, -1, 0]; if (xi - yi).abs() + (xj - yj).abs() <= 1 { res += cnt; res %= UMOD; continue; } for ii in 0..4 { let nxi = xi + dxi[ii]; let nxj = xj + dxj[ii]; let nyi = yi + dyi[ii]; let nyj = yj + dyj[ii]; if nxi >= 0 && nxi < h as i64 && nxj < w as i64 && nxj >= 0 && nyi >= 0 && nyi < h as i64 && nyj < w as i64 && nyj >= 0 { if vv[nxi as usize][nxj as usize] == vv[nyi as usize][nyj as usize] { let nx = (nxi * w as i64 + nxj) as usize; let ny = (nyi * w as i64 + nyj) as usize; let ncnt = *data.entry((nx, ny)).or_insert(0); data.insert((nx, ny), (cnt + ncnt) % UMOD); st.push_front((nx, ny)); } } } } // let res = *data.entry((h * w - 1, 0)).or_insert(0); println!("{}", res); }