#include <bits/stdc++.h>

using namespace std;
int t, x, y, k, cnt, a[330][330];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void dfs(int p, int q)
{
    a[p][q] = 0;
    for(int i = 0 ; i < 4 ; i++)
    {
        if( p+dx[i] < 0 or p+dx[i] > x-1 or q+dy[i] < 0 or q+dy[i] > y-1 ) continue;
        if( a[p+dx[i]][q+dy[i]] > k )
            dfs(p+dx[i], q+dy[i]);
    }
}
int main()
{
    freopen(".1.in","r",stdin);
    //freopen(".1.out","w",stdout);
    cin>>t;
    while(t--)
    {
        cnt = 0;
        cin>>x>>y>>k;
        for(int i = 0 ; i < x ; i++ )
            for(int j = 0 ; j < y ; j++ )
                cin>>a[i][j];
        for(int i = 0 ; i < x ; i++ )
            for(int j = 0 ; j < y ; j++ )
                if( a[i][j] > k )
                    dfs(i, j), cnt++;
        cout<<cnt<<endl;
    }
}
