/* Question: Given a matrix, the southwest corner is the starting point. Each unit has a valuable gold mine (# indicates rock, inaccessible, * indicates space-time gate, and can reach a specified Unit) what benefits can be obtained at most? Strong connectivity, longest path; if there is no space-time portal, it is the longest path of a pure directed acyclic graph. Now there is a space-time portal, if you want to scale down the connected component, you can create a directed acyclic graph for the scaled-down graph, then, you can find the longest path. */# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <climits> # include <queue> # include <vector> using namespace std; const int MAXV = 49*49; int n, m; int value [MAXV]; char graph [41] [41]; struct spfa // calculates the longest path {static const in T INF =-(1 <30); struct Edge {int v, next; int w;} ep [MAXV * MAXV]; int first [MAXV]; int d [MAXV]; int e; int V; inline void init (int n) {memset (first,-1, sizeof (first); e = 0; V = n;} void addEdge (int u, int v, int dd) {ep [e]. v = v; ep [e]. w = dd; ep [e]. next = first [u]; first [u] = e ++;} void solve (int source) {static bool OK [MAXV]; memset (OK, false, sizeof (OK); queue <int> q; q. push (source); for (int I = 0; I <V; I ++) d [I] = INF; d [Source] = 0; while (! Q. empty () {int x = q. front (); q. pop (); OK [x] = false; for (int k = first [x]; k! =-1; k = ep [k]. next) {int v = ep [k]. v; if (d [v] <d [x] + ep [k]. w) {d [v] = d [x] + ep [k]. w; if (OK [v] = false) {OK [v] = true; q. push (v) ;}}}} g; struct TarJan {struct Edge {int v, next;} edge [4 * MAXV]; int first [MAXV], ins [MAXV], dfn [MAXV], low [MAXV], Stack [MAXV], scc [MAXV]; int top, index, sccnum, e, V; void init (int n) {e = 0; V = n; memset (first,-1, sizeof (first [0]) * n); memset (ins, 0, sizeof (ins [0]) * n); memset (dfn, 0, sizeof (dfn [0]) * n); memset (low, 0, Sizeof (dfn [0]) * n);} void addEdge (int u, int v) {edge [e]. v = v; edge [e]. next = first [u]; first [u] = e ++;} void solve () {index = 1; top =-1; sccnum =-1; for (int I = 0; I <V; I ++) if (dfn [I] = 0) tarjan (I);} void tarjan (int u) {int v; low [u] = dfn [u] = index ++; Stack [++ top] = u; ins [u] = true; for (int k = first [u]; k! =-1; k = edge [k]. next) {v = edge [k]. v; if (dfn [v] = 0) {tarjan (v); low [u] = min (low [u], low [v]);} else if (ins [v]) {low [u] = min (low [u], dfn [v]);} if (dfn [u] = low [u]) {sccnum ++; do {v = Stack [top --]; ins [v] = false; scc [v] = sccnum;} while (u! = V) ;}}my; void init () {int x, y; memset (value, 0, sizeof (value); my. init (n * m); for (int I = 0; I <n; I ++) {for (int j = 0; j <m; j ++) if (graph [I] [j]! = '#') {If (I! = 0) my. addEdge (I-1) * m + j, I * m + j); if (j! = 0) my. addEdge (I * m + J-1, I * m + j); if (graph [I] [j] = '*') {scanf ("% d ", & x, & y); if (graph [x] [y]! = '#') My. addEdge (I * m + j, x * m + y) ;}} my. solve ();} void solve () {g. init (my. sccnum + 2); for (int I = 0; I <n; I ++) for (int j = 0; j <m; j ++) {if (graph [I] [j]! = '*' & Graph [I] [j]! = '#') {Value [my. scc [I * m + j] + = graph [I] [j]-'0' ;}} for (int I = 0; I <n * m; I ++) {for (int k = my. first [I]; k! =-1; k = my. edge [k]. next) {int v = my. edge [k]. v; if (my. scc [I]! = My. scc [v]) {g. addEdge (my. scc [I], my. scc [v], value [my. scc [v]) ;}} int ans = 0; g. solve (my. scc [0]); for (int I = 0; I <= my. sccnum; I ++) {if (g. d [I]> ans) ans = g. d [I];} printf ("% d \ n", ans + value [my. scc [0]);} int main () {int ca; scanf ("% d", & ca); while (ca --) {scanf ("% d", & n, & m); for (int I = 0; I <n; I ++) {scanf ("% s ", graph [I]);} init (); solve ();} return 0 ;}