#include #include using namespace std; char x[1005][1005]; int visted[1005][1005]; int y[1005][1005]; int main() { int di[]={0,1,-1,0}; int dj[]={1,0,0,-1}; int t; cin >> t; for(int T = 0; T < t; T++) { int n,m; cin>>n>>m; for(int i = 0;i>x[i][j]; } } for(int i = 0; i < 1004; i++) { for(int j = 0; j < 1004; j++) { visted[i][j] = false; y[i][j] = 2e9; } } int si = 0; int sj = 0; int ei = 0; int ej = 0; for(int i = 0;ibfs; bfs.push(ei); bfs.push(ej); bfs.push(cekori); while(!bfs.empty()){ int ci=bfs.front(); bfs.pop(); int cj=bfs.front(); bfs.pop(); int cekor=bfs.front(); bfs.pop(); y[ci][cj]=cekor; for(int i=0;i<4;i++){ int ti=ci+di[i]; int tj=cj+dj[i]; if(ti>=0 && ti=0 && tj= '1' && x[i][j]<='9'){ dist2=y[i][j]; if(dist2 <= dist) { int c = x[i][j] - '0'; hu -= c; } } } } if(hu<0){ cout<<0<