#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[15][15];
bool vis[15][15] = {};
int dx[4]{ 0,1,0,-1 };
int dy[4]{ -1,0,1,0 };
int sum = 0, num = 0, mixn = -1;
void dfs(int x, int y) {
if (x == n && y == m) {//到达目标点
mixn = max(num, mixn);
return;
}
else {
for (int i = 0; i < 4; i++) {
int xx = x + dx[i];
int yy = y + dy[i];//记录新位置
if (vis[xx][yy] == 0 && xx >= 1 && yy >= 1 && xx <= n && yy <= m && a[xx][yy] != '#') {
vis[xx][yy] = 1;
if (a[x][y] != '*') {
num += a[x][y] - '0';
}
dfs(xx, yy);
vis[xx][yy] = 0;
num -= (a[x][y] - '0');
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
vis[1][1] = 1;
dfs(1, 1);
cout << mixn;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
char a[25][25];
int n, sum = -1, x, y, m, s = 0, vis[25][25];
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };
void f(int x, int y, int step, int cnt) {
if (x == n && y == m) {
sum = max(sum, cnt);
return;
}
vis[x][y] = 1;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx >= 1 && ty >= 1 && tx <= n && ty <= m && a[tx][ty] != '#' && vis[tx][ty] == 0) {
cnt += a[x][y] - '0';
f(tx, ty, step + 1,cnt);
}
}
vis[x][y] = 0;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
f(1, 1, 0, 0);
cout << sum;
}