#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	freopen("TOWER.IN", "r", stdin);
	int t;
	cin >> t;
	while (t--)
	{
		int h, l;
		long time = 0;
		cin >> h >> l;
		for (int i = 0; i < h; i++ )
		{
			vector < pair < int, int > > v;
			int temp, sum = 0;
			for (int j = 0; j < l; j++)
			{
				cin >> temp;
				if (temp != -1)
				{
					sum++;
					v.push_back(make_pair(temp, j));
				}
			}
			time += sum * 2 * i * 10;
			sort(v.begin(), v.end());
			int cur_ind = 0, wheretogo = 0;
			long leftright = 0;
			for (int j = 0; j < v.size(); j++)
			{
				cur_ind = wheretogo;
				wheretogo = v[j].second;
				int m = abs(cur_ind -  wheretogo);
				m = min(m, l - m);
				leftright += m;
			}
			time += leftright * 5;
		}
		cout << time << endl;
	}
	return 0;
}