MOD = 10**9 + 7 H, W = map(int, input().split()) grid = [input().strip() for _ in range(H)] if H == 1 and W == 1: print(1) else: max_d = (H + W - 2) // 2 from collections import defaultdict prev_dp = defaultdict(int) # Initialize for d=0 if grid[0][0] == grid[H-1][W-1]: prev_dp[(1, 1, H, W)] = 1 for d in range(max_d): next_dp = defaultdict(int) for (i, j, k, l), cnt in prev_dp.items(): if grid[i-1][j-1] != grid[k-1][l-1]: continue # Generate start moves (down or right) start_moves = [] if i < H: start_moves.append((i+1, j)) if j < W: start_moves.append((i, j+1)) # Generate end moves (up or left) end_moves = [] if k > 1: end_moves.append((k-1, l)) if l > 1: end_moves.append((k, l-1)) # Process all combinations for new_i, new_j in start_moves: for new_k, new_l in end_moves: if grid[new_i-1][new_j-1] == grid[new_k-1][new_l-1]: next_dp[(new_i, new_j, new_k, new_l)] = (next_dp[(new_i, new_j, new_k, new_l)] + cnt) % MOD prev_dp = next_dp # Sum all states where (i,j) == (k,l) answer = 0 for (i, j, k, l), cnt in prev_dp.items(): if i == k and j == l: answer = (answer + cnt) % MOD print(answer)