fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define mp make_pair
  5. #define pb push_back
  6. #define lf long double
  7. typedef vector <ll> vi;
  8. typedef pair <ll,ll> ii;
  9. typedef vector <pair <int,int> >vii;
  10. typedef vector <vector <ll> > vv;
  11. typedef vector <string> vs;
  12. ll mod=1000000007;
  13. int main()
  14. {
  15. ll n,m;
  16. cin>>n>>m;
  17. vs s(n,"");
  18. for(ll i=0;i<n;i++)
  19. cin>>s[i];
  20. // vector <vector <ii> > a(n/3+1,vector <ii> ());
  21. map <pair <pair <ll,ll> ,ll > ,bool> ma;
  22. for(ll len=1;len<=n/3;len++)
  23. {
  24. for(ll i=0;i+3*len-1<n;i++)
  25. {
  26. for(ll j=0;j<m;j++)
  27. {
  28. char c1=s[i][j];
  29. bool pos=true;
  30. for(ll i1=i;i1<i+len;i1++)
  31. {
  32. if(s[i1][j]!=c1)
  33. {
  34. pos=false;
  35. break;
  36. }
  37. }
  38. if(pos==false)
  39. continue;
  40. char c2=s[i+len][j];
  41. if(c2==c1)
  42. pos=false;
  43. for(ll i1=i+len;i1<i+2*len;i1++)
  44. {
  45. if(s[i1][j]!=c2)
  46. {
  47. pos=false;
  48. break;
  49. }
  50. }
  51. if(pos==false)
  52. continue;
  53. char c3=s[i+2*len][j];
  54. if(c3==c2)
  55. pos=false;
  56. for(ll i1=i+2*len;i1<i+3*len;i1++)
  57. {
  58. if(s[i1][j]!=c3)
  59. {
  60. pos=false;
  61. break;
  62. }
  63. }
  64. if(pos==false)
  65. continue;
  66. if(pos==true)
  67. {
  68. ma[mp(mp(i,j),len)]=true;
  69. }
  70. }
  71. }
  72. }
  73. ll ans=ma.size();
  74. for(ll len=1;len<=n/3;len++)
  75. {
  76. for(ll i=0;i<n;i++)
  77. {
  78. for(ll j=0;j<m;j++)
  79. {
  80. if(ma[mp(mp(i,j),len)])
  81. {
  82. ll num=1;
  83. char c1=s[i][j];
  84. char c2=s[i+len][j];
  85. char c3=s[i+2*len][j];
  86. ll j1=j+1;
  87. while(j1<m)
  88. {
  89. if(ma[mp(mp(i,j1),len)])
  90. {
  91. if(c1==s[i][j1] && c2==s[i+len][j1] && s[i+2*(len)][j1]==c3)
  92. {
  93. num++;
  94. j1++;
  95. }
  96. else
  97. break;
  98. }
  99. else
  100. {
  101. break;
  102. }
  103. }
  104. ans+=num*(num-1)/2;
  105. j=j1-1;
  106. }
  107.  
  108. }
  109. }
  110. }
  111. cout<<ans<<endl;
  112. }
Success #stdin #stdout 0s 15240KB
stdin
9 8
wmmmmrww
wmmmmrww
wxxxxgww
ggggxgww
ssssmcww
xxxxzcww
wkwwwkww
wcccccww
wwwwwwww
stdout
45