1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include <iostream> #include <cstring> #include <algorithm> #include <deque> #include <queue> #include <vector> #include <set> #include <map> #include <cmath> #include <bitset> #include <iterator> #include <random> #include <iomanip>
using namespace std; typedef long long ll; const ll N=static_cast<ll>(1e3)+10; ll n,T,m; char maze[N][N]; const char ob[6]={'1','5','4','3'};
int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>T; while (T--) { ll ans=0; cin>>n>>m; for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ cin>>maze[i][j]; } } ll ceng=min(n,m)/2; for(int i=1;i<=ceng;++i){ ll cnt=0; ll upm=m-(i-1),upn=n-(i-1); for(int j=i;j<=upm;++j){ if(maze[i][j]==ob[cnt]){ ++cnt; }else { cnt=0; } if(cnt==4) ++ans,cnt=0; } for(int j=i+1;j<=upn;++j){ if(maze[j][upm]==ob[cnt]){ ++cnt; }else { cnt=0; } if(cnt==4) ++ans,cnt=0; } for(int j=upm-1;j>=i;--j){ if(maze[upn][j]==ob[cnt]){ ++cnt; }else { cnt=0; } if(cnt==4) ++ans,cnt=0; } for(int j=upn-1;j>=i;--j){ if(maze[j][i]==ob[cnt]){ ++cnt; }else { cnt=0; } if(cnt==4) ++ans,cnt=0; } ll step=1; if(cnt<2) continue; for(int j=i+1;j<=upm;++j){ if(step>=3) break; ++step; if(maze[i][j]==ob[cnt]){ ++cnt; }else { cnt=0; } if(cnt==4) ++ans,cnt=0; } for(int j=i+1;j<=upn;++j){ if(step>=3) break; ++step; if(maze[j][upm]==ob[cnt]){ ++cnt; }else { cnt=0; } if(cnt==4) ++ans,cnt=0; } } cout<<ans<<endl; } return 0; }
|