/*input
10 165
82 94 21 65 28 22 61 80 81 79
93 35 59 85 96 1 78 72 43 5
12 15 97 49 69 53 18 73 6 58
60 14 23 19 44 99 64 17 29 67
24 39 56 92 88 7 48 75 36 91
74 16 26 10 40 63 45 76 86 3
9 66 42 84 38 51 25 2 33 41
87 54 57 62 47 31 68 11 83 8
46 27 55 70 52 98 20 77 89 34
32 71 30 50 90 4 37 95 13 100

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long 
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i,n) for (int i = 1; i <= n; i++)
#define REP(i,n) for (int i = 0; i < (int)n; i++)

#define all(v) v.begin(),v.end()
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 998244353;
const double PI = 3.14159265358979323846264338;

int raise(int a,int n,int m = MOD){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= raise(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

const int N = 51;
vector<int> adj[N];
int a[N][N];
bool vis[N];
int sz = 0;
int n,k;
int ans = 1;

void dfs(int u){
	vis[u] = 1;
	sz++;
	ans *= sz;
	ans %= MOD;
	for(int v:adj[u]){
		if(vis[v])continue;
		dfs(v);
	}
}

void solve(){
  	cin >> n >> k;
  	RE(i,n){
  		RE(j,n){
  			cin >> a[i][j];
  		}
  	}
  	RE(x,n){
  		RE(y,n){
  			// columns
  			if(x == y)continue;
  			bool ok = 1;
  			RE(i,n){
  				ok &= (a[i][x]+a[i][y] <= k);
  			}
  			if(ok)adj[x].pb(y);
  		}
  	}
  	RE(i,n){
  		if(vis[i])continue;
  		sz = 0;
  		dfs(i);
  	}
  	// rows
  	RE(i,n){
  		vis[i] = 0;
  		adj[i].clear();
  	}
  	RE(x,n){
  		RE(y,n){
  			// rows
  			if(x == y)continue;
  			bool ok = 1;
  			RE(i,n){
  				ok &= (a[x][i]+a[y][i] <= k);
  			}
  			if(ok)adj[x].pb(y);
  		}
  	}
  	RE(i,n){
  		if(vis[i])continue;
  		sz = 0;
  		dfs(i);
  	}
  	cout << ans;
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }
  return 0;
}
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define t3 1000+10
#define t4 10000+10
#define t5 100000+10
#define t6 1000000+10
#define MOD 1000000007
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i--)
#define pb push_back
const double pi = acos(-1.0);

namespace X{
  ll r(){ll r;scanf("%lld",&r);return r;} // read
  void add(ll &v0,ll &v1){(v0+=v1%MOD)%=MOD;} // add with MOD
  ll mpow(ll v,ll mi){ll r = 1;while(mi){if(mi%2)(r*=v)%=MOD;(v*=v)%=MOD;mi>>=1;};return r;} // quick power with MOD
  ll mod(ll v){return (v%MOD+MOD)%MOD;} // output
  ll gcd(ll a,ll b){return b == 0?a:gcd(b,a%b);}
};

char s[5010];

pair<int,int>cnt[5010];

int main(){
  int n;
  scanf("%d",&n);
  scanf("%s",s);
  cnt[0] ={0,0};
  rep(i,0,n){
    cnt[i+1] = cnt[i];
    if(s[i] == 'A'){
      cnt[i+1].first ++;
    }else if(s[i] == 'T'){
      cnt[i+1].first --;
    }else if(s[i] == 'C'){
      cnt[i+1].second ++;
    }else if(s[i] == 'G'){
      cnt[i+1].second --;
    }
  }
  int ans = 0;
  rep(i,0,n){
    rep(j,i+1,n+1){
      if(cnt[j].first == cnt[i].first && cnt[j].second == cnt[i].second){
        ans++;
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}

/*
 *
 * hash prime
   61,83,113,151,211,281,379,509683,911
   1217,1627,2179,2909,3881,6907,9209
   12281,16381,21841,29123,38833,51787,69061,92083
   122777,163729,218357,291143,388211,517619,690163,999983
   1226959,1635947,2181271,2908361,3877817,5170427,6893911,9191891
   12255871,16341163,21788233,29050993,38734667,51646229,68861641,91815541
   122420729,163227661,217636919,290182597,386910137,515880193,687840301,917120411
   1000000007,1000000009
 * */
